Use a Switch statement.A switch statement is just another way of writing an IF statement. The user will enter f, s, j, or r from the keyboard for freshman, sophomore, junior, or senior.
Use character data type for c++. Then you can say something like:
char level ='f';
level=toupper(level);
You would need a loop for 1 to 5, to test for f, s, j, r and also an illegal grade level. Use a "z" for the illegal grade level
Convert the input to upper case so you won't have a case in your switch for both lower and upper case letters.
Remember, a switch statement is just another way of writing an if statement.
Then inside the switch statement print out if they were a freshman, sophomore, etc. spelled all the way out.
So the input for freshmen is a "f", but the output is "Freshmen".
So you have an outer loop from 1 to 5, then inside the loop
you enter a letter
change the letter to upper case
have a switch statement to check what was entered from the keyboard and print the appropriate grade level spelled out as in "Senior".
Don't forget your breaks in the switch statement.
In C++, please us visual studios and show output
#include <iostream>
using namespace std;
int main()
{
char gradelevel,level;
for(int i=0;i<5;i++)
{
cout<<"Enter character f,s,j,r or any other : ";
cin>>gradelevel;
level=toupper(gradelevel);
switch(level)
{
case 'F': cout<<"Freshman"<<endl;
break;
case 'S': cout<<"Sophomore"<<endl;
break;
case 'J':cout<<"Junior"<<endl;
break;
case 'R':cout<<"Senior"<<endl;
break;
default:cout<<"Invalid option";
}//end switch
}//end for loop
return 0;
}//end main
Get Answers For Free
Most questions answered within 1 hours.