-3

How do I fix the error: "unassigned local variable" for grade? I tried using public and private prefixes before 'string', but it didn't work. Could someone please explain what private and public does as well? Thanks.

using System;

public class Program
{
    public static void Main()
    {
        bool answered = false;
        string grade;

        while(answered == false)
        {
            Console.Write("What is your mark? ");
            int mark = Convert.ToInt16(Console.ReadLine());
            answered = true;        

            if(mark >= 90 && mark <= 100)
            {
                grade = "A";

            } else if(mark >= 80 && mark < 90)
            {
                grade = "B";

            } else if(mark >= 55 && mark < 80)
            {
                grade = "C";

            } else if(mark >= 40 && mark < 55)
            {
                grade = "D";

            } else if(mark >= 0 && mark < 40)
            {
                grade = "E";

            } else
            {
                Console.WriteLine("Please enter your mark between 0-100");
                answered = false;
            }
                Console.WriteLine("Your grade is a " + grade);
                Console.ReadLine();
        }

    }
}
CodingMike
  • 33
  • 7
  • 1
    Does `grade` get assigned if it hits the `else` block? – madreflection Feb 22 '20 at 04:54
  • Assign it something, like an empty string? – Trevor Feb 22 '20 at 04:59
  • Assigning it an empty string worked! Thanks. – CodingMike Feb 22 '20 at 05:00
  • Don't get ahead of yourself, though. Sure, the error goes away, but now the output will be incomplete. You need to test that `grade` isn't empty before writing the output to the console. Or maybe there's a different requirement. – madreflection Feb 22 '20 at 05:04
  • The program now fully works. I added another if statement at the end, because I ended up running into another problem where the message get's printed all the time. I tried using try and catch statements to avoid errors and crashes if the user enters a string instead of an integer, but the message in the catch statement get's printed 100% of the time for some reason... – CodingMike Feb 22 '20 at 05:14

1 Answers1

2

You need to make sure you assign a value to grade in all case, so that it always has a value. You can start by just initializing it with a value:

string grade = "";

Alternatively, set it to a value in all your conditions, which means setting it in your else here:

 else
 {
    Console.WriteLine("Please enter your mark between 0-100");   
    grade = "";
 }
Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • Why did I have to assign it a value though? Not assigning seems to work with numbers. Is it just something I have to do with strings? – CodingMike Feb 22 '20 at 04:59
  • 1
    The difference is not the data type; it's the scope. Fields that aren't initialized will have their default values (e.g. 0, null). Local variables have to be initialized before they're used. – madreflection Feb 22 '20 at 05:01
  • To enhance discussion I read "String.empty" don't create object, when "" make it. http://blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx https://stackoverflow.com/questions/2905378/string-empty-versus – Vonkel. Feb 22 '20 at 09:47
  • 1
    @Vonkel. it's true, but for the purpose of explaining the problem here, the difference isn't a big deal. – Oleksi Feb 22 '20 at 16:11