Home > other >  return and while in function
return and while in function

Time:06-05

This function accepting input and telling the user whether the input is number or not a number.

static string isnum()
{
        Console.WriteLine("Write a number please");
        string a = Console.ReadLine();
        string nums = "123456789";
        int cnt = 0;
        for (int i = 0; i < a.Length; i  )
        {
            for (int j = 0; j < nums.Length; j  )
            {
                if (a[i] == nums[j])
                {
                    cnt  ;
                    break;
                }
            }
        }
        if (cnt == a.Length)
        {
            Console.WriteLine(a   "  is a number");
            return a;
        }
        else
        {
            Console.WriteLine(a   "  is not a number");
            return "";
        } 
}


isnum();

I would like this function to repeat herself if the input is not a number, till the input will be a number, and then to stop. This function working now, but she's working only one time. When I'm trying to add a while block to the function to make her run again and again till the input is number I'm getting the "not all code paths return a value" error.

is it because a "return" statement ends a function, and therefore prevent her to run again? how can I solve that?

Thank you very much!

CodePudding user response:

You can fix this with creating a loop arround it and do not return when it's not a number.

static string isnum()
{
    // just loop forever.
    while (true)
    {
        Console.WriteLine("Write a number please");
        string a = Console.ReadLine();
        string nums = "123456789";
        int cnt = 0;
        for (int i = 0; i < a.Length; i  )
        {
            for (int j = 0; j < nums.Length; j  )
            {
                if (a[i] == nums[j])
                {
                    cnt  ;
                    break;
                }
            }
        }
        if (cnt == a.Length)
        {
            Console.WriteLine(a   "  is a number");
            return a;
        }
        else
        {
            Console.WriteLine(a   "  is not a number");
            // don't return here
        }
    }
}

CodePudding user response:

In this case the best approach is to use do while because you want your code to at least run once.

you have one problem in your code which is returning when variable is not a number. see these modifications:

static string isnum()
{
    do{
        Console.WriteLine("Write a number please");
        string a = Console.ReadLine();
        string nums = "123456789";
        int cnt = 0;
        for (int i = 0; i < a.Length; i  )
        {
            for (int j = 0; j < nums.Length; j  )
            {
                if (a[i] == nums[j])
                {
                    cnt  ;
                    break;
                }
            }
        }
        if (cnt == a.Length)
        {
            Console.WriteLine(a   "  is a number");
            return a;
        }
        else
        {
            Console.WriteLine(a   "  is not a number");
        } 
    }while(true);
}

CodePudding user response:

Call it in a while loop, and loop until the result is a number:

string result = "";
while (result == "")
{
    result = isnum();
}
Console.WriteLine("result is a number: "   result);
  • Related