In: Computer Science
In the PDF file that will contain your screen shots, explain what is wrong with the code below. Do not just state thing like “line 3 should come after line 7,” you must explain why it needs to be changed. This shows that you are fully understanding what we are going over.
// This program uses a switch-case statement to assign a
// letter grade (A, B, C, D, or F) to a numeric test score.
#include <iostream>
using namespace std;
int main()
{
double testScore;
cout<< "Enter your test score and I will tell you\n";
cout<<"the letter grade you earned: ";
cin>> testScore;
switch (testScore)
{
case (testScore < 60.0):
cout << "Your grade is F. \n";
break;
case (testScore < 70.0):
cout << "Your grade is D. \n";
break;
case (testScore < 80.0):
cout << "Your grade is C. \n";
break;
case (testScore < 90.0):
cout << "Your grade is B. \n";
break;
case (testScore < 100.0):
cout << "Your grade is A. \n";
break;
default:
cout << "That score isn't valid\n";
return 0;
}
Here we are using the 5 cases in the switch case but our scores
will be around 1-100
so we can't assign the proper grade using these 5 case
statements
so we need to divide score with 10 than we will get like 9,8,7,6,5
using case blocks to assign the correct grade:
Another issue:
in case labeles we have written conditions which means these values
always 0 or 1 as we have used < > operators
which will return 0 or 1 so we have only 2 cases here
case 0:
case 1:
if we get anything other than these values default case will
executes and prints This score isn't va;id
Correct Program:
#include <iostream>
using namespace std;
int main(){
double testScore;
cout<< "Enter your test score and I will tell you\n";
cout<<"the letter grade you earned: ";
cin>> testScore;
int g=testScore/10;
switch (g)
{
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
cout << "Your grade is F. \n";
break;
case 6:
cout << "Your grade is D. \n";
break;
case 7:
cout << "Your grade is C. \n";
break;
case 8:
cout << "Your grade is B. \n";
break;
case 9:
cout << "Your grade is A. \n";
break;
default:
cout << "That score isn't valid\n";
return 0;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me