What am I doing wrong ?

Write a program that allows the user to enter the grade scored in a programming class (0-100).
If the user scored a 100 then notify the user that they got a perfect score.

★ Modify the program so that if the user scored a 90-100 it informs the user that they scored an A

★★ Modify the program so that it will notify the user of their letter grade
0-59 F 60-69 D 70-79 C 80-89 B 90-100 A

Can someone tell me how can I enter a number and that number will tell which grade I have. For example I wanna type 55 and when I typed it I want that the program to tell me F. How can I do that ?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>
using namespace std;

int main() 
{
	int x;

	cout << "What grade did you scored in the programming class: "  <<endl;
	cout << "1. 0 - 59 " <<endl;
	cout << "2. 60 - 69" <<endl;
	cout << "3. 70 - 79" <<endl;
	cout << "4. 80 - 89" <<endl;
	cout << "5. 90 - 100" <<endl;
	cin >> x;

	switch (x) 
	{
	case 1:
		cout << " F " ;
		break;
	case 2:
		cout << " D " ;
		break;
	case 3: 
		cout << " C " ;
		break;
	case 4:
		cout << " B ";
		break;
	case 5:
		cout << " A " ;
		break;
	
	}

	if ( x == 100 )
	{
		cout << "Perfect score!" <<endl;
	}

	system("PAUSE");
	return 0;
	
}
You cand do a if else chain like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
     if (x < 60)
    std::cout << 'F';
else if (x < 70)
    std::cout << 'D';
else if (x < 80)
    std::cout << 'C';
else if (x < 90)
    std::cout << 'B';
else {
    std::cout << 'a' << std::endl;
    if (x == 100)
        std::cout << "perfect score";
}
std::cout << std::endl;

Last edited on
closed account (o3hC5Di1)
Hi there,

You won't be able to use a switch-statement anymore, because that only checks for equality. What you need to check is whether a number is in between a range of numbers, so if/else if statements will serve that purpose. Note that '&&' is the operator for logical AND, meaning both conditions need to be true:

1
2
3
4
5
6
7
if (x >= 0 && x < 60)
    std::cout << " F ";

else if (x >= 60 && x < 70)
    std::cout << " D ";

//etc. 


Hope that helps.

All the best,
NwN
I didnt know how to do that NwN .. you helped me a lot ..
Thx very much !!!
Topic archived. No new replies allowed.