please help me! this is in the morning!!!!!

I am having the hardest time. I cant use anything advanced it has to be simple loops, while loops and must use switch instead of if statements.


Write a main function to do the following "entering grade" task:

* Ask the user to enter either "E" for entering a number, or "Q" for quit.

* If "E" is entered, the user is asked to enter an integer (grade) ranging from 0 to 100. Your code will print out a letter-grade on screen based on the number entered. If the number is between 0-59, print "F", 61-69 for "D", 70-79 for "C", 80-89 for "B", and 90-100 for "A". Requirement: you MUST use switch rather than if-else statements to implement the task.

* The process will be repeated until a "Q" is entered.
closed account (48T7M4Gy)
Yep C++ can be applied here.
Perhaps if you divide the number by 10 and use the result as the switch value?
#include <iostream>
using namespace std;
int main()
{
int grade;
cout<<"Enter the number of ur grade ";
cin>>grade;

if (grade >=90 && grade <= 100)
cout<<"you got A"<<endl;

switch (grade)
{
case 1:
{
if (grade >=80 && grade <= 89)
cout<<"you got B"<<endl;
break;
}
case 2:
{
if (grade >=70 && grade <= 79)
cout<<"you got C"<<endl;
break;
}
case 3:
{
if (grade >=60 && grade <= 69)
cout<<"you got D"<<endl;
break;
}
case 4 :
{
if (grade >=0 && grade <=59)
cout<<"you got F"<<endl;
break;
}
default :
cout<<"Error"<<endl;
}
return 0;
}

this is the latest code I tried with no luck.
Hey,

I think you have used switch for wrong thing. Use switch to determine if user entered 'E' or 'Q'. If 'E' calculate grade letter, If 'Q' just exit. Maybe this will help you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    
    //while( 1 )
    
    	// get user choice
    	
    	//switch choice
    		//case 'E'
    			//calculate letter
    			
    		//case 'Q'
    			//exit 
}


To get grade letter you could switch value grade / 10.
0 - 5 = 'F'
6 = 'D'
7 = 'C'
8 = 'B'
9 - 10 = 'A'

I encourage you to move calculating letter to another function :)
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

int main()
{
    int grade = 0;
    char mark = 'E';
    
    do{
        std::cout << "Please enter grade: ";
        std::cin >> grade;
        
        if ( grade >= 90 )
            mark = 'A';
        else if ( grade >=80 )
            mark = 'B';
        else if ( grade >= 70 )
            mark = 'C';
        else if ( grade >= 60 )
            mark = 'D';
        else
            mark = 'F';
        
        std::cout << "Grade = " << grade << " Mark = " << mark << std::endl;
        
        std::cout << "Please enter 'E' to enter grade, or 'Q' to quit" << std::endl;
        std::cin >> mark;
    }while( mark != 'Q');
    
    return 0;
}
right above my reply has great result(with tiny conceptual problem), just use same platform and change if to switch. case 1, 2 and so on. you will get 90+
thank you guys for the help. I understand it better now.
Topic archived. No new replies allowed.