einspert
einspert

Reputation: 3

Fail of guessing game

So i was learning c# from a tutorial video and the guy on the video showed a guessing game so i wrote the exact code of him

but it has a problem! it does not work accurate

and i guess i see where the problem is but why this guy does not have a problem with it

Code:

string secretWord = "girrafe";
string guess = "";
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;

while(guess != secretWord && !outOfGuesses)
{
    if (guessCount < guessLimit)
    {
        Console.Write("Enter guess ");
        guess = Console.ReadLine();
        guessCount++;
    }
    else
    {
        outOfGuesses = true;
    }

    if (outOfGuesses)
    {
        Console.WriteLine("You Lose!");
    }
    else
    {
        Console.WriteLine("You win");
    }
} 

I think the problem is on the last else statement.

Upvotes: 0

Views: 79

Answers (1)

tnavidi
tnavidi

Reputation: 1393

seems you're missing one closing bracket for the while statement.

string secretWord = "girrafe";
string guess = "";
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;

while(guess != secretWord && !outOfGuesses)
{
    if (guessCount < guessLimit)
    {
        Console.Write("Enter guess ");
        guess = Console.ReadLine();
        guessCount++;
    }
    else
    {
        outOfGuesses = true;
    }
} // <--- this one, to close the while-loop
if (outOfGuesses)
{
    Console.WriteLine("You Lose!");
}
else
{
    Console.WriteLine("You win");
}
 

Upvotes: 4

Related Questions