Wassim Dernayka
Wassim Dernayka

Reputation: 57

Why am I getting "Use of unassigned local variable"?

I have created a simple program in which the user have to answer some question and see in the end how much credit he had got. The answer should be Yes or No. And recently (after I finished my program and it worked fine), I have noticed that there are amethod ToUpper(). So I wanted to enter it in my program. I have used if statement for useranswer Yes or No and since I can't write like that: if (answer1 == yes), I have stored "Yes" in a string called s. And it worked, but I can't use now s.ToUpper. So I had to change from if statement to switch statement and everything goes alright except that unassigned local variable which store how much each question have credit. The wrong is in g = i1 + i2 + i3 + i4 + i5. Those "i" are unassingned value. Can anyone help me? And if there is a way to use ToUpper with if statement, I don't mind to use it again. Here's my code:

int p = 0;
        int i1;
        int i2;
        int i3;
        int i4;
        int i5;
        int g;
        int qu1 = 10;
        int qu2 = 20;
        int qu3 = 20;
        int qu4 = 25;
        int qu5 = 25;
        Console.WriteLine("Calculating the probability of being diabete patient, please answer by yes or no");
        Console.Write("What is your name ? \n");
        string username = Console.ReadLine();
        Console.Write("Do you smoke ? \n");
        string answer1 = Console.ReadLine();
        switch(answer1.ToUpper())
        {
            case "YES":
                i1 = p + qu1;
                break;
            case "NO":
                i1 = p + 0;
                break;
        }

        Console.Write("do one of your parents have diabetes ? \n");
        string answer2 = Console.ReadLine();
        switch (answer2.ToUpper())
        {
            case "YES":
                i2 = p + qu2;
                break;
            case "NO":
                i2 = p + 0;
                break;
        }

        Console.Write("do u eat ? \n");
        string answer3 = Console.ReadLine();
        switch (answer3.ToUpper())
        {
            case "YES":
                i3 = p + qu3;
                break;
            case "NO":
                i3 = p + 0;
                break;
        }
        Console.Write("do u drink ? \n");
        string answer4 = Console.ReadLine();
        switch (answer4.ToUpper())
        {
            case "YES":
                i4 = p + qu4;
                break;
            case "NO":
                i4 = p + 0;
                break;
        }

        Console.Write("do u speak ? \n");
        string answer5 = Console.ReadLine();
        switch (answer5.ToUpper())
        {
            case "YES":
                i5 = p + qu5;
                break;
            case "NO":
                i5 = p + 0;
                break;
        }

        g = i1 + i2 + i3 + i4 + i5;
        Console.WriteLine(username + "," + "your percentage of gtting diabetes is {0}", g + "%");
        if (g == 100)
        {
            Console.WriteLine("You need to take care, and try to follow a healthy lifestyle and stop smoking");
        }
        if (g >= 50)
        {
            Console.WriteLine("Pay attention, you are no longer in the safe zone");
        }
        if (g <= 50)
        {
            Console.WriteLine("You are in the safe zone, but you can decrease the percentage if you take a little bit care of your health");
        }
        Console.ReadKey();

Upvotes: 0

Views: 122

Answers (3)

Rahul
Rahul

Reputation: 77876

That's probably coming in the below code part cause the variable i1 is not assigned at all as in your code int i1;.

     case "YES":
            i1 = p + qu1;
            break;

You should rather declare and assign it to default value and then use it in Switch statement saying

    int i1 = 0;

(OR)

    int i1 = default(int);

Upvotes: 0

Eli Sadoff
Eli Sadoff

Reputation: 7308

None of your switch statements have default paths so if any of the answers are neither "YES" nor "NO" then the respective i will be unassigned. Either give the is default values or have your switches have default clauses as such:

switch(answer){
  case "YES":
    // case body
  case "NO":
   // case body
  default:
    i = 0;
}

Upvotes: 2

John Gardner
John Gardner

Reputation: 25126

you don't have any default: statements in your switch statements, so the compiler is telling you that if the user typed "NOPE" for answer 1 it doesn't know what to do.

you should either use if/else, or you need to add something like

default:
case "NO":

to make "no" the default answer if someone types anything else.

see related: What should every programmer know about security? "Never trust user input"

Upvotes: 1

Related Questions