Question

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 the code to be used:
// This program illustrates the use of the Switch statement.
// PLACE YOUR NAME HERE
#include <iostream>
using namespace std;
int main()
{
char grade;
cout << "What grade did you earn in Programming I ?" << endl;
cin >> grade;
switch( grade ) // This is where the switch statement begins
{
case 'A': cout << "an A - excellent work !" << endl;
break;
case 'B': cout << "you got a B - good job" << endl;
break;
case 'C': cout << "earning a C is satisfactory" << endl;
break;
case 'D': cout << "while D is passing, there is a problem" << endl;
break;
case 'F': cout << "you failed - better luck next time" << endl;   
break;
default: cout << "You did not enter an A, B, C, D, or F" << endl;
}
return 0;
}

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

Given below is the modified code for the question. Please do rate the answer if it helped. Thank you.


When you remove the break statement, all the cases starting from the case for the given grade are executed. For example, if input is C, then the cout statement for grade C, D, F , default are all executed.


// This program illustrates the use of the Switch statement.
// PLACE YOUR NAME HERE
#include <iostream>
using namespace std;
int main()
{
   char grade;
   cout << "What grade did you earn in Programming I ?" << endl;
   cin >> grade;
  
   switch(grade)
   {
       case 'A':
       case 'B':
       case 'C':
       case 'D':
           cout << "YOU PASSED!";
   }
  
   switch( grade ) // This is where the switch statement begins
   {
   case 'A': cout << "an A - excellent work !" << endl;
   break;
   case 'B': cout << "you got a B - good job" << endl;
   break;
   case 'C': cout << "earning a C is satisfactory" << endl;
   break;
   case 'D': cout << "while D is passing, there is a problem" << endl;
   break;
   case 'F': cout << "you failed - better luck next time" << endl;   
   break;
   default: cout << "You did not enter an A, B, C, D, or F" << endl;
   }
return 0;
}

output
---
What grade did you earn in Programming I ?
A
YOU PASSED!an A - excellent work !

Add a comment
Know the answer?
Add Answer to:
The switch Statement C++program This lab work with the switch statement. • Remove the break statements...
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...

  • 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";...

  • 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...

  • 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,...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • Topics c ++ Loops While Statement Description Write a program that will display a desired message...

    Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement.    Testing For submitting, use the data in the...

  • 41. Executing the "continue" statement from within a loop     causes control to go     A....

    41. Executing the "continue" statement from within a loop     causes control to go     A. to the next line of code     B. out of the loop     C. to the beginning of the loop          D. to check the loop condition for repeating the loop     E. none of the above      42. The 'default' statement inside a 'switch()' is optional.          T__   F__             43. The following 'switch' implementation is legal:              int i = 2;        ...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout...

    Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;

  • Hello i am having a bit of trouble with a verified exit for my program. For...

    Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() {    char chChoice3 = ' ';    do    {        cout << "\n\t--Menu--" << endl;        cout << "1) Enter Player Name" <<...

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