2

This is my very rudimentary guessing game. The answer is "luigi", but if I type "Luigi", it doesn't register as a valid answer because my secretWord (luigi) is not in the same caps. how do I get around this in c#?

These are my variables:

string secretWord1 = "luigi";

string guess = "";

int guessLimit = 0;

bool outOfGuesses = false;

int guessesLeft = 3;

This my guessing game:

        Console.WriteLine("He is a video game character");
        while (guess != secretWord1 && !outOfGuesses)
        {
            if (guessesLeft > guessLimit)
            {
                Console.Write("Enter guess: ");
                guess = Console.ReadLine();
                guessesLeft--;
                if (guessesLeft == 2 && guess != secretWord1)
                {
                    Console.WriteLine("He is a character in a Nintendo game");
                }
                if (guessesLeft == 1 && guess != secretWord1)
                {
                    Console.WriteLine("He is a character from the Mario Bros. games");
                }
                Console.WriteLine(guessesLeft);
            }
            else
            {
                outOfGuesses = true;
            }
        }
        if (outOfGuesses)
        {
            Console.WriteLine("You Lose");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("You Win!");
            Console.ReadLine();
        }
Khaled
  • 21
  • 2
  • 1
    Does this answer your question? [How can I do a case insensitive string comparison?](https://stackoverflow.com/questions/3121957/how-can-i-do-a-case-insensitive-string-comparison) – nyconing May 08 '20 at 03:06
  • To make sure all cases, you can change the `while` line to: `while (guess.ToLower() != secretWord1.ToLower() && !outOfGuesses)` – Shawn Xiao May 08 '20 at 03:08

2 Answers2

2

Change the 7th line to this

guess = Console.ReadLine().toLower();

Please see

s6xy
  • 398
  • 1
  • 11
1

Use the .Equals String Comparison

!guess.Equals(secretWord1, StringComparison.InvariantCultureIgnoreCase)
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51