0

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();
John Gardner
  • 24,225
  • 5
  • 58
  • 76
  • you could do: `string answerX = Console.ReadLine().Trim().ToUpper()`, then `if (answer1 == "YES") { ... } else { ... }` – John Gardner Jun 20 '16 at 22:37

3 Answers3

2

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;
}
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
  • Thanks! That's worked. Is it impossible to have a switch statement without a default case ? – Wassim Dernayka Jun 20 '16 at 23:40
  • No, however, if the variable is uninitialized it is. The compiler looks for every possibility and if `"YES"` or `"NO"` was not passed to the case statement and `int i;` was declare but there was no initialization then your code could have gotten to read `i` and `i` would have had no value. – Eli Sadoff Jun 21 '16 at 01:49
1

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"

Community
  • 1
  • 1
John Gardner
  • 24,225
  • 5
  • 58
  • 76
0

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);
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Your third solution doesn't resolve the problem because if the answer is neither `YES` nor `NO`, `i1` will still be unassigned. – Eli Sadoff Jun 20 '16 at 22:26
  • (its technically happening because the OP is using switch instead of if/else, with no defaults in the switches). assigning default values for these is just going to hide the problem, which the compiler is actually warning them about, that there are paths through this program that don't work they way they think they are. – John Gardner Jun 20 '16 at 22:26
  • @JohnGardner, Yes but if you assign a default value to the variable then you are not bound to have a `default` switch. – Rahul Jun 20 '16 at 22:30
  • @Rahul no worries! – Eli Sadoff Jun 20 '16 at 22:30