Question

Find the errors in following program.   //This program uses a switch statement to assign a...

Find the errors in following program.

 

//This program uses a switch statement to assign a

// letter grade (A, B, C, D, or F) to numeric test score.

 

 

#include <iostream>

uisng  namespace std;

 

int main()

{

           

            int 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)

                                    cout<<"Your grade is F.\n";

                                    break;

                        case (testScore <70)

                                    cout<<"Your grade is D.\n"

                                    break;

                        case (testScore  <80)

                                    cout<<"Your grade is C.\n";

                                    break;

                        case (testScore < 90)

                                    cout<<"Your grade is B.\n";

                                    break;

                        case (testScore <= 100)

                                    cout<<"Yourgrade is A.\n";

                                    break;

                        default:

                                    cout<<"That score isn't valid\n";

                        return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer-

Errors in program-

1) switch statement does not accepts conditions.

case (testScore < 60) generates error

2) case must be followed by semicolon(:)

3) before return there must be closing parenthesis of switch ( } )

Switch Statement-It is a multiway branch statement.In switch
statement a variable is tested for different part of code
against a list of values.When value of variable matched with
case that part of program executed.
Syntax-
switch (expression)
{
case 1: // case 1 executed when expression match with
//value of case 1
break;
case 2: // case 2 executed when expression match with
//value of case 2
break;
default: // default will be executed if expression
// doesn't match any cases
}
Conditions for switch statement-
1) Expression should be a constant value or result of
expression provide constant value'
example-
switch(5+3+8)
switch(4*5+9%6)
here both switch statements are valid because expression
derives a constant value.
2) switch statement does not accepts conditions.

Correct Code-

#include <iostream>
using namespace std;
int main()
{
int testScore; // variable declaration
char grade; // variable declaration
cout<<"Enter your test score and i will tell you\n";
cout<<"the letter grade you earned: ";
cin >> testScore;
if(testScore>=90 && testScore<=100) // test score >= 90 and less that 100
{
grade='A';
}
else if(testScore>=80 && testScore<90) // test score >= 80 and <90
{
grade='B';
}
else if(testScore>=70 && testScore<80) // test score >=70 and < 80
{
grade='C';
}
else if(testScore>=60 && testScore<70) // test score >=60 and < 70
{
grade='D';
}
else if(testScore>=0 && testScore<60) // test score >=0 and <60
{
grade='F';
}
  
switch (grade) // switch statement
{
case 'F': // case for < 60
cout<<"Your grade is F.\n";
break;
case 'D': // case for < 70
cout<<"Your grade is D.\n";
break;
case 'C': // case for < 80
cout<<"Your grade is C.\n";
break;
case 'B': // case for <90
cout<<"Your grade is B.\n";
break;
case 'A': // // case for <=100
cout<<"Yourgrade is A.\n";
break;
default: // // case for invalid grade
cout<<"That score isn't valid\n";
}
return 0;
}

Output-

Add a comment
Know the answer?
Add Answer to:
Find the errors in following program.   //This program uses a switch statement to assign a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Find the errors in following program. //This program uses a switch statement to assign a //...

    Find the errors in following program. //This program uses a switch statement to assign a // letter grade (A, B, C, D, or F) to numeric test score. #include <iostream> uisng namespace std; int main() { int 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) cout<<"Your grade is F.\n"; break; case (testScore <70) cout<<"Your grade is D.\n" break; case (testScore <80) cout<<"Your grade is...

  • The switch Statement C++program This lab work with the switch statement. • Remove the break statements...

    The switch Statement C++program This lab work with the switch statement. • Remove the break statements from each of the cases. What is the effect on the execution of the program? • Add an additional switch statement that allows for a Passing option for a grade of D or better. Use the sample run given below to model your output. Sample Run: What grade did you earn in Programming I? YOU PASSED! An A - excellent work! The following is...

  • Today assignment , find the errors in this code and fixed them. Please I need help...

    Today assignment , find the errors in this code and fixed them. Please I need help with find the errors and how ro fixed them. #include <iostream> #include <cstring> #include <iomanip> using namespace std; const int MAX_CHAR = 100; const int MIN_A = 90; const int MIN_B = 80; const int MIN_C = 70; double getAvg(); char determineGrade(double); void printMsg(char grade); int main() { double score; char grade; char msg[MAX_CHAR]; strcpy(msg, "Excellent!"); strcpy(msg, "Good job!"); strcpy(msg, "You've passed!"); strcpy(msg, "Need...

  • Why is this not giving me a letter grade when run? #include <iostream> #include <iomanip> using...

    Why is this not giving me a letter grade when run? #include <iostream> #include <iomanip> using namespace std; int main() { double grade1, grade2, grade3, grade4, total; cout << "please enter the first grade : "; cin >> grade1; cout << "Please enter the second grade : "; cin >> grade2; cout << "Please enter the third grade : "; cin >> grade3; cout << "Please enter the fourth grade : "; cin >> grade4; total = (grade1 + grade2...

  • I've built a C++ program that calculates the user's class average based on three test. Can...

    I've built a C++ program that calculates the user's class average based on three test. Can somebody show me how to get the program to look for invalid input? #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { //variables int score_1; //test 1 input from user int score_2; //test 2 input from user int score_3; //test 3 input from user int highest; // the higher score of test 1 & 2 int course_average; //average for class //Introduce...

  • I am working on this switch program everything seems to be ok but the compiler is...

    I am working on this switch program everything seems to be ok but the compiler is giving me an error on the final closing brace and wanted to know where I am making an error #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int choice; char repeat; double MidTerm = 0; double FinalExam = 0; double Quiz1 = 0; double Quiz2 = 0; double MidTermGrade = 1, FinalExamGrade = 2, Quiz1Grade = 3, Quiz2Grade = 4,...

  • Could use some help with this, Instructions: You will need to add an input statement for...

    Could use some help with this, Instructions: You will need to add an input statement for the grade variable to this code. Also add a for loop so that you can enter more than one grade. Code the for loop so that at least 7 grades can be entered. #include <iostream> #include <string> using namespace std; void main() {      char grade;           for (int x = 0; x < 7; x++)      {            cout << "Enter a...

  • Find the errors in the following code: / / Warning! This code contains ERRORS! switch (score)...

    Find the errors in the following code: / / Warning! This code contains ERRORS! switch (score) { case (score > 90): grade = 'A'; break; case(score > 80): grade = 'B'; break; case(score > 70): grade = 'C'; break; case (score > 60): grade = 'D'; break; default: grade = 'F'; } *Submit in Java format

  • Find errors in code and get 100% feedback!! Foblem (30 Points) This program This programaverages >...

    Find errors in code and get 100% feedback!! Foblem (30 Points) This program This programaverages > // It uses the ram has errors. Find the errors in the code: n averages 3 test scores. the variable perfect Score as a flag. Line # Line # include <iostream> using namespace std; int maino cout << "Enter your 3 test scores and I will ": <<"average them;": int score1, score2, score3, cin >> score1 >> score2 >> score3; double average; average (scorel...

  • I need MIPS code for this program Translate the following C++ program to MIPS assembly program...

    I need MIPS code for this program Translate the following C++ program to MIPS assembly program (Please explain each instruction of your code by a comment and submit a .asm file) // Example program #include <iostream> #include <string> using namespace std; int main() { const int ADULT_CHOICE= 1, CHILD_CHOICE= 2, SENIOR_CHOICE= 3, QUIT_CHOICE= 4, ADULT = 250, CHILD = 200, SENIOR = 350; int choice, months; int charges = 0; do { cout <<"\n\t\tHealth Club Membership Menu\n\n" <<"1. Standard Adult...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT