Grading Program

Hello! I've made a simple program where a user inserts a test grade number and then receives the letter grade for it. Thanks for looking at it, I am open to any suggestions on improving it thanks :D



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
45
46
47
48
49
50
51
52
/*
Grading Program
Requires:
 variables, data types, and numerical operators
 basic input/output
 logic (if statements, switch statements)

 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

*/
#include <iostream>
 using namespace std;
int score;
int main()

{
cout<<"Hello User, please enter the grade that you have scored in class.\n";
cin>>score;
switch (score)
{case 0 ... 59: {
    cout<<"\nLetter Grade: F";
    break;}

case 60 ... 69:{
cout<<"\nLetter Grade: D";
break;
}

case 70 ... 79:{
cout<<"\nLetter Grade: C";
break;}

case 80 ... 89:{
cout<<"\nLetter Grade: B";
break;}

case 90 ... 100:{
cout<<"\nLetter Grade: A";
break;}

default:
{cout<<"\nInsert proper score (1-100)"<<endl;}

}
return 0;
}
Last edited on
try something like:

1
2
3
if( score > 0 && score < 50 ){
  cout<<endl<<"Lettter Grade: F";
}

the "&&" is a and
Last edited on
Like DVS said (although he got the if statement totally wrong :D), in this case, its much better to use if statements rather than 100 cases.
And in the future, please use code tags - http://www.cplusplus.com/articles/jEywvCM9/


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
int main()
{
	int score; // declare variables inside of main, never globally

	cout << "Hello User, please enter the grade that you have scored in class.\n";
	cin >> score;

	if (score >= 0 && score <= 59)
	{
		cout << "\nLetter Grade: F" << endl;
	}
	else if (score >= 60 && score <= 69)
	{
		cout << "\nLetter Grade: D" << endl;

	}
	else if (score >= 70 && score <= 79)
	{
		cout << "\nLetter Grade: C" << endl;
	}
	else if (score >= 80 && score <= 89)
	{
		cout << "\nLetter Grade: B" << endl;
	}
	else if (score >= 90 <= 100)
	{
		cout << "\nLetter Grade: A" << endl;
	}
	else
	{
		cout << "Nope, that shit aint between 0-100." << endl;
	}

	system("pause");
	return 0;
}
Last edited on
Yep already corrected that
but i have a better version:
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
#include <iostream>
using namespace std;
int main(){
	int score;
	cout << "Hello User, please enter the grade that you have scored in class."<<endl;
	cin >> score;
	cout << endl;	//endl makes a new line just like \n , but it's more beautiful
	if (score>=0 && score<50){
		cout << "Letter Grade: F";
		}

	else if (score>=50 && score<70){
		cout << "Letter Grade: D";
		}

	else if (score>=70 && score<80){
		cout << "Letter Grade: C";
		}

	else if (score>=80 && score<90){
		cout << "Letter Grade: B";
		}

	else if (score>=90 && score<=100){
		cout <<"Letter Grade: A"<<endl;
		cout <<"You have got a perfect score!";
		}

	else {
		cout << "Insert proper score (0-100)" << endl; 
	}
	system("pause");	//Stop the program from exiting so fast
	return 0;
}
Last edited on
All you did was make it worse @DVS. It doesnt get better than my version.

(score<=0 && score>50){

There is not a number on earth that is both less than 0, and at the same time bigger than 50.

Same can be said about all your other if statements.
Last edited on
sorry already correted that after i saw it
@mjamesball9
PS:
use code tags: http://www.cplusplus.com/articles/jEywvCM9/
Thank you for the reference to the code tags.

Why is using if statements better in this situation?

Thanks for the tip of stopping the program.system("pause);
system("pause); Is not the best, but if its for homework and stuff, basically anything thats not a real program then its all good.

a case holds just that, a case. If you were to use if statements, you would need 100 different cases.

1
2
3
4
5
case 0:
case 1:
case 2:
case 3:
case4 :


etc

Thats why using if statements, we can just say between 0 and 59.


Edit: Also, are you doing the beginner exercises?
Last edited on
@TarikNeaj

Would using getch(); be more better for a real program?

So cases are the ones that are supposed to be used individually. And the way I used it was unorthodox, but still works?

case 0 ... 59:

if the ... means all the cases between than yes, but I do not recommend it :D

getch(); is more C style. try cin.ignore();

@TarikNeaj I will be sure to keep that in mind when making my next program. if/else statements and case are suited better for different tasks. And cin.ignore(); can be used to pause the program when needed.

Thanks a lot for the help!
Topic archived. No new replies allowed.