My Switch case is broken.

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hello,

So I tried making an application that would ask a student about the score on their test and then translate that to an alphabetical score. I tried doing this with a switch statement, but it seems to hate using numerical values, or it could just be my coding.

So I initialised the grade variables:

C#:
            string A;
            string B;
            string C;
            string D;
            string E;
            string U;

Initially, I did declare them as an int and then specified their grade boundary like this:

C#:
int A = 95;

But I saw no use for it, so I just stuck to the string type.

I then gathered the user's input and saved it:

C#:
            Console.WriteLine("Please enter your first score from 0 to 100: ");
            int userGrade1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter your second score from 0 to 100: ");
            int userGrade2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Please enter your third score from 0 to 100: ");
            int userGrade3 = Convert.ToInt32(Console.ReadLine());

Made a variable to store the output of the switch case:

C#:
string endResult1;


And then I tried setting the switch case which was the downfall of my program:

C#:
switch (userGrade1)
            {
                case => 95:
                    endResult = A;
                    break;


            }

But as you can see, the switch case is very weird. No one on the internet seems to have faced this problem, so there was really no placed I could've used.

Any help welcome, thanks
 
That's not a valid case statement. As the documentation states:
Each case label specifies a constant value.
Unlike a VB 'Select Case', you can't use a C# 'switch' statement to match lists or ranges of values. To do what you want to do, i.e. set 'endResult' to "A" if 'userGrade1' is 95 or greater, you'd have to do this:
switch (userGrade1)
{
    case 100:
    case 99:
    case 98:
    case 97:
    case 96:
    case 95:
        endResult = "A";
        break;
}

I think that it's safe to say that you don't want to provide a case for such a large number of possible values so the solution is to not use a 'switch' statement but rather use an 'if...else if' instead.
 
Hello,

Thanks for the reply. I was just trying the If and Else If statements and I really screwed up.

I cannot find a way to compare them!

C#:
Console.WriteLine("Enter the score that you received on your exam: ");
            int userInput = Convert.ToInt32(Console.ReadLine());
            if (userInput >= 89 && userInput <= 69)
            {
                Console.WriteLine("You have achived a grade A!");
            }
            else if (userInput >= 68 && userInput <= 56)
            {
                Console.WriteLine("You have achived a grade B.");
            }
            if (userInput >= 55 && userInput <= 45)
            {
                Console.WriteLine("You have achived a grade C.");
            }
            else if (userInput >= 44 && userInput <= 36)
            {
                Console.WriteLine("You have achived a grade D.");
            }
            if (userInput <= 35)
            {
                Console.WriteLine("You have failed!");
            }
            Console.ReadKey();

I tried to set boundaries with the <= function but it did not like it after the first input. It just crashes.

I also tried without the 'lower than' boundary but that would randomly output "You have achieved a grade C" even though it outputs the first line correctly.

This kind of thing really makes me pull my hair. It was supposed to be a simple application that helped practice my skills but it has been the exact opposite.
 
C#:
if (userInput >= 89 && userInput <= 69)
How can anything ever be greater than or equal to 89 and simultaneously less than or equal to 69? That's obvious nonsense.

As I often say, the way to solve a programming problem like this is to start by forgetting that it's a programming problem. Assume that it's a manual problem that must be solved manually. Assume that you have been given a complete imbecile to do the work and you have to provide them with written, step by step instructions on how to do it. What instructions would you provide? Pick up a pen and paper and write them down. You should then be able to follow those instructions manually and come up with the same output as your program is supposed to do.

Assuming that those instructions work, you have now solved the problem. Writing the code is then not a case of solving the problem but merely implementing the solution that you already have. As you write the code, you can refer back to your written algorithm at any time to check that your code does EXACTLY what it's supposed to do.

As a clue in this case, you never need more than one condition to check whether a score corresponds to a grade. If you first check whether a score corresponds to a grade and it doesn't then you have implicitly established that the score falls below an upper bound so you only need to check the lower bound for the next grade. You don't have to check an upper bound for the highest grade.
 
I did some digging and I managed to fix it.

This is the new code:
C#:
 Console.WriteLine("Enter the score that you recived on your exam: ");
            int userInput = Convert.ToInt32(Console.ReadLine());
            if (userInput >= 89)
            {
                Console.WriteLine("You have achived a grade A!");
            }
            else if (userInput >= 68)
            {
                Console.WriteLine("You have achived a grade B.");
            }
            else if (userInput >= 55)
            {
                Console.WriteLine("You have achived a grade C.");
            }
            else if (userInput >= 44)
            {
                Console.WriteLine("You have achived a grade D.");
            }
            else if (userInput <= 35)
            {
                Console.WriteLine("You have failed!");
            }
            Console.ReadKey();

Just changed some of the IF statements to Else-If statements and removed the useless lower boundaries (as you stated).

Thanks, Mayyte (in an Aussie accent) :)
 
C#:
 Console.WriteLine("Enter the score that you recived on your exam: ");
            int userInput = Convert.ToInt32(Console.ReadLine());
            if (userInput >= 89)
            {
                Console.WriteLine("You have achived a grade A!");
            }
            else if (userInput >= 68)
            {
                Console.WriteLine("You have achived a grade B.");
            }
            else if (userInput >= 55)
            {
                Console.WriteLine("You have achived a grade C.");
            }
            else if (userInput >= 44)
            {
                Console.WriteLine("You have achived a grade D.");
            }
            else if (userInput <= 35)
            {
                Console.WriteLine("You have failed!");
            }
            Console.ReadKey();
That code will do nothing for values from 36 to 43.
Thanks, Mayyte (in an Aussie accent) :)
I thought you were a local for a moment there. ;)
 
Back
Top Bottom