-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(); } } } 
5
  • 1
    Does grade get assigned if it hits the else block? Commented Feb 22, 2020 at 4:54
  • Assign it something, like an empty string? Commented Feb 22, 2020 at 4:59
  • Assigning it an empty string worked! Thanks. Commented Feb 22, 2020 at 5: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. Commented Feb 22, 2020 at 5: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... Commented Feb 22, 2020 at 5:14

1 Answer 1

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 = ""; } 
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
To enhance discussion I read "String.empty" don't create object, when "" make it. blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx stackoverflow.com/questions/2905378/string-empty-versus
@Vonkel. it's true, but for the purpose of explaining the problem here, the difference isn't a big deal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.