-1

So basically I'm trying to make a quiz game and this subprogram check if the answer is correct. And basically every time i try to run the program in gives me the error that not all code paths return a value even though it is clearly seen that there is a return true; and a return false; statement at the end.

private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
    {
        int adjustmentToTheScore;

        const int EASY_QUESTION_5 = 1;
        const int MEDIUM_QUESTION_10 = 2;
        const int HARD_QUESTION_15 = 3;
        const int GENERALPOINTS = 100;

        if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
        {
            if (answers == userAnswer)
            {
                if (questionNum <= EASY_QUESTION_5)
                {
                    adjustmentToTheScore = (EASY_QUESTION_5 * GENERALPOINTS / totalTimePassed);

                    userScore += adjustmentToTheScore;
                }
                else if (questionNum <= MEDIUM_QUESTION_10)
                {
                    adjustmentToTheScore = (MEDIUM_QUESTION_10 * GENERALPOINTS / totalTimePassed);

                    userScore += adjustmentToTheScore;
                }
                else if (questionNum <= HARD_QUESTION_15)
                {
                    adjustmentToTheScore = (HARD_QUESTION_15 * GENERALPOINTS / totalTimePassed);

                    userScore += adjustmentToTheScore;
                }
                rightAnswerCount++;

                goalSound.SoundLocation = "Goal_Sound.wav";
                goalSound.Play();

                lblTotalCorrect.Text = Convert.ToString(rightAnswerCount);

                if (fastestAnswer == 0 || totalTimePassed < fastestAnswer)
                {
                    fastestAnswer = totalTimePassed;

                    lblFastestAnswer.Text = Convert.ToString(fastestAnswer) + "(s)";
                }

           else
                {
                    adjustmentToTheScore = 10;

                    userScore = userScore - adjustmentToTheScore;

                    booing.SoundLocation = "Booing.wav";
                    booing.Play();
                }

                lblScore.Text = "Score: " + Convert.ToString(userScore);

                return true;
            }
            else
            {
                MessageBox.Show("Invalid answer please put a, b, c or d!");

                return false;
            }

        }
    }
}

}

  • Nesting of conditional statements reduce readability of code, bad habit to code this way.. static code analysis tools would offer you suggestions so improve your code ... [see here](http://stackoverflow.com/questions/268132/invert-if-statement-to-reduce-nesting) some good discussion about this scenario. – felickz Apr 08 '16 at 02:58

6 Answers6

1

That error means you have return statements that are conditional and it's possible that the code can never reach those conditions, thus it won't return a value.

Just cut the return false; part and paste it after the second curly brace below where it is now.

You'll still get the message box and it will still return false. It looks like the only condition where it's true is above. The return statements end evaluation of the method scope. So nothing below the return true will be evaluated once it hits that line.

Joe Ivans
  • 33
  • 6
0

Try adding a return false after the very last else statement.

Dr Archer
  • 348
  • 1
  • 3
  • 14
0

You only return true or false if you enter that first if statement. You need a return at the bottom if you fail the first if.

Joels Elf
  • 714
  • 6
  • 10
0

You need to specify what the program wanted to do if the first condition if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D") evaluates to false. since the method should return a value of type bool for all scenarios. Hence what you have to do is Add a return statement after the if it may either true or false that's up to you. so the snippet will looks like:

private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
    {
       //init here
        if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
        {
            if (answers == userAnswer)
            {
                if (questionNum <= EASY_QUESTION_5)
                {
                    //Some code
                }
                else if (questionNum <= MEDIUM_QUESTION_10)
                {
                    //Some code
                }
                else if (questionNum <= HARD_QUESTION_15)
                {
                    //Some code
                }
                if (true)
                {
                    //Some code
                }

                else
                {
                    //Some code
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
  }  
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

In a non void function, every logical path in the program/method should end returning a value to the caller.

In your case, first if block's false condition doesn't return anything, which is causing the exception

private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
{
    int adjustmentToTheScore;

    const int EASY_QUESTION_5 = 1;
    const int MEDIUM_QUESTION_10 = 2;
    const int HARD_QUESTION_15 = 3;
    const int GENERALPOINTS = 100;

    if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
    {
        if (answers == userAnswer)
        {
            if (questionNum <= EASY_QUESTION_5)
            {
                adjustmentToTheScore = (EASY_QUESTION_5 * GENERALPOINTS / totalTimePassed);

                userScore += adjustmentToTheScore;
            }
            else if (questionNum <= MEDIUM_QUESTION_10)
            {
                adjustmentToTheScore = (MEDIUM_QUESTION_10 * GENERALPOINTS / totalTimePassed);

                userScore += adjustmentToTheScore;
            }
            else if (questionNum <= HARD_QUESTION_15)
            {
                adjustmentToTheScore = (HARD_QUESTION_15 * GENERALPOINTS / totalTimePassed);

                userScore += adjustmentToTheScore;
            }
            rightAnswerCount++;

            goalSound.SoundLocation = "Goal_Sound.wav";
            goalSound.Play();

            lblTotalCorrect.Text = Convert.ToString(rightAnswerCount);

            if (fastestAnswer == 0 || totalTimePassed < fastestAnswer)
            {
                fastestAnswer = totalTimePassed;

                lblFastestAnswer.Text = Convert.ToString(fastestAnswer) + "(s)";
            } else
            {
                adjustmentToTheScore = 10;
                userScore = userScore - adjustmentToTheScore;

                booing.SoundLocation = "Booing.wav";
                booing.Play();
            }

            lblScore.Text = "Score: " + Convert.ToString(userScore);
            return true;
        }
        else
        {
            MessageBox.Show("Invalid answer please put a, b, c or d!");
            return false;
        }
    }

    return false; // this is required.
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0
    private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
        {
            int adjustmentToTheScore;

            const int EASY_QUESTION_5 = 1;
            const int MEDIUM_QUESTION_10 = 2;
            const int HARD_QUESTION_15 = 3;
            const int GENERALPOINTS = 100;

            if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
            {
                if (answers == userAnswer)
                {
                    if (questionNum <= EASY_QUESTION_5)
                    {
                        adjustmentToTheScore = (EASY_QUESTION_5 * GENERALPOINTS / totalTimePassed);

                        userScore += adjustmentToTheScore;
                    }
                    else if (questionNum <= MEDIUM_QUESTION_10)
                    {
                        adjustmentToTheScore = (MEDIUM_QUESTION_10 * GENERALPOINTS / totalTimePassed);

                        userScore += adjustmentToTheScore;
                    }
                    else if (questionNum <= HARD_QUESTION_15)
                    {
                        adjustmentToTheScore = (HARD_QUESTION_15 * GENERALPOINTS / totalTimePassed);

                        userScore += adjustmentToTheScore;
                    }
                    rightAnswerCount++;

                    goalSound.SoundLocation = "Goal_Sound.wav";
                    goalSound.Play();

                    lblTotalCorrect.Text = Convert.ToString(rightAnswerCount);

                    if (fastestAnswer == 0 || totalTimePassed < fastestAnswer)
                    {
                        fastestAnswer = totalTimePassed;

                        lblFastestAnswer.Text = Convert.ToString(fastestAnswer) + "(s)";
                    }

               else
                    {
                        adjustmentToTheScore = 10;

                        userScore = userScore - adjustmentToTheScore;

                        booing.SoundLocation = "Booing.wav";
                        booing.Play();
                    }

                    lblScore.Text = "Score: " + Convert.ToString(userScore);

                    return true;
                }
                else
                {
                    MessageBox.Show("Invalid answer please put a, b, c or d!");

                    return false;
                }

            }
            else  
            {
            return false; --- add for first if condition
            }
    }