looping check problem

ok so I have a class assignment that asks me to write a program that will give a letter grade based on what numerical value one inputs. so for example if I put in a numerical value between a 90 and 100 the letter A comes out. if its between 80 and 90 then a B comes out and so on.i cant find a way for it to check if a number lower than 80 was inputed AND output a correct letter grade. what i was trying to do was just keep going through checks until one check is true and then output the correct letter grade but i cant seem to get that to work. if someone could help it wou;d be awesome. I just started learning c++ so im a total noob so please dont assume i know what you are talking about. this is what i have so far...

#include <iostream>
using namespace std;

int main()
{
int grade; // grade variable
bool validNumber = false;
bool validGrade_1 = false;

// checks to see if number inputed is between 0 and 100
// if not then error message occurs and ask to input number again
// then loops until number inputed is between 0 and 100

while (validNumber == false) {

cout << "Please enter a grade between 0 and 100: ";

cin >> grade;


if ((grade < 0 ) || (grade > 100)) {

cout << "ERROR the grade you entered is not between 0 and 100. \n\n";


}

else

validNumber = true;

}




// checks to see what numerical grade was inputed in
// order to determine what letter grade to output

while (validGrade_1 == false) {


if (grade >= 90) {

cout << "The student has an A. \n\n";

break;


}

else
if ((grade <=89.99) || (grade > 79.99)) {

cout << "The student has a B \n";

break;

}
}

// I am stumped after this. I can not find a way for it to correctly
// decide whether a number smaller than 79.99 was inputed so that it can display
// a proper letter grade. When I try to continue, it just keeps on outputting B as a letter grade

system ("pause");
return 0;
Last edited on
I don't understand what while (validGrade_1 == false) { is for? Up until that point it seemed to be ok, but then I lose what your trying to do. After you come out of the first while loop why not just have a few if statements:

1
2
3
4
5
6
7
8
9
if (grade >= 90) {
	cout << "The student has an A. \n\n";
}else if(grade >=80){
	cout << "The student has an B. \n\n";
}else if(grade >=70){
	cout << "The student has an C. \n\n";
}else{
	cout << "The student failed. \n\n";
}


Or something to that effect.

Also when you are posting code use code tags [code]code goes here[/code]

EDIT: Haha, how do you show someone how to use code tags?

Meerkat
Last edited on
closed account (1vRz3TCk)
EDIT: Haha, how do you show someone how to use code tags?

put a bold tag in the middle of the code tag: [co[bold tag here]de] [/co[bold tag here]de]

[code] your code here [/code]
[code] Ah thanks, I had never really thought about how people were able to do that before.[/code]
Last edited on
thank you guys for your help.

ok so i managed to get all of that to work but now im stuck again. now i have to figure out a way for the program to check if the grade entered is 0.5 points or less from the next letter grade. I got it to check but i cant figure out how to set a boundary. for example, in the code, the program will check to see if the grade entered it greater than 99.5 and if its not then it goes on to the next check which is to check if the number is greater than 89.5. the problem im having is that lets say i input 94.5 as my grade. it will skip the first check but not the second because technically 94.5 is greater than 89.5 so it will run the code under the check but then that grade is not 0.5 points away from the next letter grade. If anyone can help i would really appreciate it.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
using namespace std;

int main()
{
	double grade;					// grade variable
	bool validNumber = false;		// constant for numerical grade check
	bool validGrade = false;		// constant for grade over 100 check       
	char yesno = 'y';				// asks user yes or no
	char correct_grade = 'y';		// asks user yes or no
	char round_up_grade = 'n';		// asks user yes or no

	   // checks to see if number inputed is less than 0 
	   // if number inputed is less than 0, error message occurs and ask to input number again
	   // then loops until number inputed is greater than 0

		while (validNumber == false) {

			cout << "Please enter a grade between 0 and 100: ";
			cin >> grade; 
			cout << endl;
		
		if (grade < 0 ) {

                  cout << "ERROR the grade you entered is not between 0 and 100. \n\n";
		}
		  else validNumber = true;          

      }
		
		// If grade entered is over 100, the program asks if that is correct.
		// If it is correct then the program moves on
		// If it is incorrect the it asks for a numerical grade again

		while (validGrade == false) {
			
			if (grade > 100) {
			
				  cout << "Are you sure this grade is correct? \n\n Type y for Yes, n for no: ";
				  cin >> correct_grade;
				 cout << endl;
		}
		 
			if (correct_grade == 'n') {
			
				  cout << "Please enter a grade between 0 and 100: ";
				  cin >> grade;
				 cout << endl;
		}

		else validGrade = true;
		}
		// checks to see if number is 0.5 points away from next letter grade
		// if it is then the program asks the user if they wish to round up
		// to the next letter grade

			if (grade >= 99.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n"; 
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 89.5)  {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 79.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 69.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 59.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			if (round_up_grade == 'y') {
				grade = grade + 0.5;
				cout << "The new grade is " << grade;
				cout << endl;
			}
		// Checks to see if student is eligible for extra credit
		// If user answers yes then studen gets 5 extra points
		// If user answers no then grade remains the same

		cout << "Is the student eligible for extra credit? \n\n" << "Type y for yes, n for no: ";
				cin >> yesno;
				cout << endl;

			if (yesno == 'y'){
				grade = grade+5;
			}

			else if (yesno == 'n') {
				grade = grade;
			}
		
		// checks to see what numerical grade was calculated based on users decisions in 
		// order to determine what letter grade to output

		if (grade > 100) {
				cout << "The student has an A+. \n\n";
}			else if(grade >=90){
				cout << "The student has an A. \n\n";
}			else if(grade >=80){
				cout << "The student has a B. \n\n";
}			else if(grade >=70){
				cout << "The student has a C. \n\n";
}			else if(grade >=60){
				cout << "The student has a D. \n\n";
}			else if(grade < 60){
				cout << "The student has an F. \n\n";
}
		
					

			
	system ("pause");
	return 0;
}
thank you guys for your help.

ok so i managed to get all of that to work but now im stuck again. now i have to figure out a way for the program to check if the grade entered is 0.5 points or less from the next letter grade. I got it to check but i cant figure out how to set a boundary. for example, in the code, the program will check to see if the grade entered it greater than 99.5 and if its not then it goes on to the next check which is to check if the number is greater than 89.5. the problem im having is that lets say i input 94.5 as my grade. it will skip the first check but not the second because technically 94.5 is greater than 89.5 but then that grade is not 0.5 points away from the next letter grade. I guess what i am saying is that i don't know how to set a boundary. If anyone can help i would really appreciate it.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
using namespace std;

int main()
{
	double grade;					// grade variable
	bool validNumber = false;		// constant for numerical grade check
	bool validGrade = false;		// constant for grade over 100 check       
	char yesno = 'y';				// asks user yes or no
	char correct_grade = 'y';		// asks user yes or no
	char round_up_grade = 'n';		// asks user yes or no

	   // checks to see if number inputed is less than 0 
	   // if number inputed is less than 0, error message occurs and ask to input number again
	   // then loops until number inputed is greater than 0

		while (validNumber == false) {

			cout << "Please enter a grade between 0 and 100: ";
			cin >> grade; 
			cout << endl;
		
		if (grade < 0 ) {

                  cout << "ERROR the grade you entered is not between 0 and 100. \n\n";
		}
		  else validNumber = true;          

      }
		
		// If grade entered is over 100, the program asks if that is correct.
		// If it is correct then the program moves on
		// If it is incorrect the it asks for a numerical grade again

		while (validGrade == false) {
			
			if (grade > 100) {
			
				  cout << "Are you sure this grade is correct? \n\n Type y for Yes, n for no: ";
				  cin >> correct_grade;
				 cout << endl;
		}
		 
			if (correct_grade == 'n') {
			
				  cout << "Please enter a grade between 0 and 100: ";
				  cin >> grade;
				 cout << endl;
		}

		else validGrade = true;
		}
		// checks to see if number is 0.5 points away from next letter grade
		// if it is then the program asks the user if they wish to round up
		// to the next letter grade

			if (grade >= 99.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n"; 
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 89.5)  {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 79.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 69.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 59.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			if (round_up_grade == 'y') {
				grade = grade + 0.5;
				cout << "The new grade is " << grade;
				cout << endl;
			}
		// Checks to see if student is eligible for extra credit
		// If user answers yes then studen gets 5 extra points
		// If user answers no then grade remains the same

		cout << "Is the student eligible for extra credit? \n\n" << "Type y for yes, n for no: ";
				cin >> yesno;
				cout << endl;

			if (yesno == 'y'){
				grade = grade+5;
			}

			else if (yesno == 'n') {
				grade = grade;
			}
		
		// checks to see what numerical grade was calculated based on users decisions in 
		// order to determine what letter grade to output

		if (grade > 100) {
				cout << "The student has an A+. \n\n";
}			else if(grade >=90){
				cout << "The student has an A. \n\n";
}			else if(grade >=80){
				cout << "The student has a B. \n\n";
}			else if(grade >=70){
				cout << "The student has a C. \n\n";
}			else if(grade >=60){
				cout << "The student has a D. \n\n";
}			else if(grade < 60){
				cout << "The student has an F. \n\n";
}
		
					

			
	system ("pause");
	return 0;
}
Last edited on
Why not just have more if statements. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (grade >= 90) {
	cout << "The student has an A. \n\n";
}else if (grade >=89.5){
	cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n"; 
	cout << "Would you like to round up to the next letter grade?\n\n";
	cout << "y for Yes, n for No: ";
	cin >> round_up_grade;
	cout << endl;
}else if(grade >=80){
	cout << "The student has an B. \n\n";
}else if(grade >=70){
	cout << "The student has an C. \n\n";
}else{
	cout << "The student failed. \n\n";
}


Meerkat
ok that works fine for anything greater than 99.5 but then what would I do if i wanted to use a grade between 99.4 and 80? In this code below it will do what is after the else if (grade >= 89.5) even if the grade is not 0.5 points away from the next letter grade. so if i use 99.4 as the grade it will print out that it is 0.5 points or less away from the next letter grade even though its not because technically 99.4 is greater than or equal to 89.5.

Like is there anyway that i could set a boundary like (100 > grade >= 99.5) or (90 > grade >= 89.5) that way it checks to see if the grade is only between those set boundaries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (grade > 100) {
				cout << "The student has an A+ \n\n"; 
				
				cout << endl;
			}

			else if (grade >= 99.5)  {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}

			else if (grade >= 89.5) {
				cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n";
				cout << "Would you like to round up to the next letter grade?\n\n";
				cout << "y for Yes, n for No: ";
				cin >> round_up_grade;
				cout << endl;
			}
Last edited on
I don't really get what the problem is? Surely you just want it to look like this:

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
if (grade == 100) {
	cout << "The student has an A+. \n\n";
}else if (grade >=99.5){
	cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n"; 
	cout << "Would you like to round up to the next letter grade?\n\n";
	cout << "y for Yes, n for No: ";
	cin >> round_up_grade;
	cout << endl;
}else if (grade >= 90) {
	cout << "The student has an A. \n\n";
}else if (grade >=89.5){
	cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n"; 
	cout << "Would you like to round up to the next letter grade?\n\n";
	cout << "y for Yes, n for No: ";
	cin >> round_up_grade;
	cout << endl;
}else if (grade >= 80) {
	cout << "The student has an B. \n\n";
}else if (grade >=79.5){
	cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n"; 
	cout << "Would you like to round up to the next letter grade?\n\n";
	cout << "y for Yes, n for No: ";
	cin >> round_up_grade;
	cout << endl;
}else if (grade >= 70) {
	cout << "The student has an C. \n\n";
}else if (grade >=69.5){
	cout << "Grade is " << grade << " which is 0.5 points or less away from the next letter grade. \n\n"; 
	cout << "Would you like to round up to the next letter grade?\n\n";
	cout << "y for Yes, n for No: ";
	cin >> round_up_grade;
	cout << endl;
}else{
	cout << "The student failed. \n\n";
}


you can add in more if statements if you want more grades to be displayed.

Meerkat
Last edited on
I tried something like that but for some reason it did not work..... I feel really stupid.... sorry for making you go through all this trouble. I really appreciate it though. Thank you for all your help!
Hmm, I'm pretty sure that the code above should do what you're trying to achieve. But to touch on something you said earlier:

Like is there anyway that i could set a boundary like (100 > grade >= 99.5)


you can do:

1
2
3
if((grade>=99.5) &&(grade <100)){
//code goes here
}



Meerkat
Last edited on
ok thank you so much. you are a life saver. i will definitely remember that for next time. thanks again for all your help.
Yea no problem. However for the code above you shouldn't need to explicitly say it is between two particular values in any if statement as long as you have each statement checking for "greater than" or "greater than or equal to" as with "else if"s only one of the if statements will execute.

Meerkat
Last edited on
Topic archived. No new replies allowed.