Choose your own adventure step #14

I’m stuck at step #14. The project asks that you check if the user entered “NOTHING” as the answer to the riddle. The tricky thing is that the code flow is already inside of an if statement. I can’t figure out how to check the users answer when I think the program is waiting for the answer to a different question (OPEN or KNOCK). Here’s my code:

 // Start by asking for the user's name:
  Console.Write("What is your name?: ");
  string name = Console.ReadLine();
  Console.WriteLine($"Hello, {name}! Welcome to our story.");

Console.Write(“It begins on a cold rainy night. You’re sitting in your room and hear a noise coming from down the hall. Do you go investigate?”);
Console.WriteLine(“Type YES or NO:”);
string noiseChoice = Console.ReadLine();
string choice1UpperCaseify = noiseChoice.ToUpper();

    if (choice1UpperCaseify == "NO")
    {
      Console.WriteLine("Not much of an adventure if we don't leave our room!");
      Console.WriteLine("THE END.");
    }
    else if (choice1UpperCaseify == "YES")
    {
     Console.WriteLine("You walk into the hallway and see a light coming from under a door down the hall.");
      Console.WriteLine("You walk towards it. Do you open it or knock?");
    Console.WriteLine("Type OPEN or KNOCK:");
    string doorChoice = Console.ReadLine();
    string upperDoorChoice = doorChoice.ToUpper();
  }
    if (upperDoorChoice == "KNOCK")
    {
      Console.WriteLine("A voice behind the door speaks. It says Answer this riddle:");
      Console.WriteLine("Poor people have it. Rich people need it. If you eat it you die. What is it?");
     Console.WriteLine("Type your answer:");
      string riddleAnswer = Console.ReadLine();
      string upperCaseRiddle = riddleAnswer.ToUpper();
    }
/*
    else if (upperCaseRiddle == "NOTHING")
    {
      Console.WriteLine("The door opens and NOTHING is there.You turn off the light and run back to your room and lock the door.");
      Console.WriteLine("THE END");
    }
     else
    {
      Console.WriteLine("The door is locked! See if one of your three keys will open it.");
      Console.WriteLine("Enter a number (1-3):");
      string keyChoice = Console.ReadLine();
     }
    
   else 
    {
      (Console.WriteLine("You answered incorrectly. The door doesn't open."));
       Console.WriteLine("THE END."); 
    }
          
   */
     
    }
  }

}

by the way, I’ve been commenting out portions of the code to try to figure out what specific line is causing this to break.

You need to nest the if() statement, and its code block inside the code block for the user having chosen ‘KNOCK’. See my comments to your code above.
Your basic structure will look similar to this:

if (doorChoice == "KNOCK")
{
  //do stuff
  //get user input
  if (riddleAnswer == "NOTHING")
  {
    //do stuff
  }
  else
  {
    //do other stuff
  }
}
else if (doorChoice == "OPEN")
{
  //do other stuff
}

Also, you can chain methods together to eliminate the need for extra variables and lines of code like so:
string noiseChoice = Console.ReadLine().ToUpper();

Hope this helps!

1 Like

Incredibly helpful! Thank you so much. I was able to get everything cleaned up and working correctly thanks to your help. :star_struck:

1 Like