In: Computer Science
In C++ and input code using the template below
The purpose of the program is to demonstrate how to use Switch statement.
/************************************************************ * * FileName: caseswitch.cpp * Author: Guili Liu * Purpose: Demonstrate how to use Switch Statemenet. * **************************************************************/ #include <iostream> using namespace std; int main() { char grade; //Fill in this for loop so it will repeat six times for ( ; ; ) { cout << "Please enter a character grade (A, B, C, D, or F): "; cin >> grade; switch (grade) { case 'A': cout << "Great work. " << endl; break; // Add a case for 'B' that prints "Good work." case 'C': cout << "Passing work. " << endl; break; case 'D': case 'F': cout << "Unsatisfictory work. " << endl; cout << "See your instructor." << endl; break; // Add a default that prints // grade << " is not a legal grade." } //end of switch statement } //end of for loop return 0; } // end program
Code:
#include <iostream>
using namespace std;
int main()
{
char grade;
for (int i=1;i<=6;i++)
{
cout << "Please enter a character grade (A, B, C, D, or F):
";
cin >> grade;
switch (grade)
{
case 'A': cout << "Great work. " << endl;
break;
case 'B': cout << "Good work. " << endl;
break;
case 'C': cout << "Passing work. " << endl;
break;
case 'D':
case 'F': cout << "Unsatisfictory work. " <<
endl;
cout << "See your instructor." << endl;
break;
default:
cout << grade << " is not a legal grade." <<
endl;
} //end of switch statement
} //end of for loop
return 0;
}
// end program
Please refer to the screenshot of the code to understand the indentation of the code:
Output:
For any doubts or questions comment below.