First Solo Challenge

Hi All,

Got stuck with an issue, and would appreciate any help that can be provided. Basically, I am trying to make the game more legible by adding detail about what the user is doing. Everything works, but for some reason, the else statement for CheckPassword() fires even before the user has a chance to enter an input. My code is below:
void StartGame()
{
Terminal.ClearScreen();
InformationChoice();
}

private void InformationChoice()
{
    if (level == 1)
    {
        CurrentScreen = Screen.Password;
        Terminal.WriteLine("You have selected Level " + level);
        Terminal.WriteLine("Breaking into " + LocalElementarySchool);
        Terminal.WriteLine("Enter Password:");
    }
    else if (level == 2)
    {
        CurrentScreen = Screen.Password;
        Terminal.WriteLine("You have selected Level " + level);
        Terminal.WriteLine("Breaking into " + YourCityHall);
        Terminal.WriteLine("Enter Password:");
    }
    else if (level == 3)
    {
        CurrentScreen = Screen.Password;
        Terminal.WriteLine("You have selected Level " + level);
        Terminal.WriteLine("Breaking into " + TheFederalReserve);
        Terminal.WriteLine("Enter Password:");
    }
    else
    {
        Terminal.WriteLine("Not a valid choice");
    }
}

void CheckPassword(string input)
{
    if (input == password)
    {
        Terminal.WriteLine("Congratulations!");
    }
    else 
    {
        Terminal.WriteLine("Try again.");
    }
}

Where am I going wrong that is causing the Try again comment to come up before the user has a chance to guess?

Thanks for any help!

Hi David,

Welcome to our forum. It’s great to see that you are challenging yourself. :slight_smile:

Regarding your problem, try to figure out where the CheckPassword method gets called. If you wrapped the method call in curly brackets, check the if-condition. Maybe there is a misplaced semicolon.


See also:

Thank you so very, very much! :tada: You were absolutely right. The CheckPassword method had a semicolon in the if else block. I had no idea it could cause that much chaos.

It’s one of the typical mistakes when a method gets called for no apparent reason, and it is relatively difficult to find because it is not a mistake in terms of programming. Now that you know about the misplaced semicolon, you know where you could look for a problem next time. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms