Console.ReadLine fails to change input with .ToUpper

350 Views Asked by At

I'm creating a Choose your own adventure game and I'm trying to get the user input to change to uppercase using .ToUpper(); but for whatever reason if the input isn't already caps it skips the else if statement and goes to the next step in the game. Here's how my code currently looks.

Console.WriteLine("It begins on a cold rainy night. Your sitting in your room and hear a noise coming from down the hall. Do you go investigate?");

//First decision
Console.Write("Type YES or NO: ");
string noiseChoice = Console.ReadLine();
string capNoise = noiseChoice.ToUpper();
      

//First decision outcome      
if (noiseChoice == "NO")
{
    Console.WriteLine("Not much of an adventure if you don't leave your room!");
    Console.WriteLine("THE END.");
}
else if (noiseChoice == "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?");
}

//Second decision
Console.Write("Type OPEN or KNOCK: ");
string doorChoice = Console.ReadLine();
string capDoor = doorChoice.ToUpper();

When prompted to type YES or NO, if you input yes or no it skips to the next Console.Write(); being Type OPEN or KNOCK. However, if the input is YES or NO it proceeds thru the else if statement as desired. I'm fairly new to C# so any help is appreciated!

1

There are 1 best solutions below

0
On

You are checking the wrong variable.

Here you convert the text using ToUpper() and place the upper-cased text into the capNoise variable

string capNoise = noiseChoice.ToUpper();
  

But then you need to check capNoise which has the upper case text, but instead you are checking the noiseChoice which is your original input.

if (noiseChoice == "NO")

So you either need to store the ToUpper() result in noiseChoise.

noiseChoice= noiseChoice.ToUpper();

or check capNoise

if (capNoise == "NO")