0

I added the initialization to show the array. When it hits the else and tries to insert the input into the array it throws an exception. I have tried several different examples online, and have not yet found a solution. I am appreciative of any help that is given.

    private char[] currentMisses;

        public int makeTurn()
    {

        // Ask the user for a guess
        Console.Write("Choose A Letter: ");

        // Get the input from the user

             var currentGuess = Console.ReadKey();
             char input = currentGuess.KeyChar;
             input = Char.ToUpper(input);

        // Do input conversions
        while (!char.IsLetter(input))
                {
                    //Console.Write(input);
                    currentGuess = Console.ReadKey(true);
                    input = currentGuess.KeyChar;
                    input = Char.ToUpper(input);
                }

        // See if it exists in our word
        bool test = false;
        test = currentWord.Contains(input);
        Console.WriteLine("Hello" + input);


        // If we didn't find it
        if (!test)
            {
                if (currentMisses != null)
                {
                    Console.WriteLine("WORD");
                    // Make sure it does not currently exist in the misses array
                    for (int i = 0; i < currentMisses.Length; i++)
                    {
                        if (currentMisses[i] != input)
                        {
                            currentMisses[i] = input;
                            Console.WriteLine("In Loop");
                        }
                        Console.WriteLine("Not in Loop");
                    }
                }
                else
                {  
                   /* This is where I am trying to insert the input from the   user to the char array currentMisses, I've tried multiple ways and it seems simple but I hit a roadblock*/                
                    currentMisses[0] = input;                      
                }                 
CodeCaster
  • 131,656
  • 19
  • 190
  • 236

2 Answers2

1

Your logic is off a bit here. In your if statement you are saying "if my array is not null" then loop over the array else (i.e. You have a null array) "try to insert into that null array"

You need to initialize your array with something like:

private char[] currentMisses = new char[x]

Where x is how big you need your array to be.

James Pack
  • 774
  • 1
  • 9
  • 19
1

I'd change this:

private char[] currentMisses

to this:

int numberOfWrongGuessesAllowed = 10 //however many guess you allow user to make
private char[] currentMisses = new char[numberOfWrongGuessesAllowed]
BlackHatSamurai
  • 21,845
  • 20
  • 84
  • 147