1

So i Have been writing this program for a project and everything seemed to work out fine. I've been checking my work with my instructors code, but I don't have a copy of the top 40 lines of code to check with and I can't figure out what caused this problem

    static void Main(string[] args)
    {
        int count = 0;
        string[] names = new string[MAX_SIZE];
        int[] scores = new int[MAX_SIZE];
        string[] splitInput = new string[MAX_SIZE];

        int sum = 0;
        int minScore = 0;
        int maxScore = 0;

        string input;

        string minName;
        string maxName;

        Console.WriteLine("===============Saturday Night Coders================");
        Console.WriteLine("===============Bowling Score Program================");

        for (int i = 0; i < MAX_SIZE; i++)
        {




            Console.WriteLine("\n Please Enter a name and a score separated by a space");
            Console.WriteLine("Enter a blank line when finished");

            input = Console.ReadLine();

            if (input == "")

            {
                Console.WriteLine("===========INPUT COMPLETE=========");
                break;
            }





            splitInput = input.Split();

            string name = splitInput[0];
            int score = int.Parse(splitInput[1]);

            names[i] = name;
            scores[i] = score;
            sum += score;




            if (minScore > score)
            {
                minScore = score;
                minName = name;
            }
            if (maxScore < score)
            {
                maxScore = score;
                maxName = name;
            }

            count = i + 1;
        }

        double average = sum / count;
        Console.WriteLine("Here are the scores for this game");
        PrintScores(names, scores, count);
        Console.WriteLine("Congratulations {0}, your score of {1} was the highest", maxName, maxScore);
        Console.WriteLine("{0} , your score of {1} was the lowest, Maybe you should find a new hobby", minName, minScore);
        Console.WriteLine("\n The team average was {0:f2}", average);
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
    static void PrintScores(string[] names, int[] scores, int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.Write("{0} \t {1}", names[i], scores[i]);
            if (scores[i] == MAX_SCORE)
            {
                Console.WriteLine("*");
            }
            else
            {
                Console.WriteLine("");
            }
            Console.WriteLine();
        }
    }



}

the problem I'm having is with this section of code here

if (minScore > score)
            {
                minScore = score;
                minName = name;
            }
            if (maxScore < score)
            {
                maxScore = score;
                maxName = name;
            }

            count = i + 1;
        }

        double average = sum / count;
        Console.WriteLine("Here are the scores for this game");
        PrintScores(names, scores, count);
        Console.WriteLine("Congratulations {0}, your score of {1} was the highest", maxName, maxScore);
        Console.WriteLine("{0} , your score of {1} was the lowest, Maybe you should find a new hobby", minName, minScore);

the unassigned use of local variables error is with the minName, and maxName values. if i declare them with minName = ""; maxName = ""; the code will compile but then the name for the person that scored the lowest will be "" and the score will be 0. this all seemed to work until i added the PrintScores method. any help is appreciated, I've been fiddling with it for well over an hour now and still can't seem to figure out a solution

IR_GRIM_KC
  • 23
  • 2

1 Answers1

2

You declare minName and maxName outside your for loop. You only assign them inside the for loop... The problem: If the for loop does not run, the variables are not assigned. Therefor the compiler forbids to use them.

Solution: Simply initialize them with meaningful values, like string.Empty.

sdfgsdfg
  • 126
  • 1