Stuck On True Or False

Link to project:
https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-true-or-false
.
Code:

using System; namespace TrueOrFalse { class Program { static void Main(string[] args) { string[] questions = {"Does Mario own Owlé?", "Is Mario perfect?"}; bool[] answers = {true, false}; RunQuiz(questions, answers); } static void RunQuiz(string[] questions, bool[] answers) { while (true) { // Do not edit these lines Console.WriteLine("Welcome to 'True or False?'\nPress Enter to begin:"); string entry = Console.ReadLine(); Tools.SetUpInputStream(entry); bool[] responses = new bool[questions.Length]; if (questions.Length != answers.Length) { Console.WriteLine("There is either too less or too many questions."); } int askingIndex = 0; string input; bool isBool; bool inputBool; foreach (string question in questions) { Console.WriteLine(question); Console.Write("True or false? "); input = Console.ReadLine(); isBool = Boolean.TryParse(input, out inputBool); while (!isBool) { Console.WriteLine("Please respond with 'true' or 'false'."); input = Console.ReadLine(); isBool = Boolean.TryParse(input, out inputBool); } responses[askingIndex] = inputBool; askingIndex++; } int scoringIndex = 0; int score = 0; foreach (bool answer in answers) { bool response = responses[scoringIndex]; Console.WriteLine($"{scoringIndex + 1}. Input: {response} | Answer: {answer}"); if (response == answer) { score++; } scoringIndex++; } Console.WriteLine($"You got {score} out of {answers.Length} correct!"); while (true) { Console.WriteLine("Would you like to play again? Enter yes or no. "); string playAgain = Console.ReadLine().ToLower(); if ((playAgain == "no") || (playAgain == "yes")) { break; } } // This is causing the error: if (playAgain == "no") { break; } } } } }

Error:


.

How do I fix it? I need playAgain.

The instantiation of playAgain is in a conditional branch. This means that the program is not guaranteed to have this exist. The thing here is to recognize that the compiler will not evaluate even if something is guaranteed to run in theory (like while(true)) because its job is not to evaluate conditionals (that’s the execution of the program, not the compilation step).

Which is why when it tries to build it runs into trouble.

3 Likes

I don’t understand. The instantiation of playAgain is not in a conditional, it is in a while loop. Why is it still causing an error?

A while loop only runs on a conditional basis. It’s not guaranteed to run by definition. You might see it as obvious that it runs because your condition is one that always triggers i.e. while (true)… but C# is a compiled language (unlike say, Python). This means the compiler has to build the executable from the source code and to do that it has to follow some protocols (like type checking). Like I wrote before, the compiler can’t check that the condition attached to while works or not. Meaning everything that falls under the while-loop block doesn’t get considered when taking into account object instantiations.

In theory a for-loop is also dependent upon a condition (often based on a counter). If one is trying to use a loop that does something strictly unconditionally, one may consider a do-while loop (which, I don’t think is necessary in this scenario).

1 Like

Hi,

Hopefully I can explain a little better. You declared the variable inside of a loop, and then tried to access it outside of that loop. I’ve fixed your code below, basically by declaring the variable first outside of the loop.
However, if you don’t initialise the variable playAgain it will create another error. When you later ask if playAgain == no, so to fix this I’ve initialised it as an empty string.
Your corrected code is below. This will now run.
However if you are creating this in visual studio you will also have to copy across the hidden tools class. Otherwise you will get another error. If you need help on how to do this leave a message here and I will explain how to do this.

using System;

namespace TrueOrFalse
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] questions = { "Does Mario own Owlé?", "Is Mario perfect?" };
            bool[] answers = { true, false };
            RunQuiz(questions, answers);
        }

        static void RunQuiz(string[] questions, bool[] answers)
        {
            while (true)
            {
                // Do not edit these lines
                Console.WriteLine("Welcome to 'True or False?'\nPress Enter to begin:");
                string entry = Console.ReadLine();
                Tools.SetUpInputStream(entry);
                bool[] responses = new bool[questions.Length];

                if (questions.Length != answers.Length)
                {
                    Console.WriteLine("There is either too less or too many questions.");
                }

                int askingIndex = 0;
                string input;
                bool isBool;
                bool inputBool;
                foreach (string question in questions)
                {
                    Console.WriteLine(question);
                    Console.Write("True or false? ");
                    input = Console.ReadLine();
                    isBool = Boolean.TryParse(input, out inputBool);

                    while (!isBool)
                    {
                        Console.WriteLine("Please respond with 'true' or 'false'.");
                        input = Console.ReadLine();
                        isBool = Boolean.TryParse(input, out inputBool);
                    }

                    responses[askingIndex] = inputBool;
                    askingIndex++;
                }

                int scoringIndex = 0;
                int score = 0;
                foreach (bool answer in answers)
                {
                    bool response = responses[scoringIndex];
                    Console.WriteLine($"{scoringIndex + 1}. Input: {response} | Answer: {answer}");
                    if (response == answer)
                    {
                        score++;
                    }

                    scoringIndex++;
                }

                Console.WriteLine($"You got {score} out of {answers.Length} correct!");

                //this is what I have added
                string playAgain ="";

                while (true)
                {
                    Console.WriteLine("Would you like to play again? Enter yes or no. ");
                    playAgain = Console.ReadLine().ToLower();

                    if ((playAgain == "no") || (playAgain == "yes"))
                    {
                        break;
                    }
                }

                // This is causing the error:
                if (playAgain == "no")
                {
                    break;
                }
            }
        }
    }
}
1 Like

That was how I decided to fix it(I did it by my own) in this topic:

Anyway I fixed that problem without help which can fix this problem.