Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am getting this error
not all code paths return a value
. i am returning a string in the program but still why am i getting the error?

class GuessNumber
{
    public int number;
    public Random Rand;

    public string randNum()
    {
        int randomNumber = Rand.Next(1, 9);
        string guessed = "";
        while(guessed !="correct")
        {
            if(randomNumber > number)
            {
                guessed = "too low";
                return guessed;
            }
            else
                if(randomNumber < number)
            {
                guessed = "too high";
                return guessed;
            }
            else
            {
                guessed = "correct";
                return guessed;
            }
        }
    }
}


can someone please explain me.

What I have tried:

i put two parameters inside randNum method and returned one of them but still doesn't work.
Posted
Updated 9-Jul-17 23:53pm
Comments
Paulo Zemek 7-Jul-17 21:03pm    
I think you intended to write while(guessed != "")
The way this code is written, it will never enter the while and, at the end of the method, it will leave without returning.

This one is easy to figure out. What happens when the while conditional expression evaluates to false? You're not in the while loop any more, so where is the return statement there?
 
Share this answer
 
Compiler complain because
C#
public string randNum()
{
    int randomNumber = Rand.Next(1, 9);
    string guessed = "";
    while(guessed !="correct")
    {
        if(randomNumber > number)
        {
            guessed = "too low";
            return guessed;
        }
        else
            if(randomNumber < number)
        {
            guessed = "too high";
            return guessed;
        }
        else
        {
            guessed = "correct";
            return guessed;
        }
    }
    // when execution reach this point, there is no return value.
    // The compiler do not know that execution will never reach this point.
}
 
Share this answer
 
Simply return once after setting value of guessed

class GuessNumber
        {
            public int number;
            public Random Rand;

            public string randNum()
            {
                int randomNumber = Rand.Next(1, 9);
                string guessed = "";
                while (guessed != "correct")
                {
                    if (randomNumber > number)
                    {
                        guessed = "too low";
                        
                    }
                    else
                        if (randomNumber < number)
                    {
                        guessed = "too high";
                        
                    }
                    else
                    {
                        guessed = "correct";
                        
                    }
                }
                return guessed; //simply return once after setting value of guessed
            }
        }
 
Share this answer
 
v3
Comments
[no name] 10-Jul-17 6:55am    
With a relative high Chance to produce a Loop for ever ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900