Calculate student GPA using loops

Hi, I'm supposed to write a program that calculates a student's GPA. The problem is that I have to use do-while and forloops for this program, but every time I try to input a do-while loop in the program I end up just getting errors or I get an infinite running loop.

We're supposed to be able to make it ask the to enter their letter grade as many times as they want until they hit "X", exiting and giving them their total GPA. With what I have so far it only asks you to enter it a total of 3 times. I tried iserting a do while everywhere but it always messes up. I have no idea where to go from here, any help would be greatly appreciated!

Here's what I have so far:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
char grade;
double gradePoints = 0;
double gpa = 0;
int counter = 0;

for (int i=0; i<3; i++)
{

cout << "Enter the letter grade (Enter 'X' to exit) \n";
cin >> grade;
counter++;
if (grade =='X' || grade =='x')
break;

switch (grade)
{
case 'A':
case 'a':
gradePoints += 4;
break;
case 'B':
case 'b':
gradePoints += 3;
break;
case 'C':
case 'c':
gradePoints += 2;
break;
case 'D':
case 'd':
gradePoints += 1;
break;
case 'F':
case 'f':
gradePoints += 0;
break;
case 'X':
case 'x':
counter--;
break;
default:
cout <<"This input is invalid. Try again." << endl;
break;
}// end of switch
}// end of for loop

cout << setprecision(2) << fixed;

grade = toupper(grade);
gpa = gradePoints / static_cast<double>(counter);

cout << "Total Grade Points: " << gradePoints << endl;
cout << "GPA: " << gpa << "\n\n\n";

return 0;
}
Hi,

ask the to enter their letter grade as many times as they want until they hit "X", exiting and giving them their total GPA.

here you have to use a do-while loop... not for loops

1
2
3
4
5
6
do 
{
//take the grade
//evaluate it
}
while (//input is X or x) 

Thank you shadder, I just tried inserting that, the problem is now I have everything jumbled up and it's giving me an infinite loop and not recognizing ANY input as valid? :(
I've been so stressed out about finishing this piece as I'm a beginner.
Oh almost forgot to add : please use code tags
http://www.cplusplus.com/articles/jEywvCM9/

could you show us what you did/tried that gave you the holy infinite loop?
Okay, sorry about that! First time here.
I tried a do while, and I do realize X is declared to exit in my switch, but the problem is that it was giving me a "x is not declared in this scope" when I tried it at the end, so all I could think of at the moment is giving x and int of 0. But now when I input X it recognizes it as a valid input and won't exit, without it though it gives me an infinite loop.
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
#include <iostream>
#include	<iomanip>
using namespace std;
 
int main()
{
char grade;
double gradePoints = 0;
double gpa = 0;
int counter = 0;
int x = 0;
 
do
{
 
cout << "Enter the letter grade (Enter 'X' to exit) \n";
cin >> grade;
counter++;
if (grade =='X' || grade =='x')
break;
 
switch (grade)
{
case 'A':
case 'a':
gradePoints += 4;
break;
case 'B':
case 'b':
gradePoints += 3;
break;
case 'C':
case 'c':
gradePoints += 2;
break;
case 'D':
case 'd':
gradePoints += 1;
break;
case 'F':
case 'f':
gradePoints += 0;
break;
case 'X':
case 'x':
counter--;
break;
default:
cout <<"This input is invalid. Try again." << endl;
break;
}// end of switch
}// end of for loop
while ( x == x );
 
cout << setprecision(2) << fixed;
 
grade = toupper(grade);
gpa = gradePoints / static_cast<double>(counter);
 
cout << "Total Grade Points: " << gradePoints << endl;
cout << "GPA: " << gpa	<<	"\n\n\n";
 
return 0;
}
closed account (iN6fizwU)
You don't need a variable x at all; your while statement could just be
while ( true )
since you are using an in-loop test to exit anyway.

Also, you don't need to test for 'X' or 'x' in the switch statement, because the loop would have been exited anyway with these values.

Actually, your code compiled and ran on my PC.
It might just be because I am working on it using IDEONE and not an actual IDE on my computer as I don't have one installed yet. It compiles and gives me a successful output but now the main problem I can fix is that it's taking "X" into account as a 0, thus lowering the GPA score when trying to include X with the input to exit and end the loop. So if I just enter in an "A" for a course and then following an X to exit, it gives me a total GPA of just 2.0 which is wrong. I need X to exit but I can't seem to make it NOT register it as if it were receiving an F which is zero? Thank you for your help so far.
Here's my updated code:

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
#include <iostream>
#include	<iomanip>
using namespace std;
 
int main()
{
char grade;
double gradePoints = 0;
double gpa = 0;
int counter = 0;

 
do
{
 
cout << "Enter the letter grade (Enter 'X' to exit) \n";
cin >> grade;
counter++;
if (grade =='X' || grade =='x')
break;
 
switch (grade)
{
case 'A':
case 'a':
gradePoints += 4;
break;
case 'B':
case 'b':
gradePoints += 3;
break;
case 'C':
case 'c':
gradePoints += 2;
break;
case 'D':
case 'd':
gradePoints += 1;
break;
case 'F':
case 'f':
gradePoints += 0;
break;
case 'X': counter--;
case 'x': counter--;
break;
default:
cout <<"This input is invalid. Try again." << endl;
break;
}// end of switch
}// end of for loop
while (true);
 
cout << setprecision(2) << fixed;
 
grade = toupper(grade);
gpa = gradePoints / static_cast<double>(counter);
 
cout << "Total Grade Points: " << gradePoints << endl;
cout << "GPA: " << gpa	<<	"\n\n\n";
 
return 0;
}
Just put your counter++ on line 18 after the if statement and you are good to go

18
19
20
if (grade =='X' || grade =='x')
break;
 counter++;



Enter the letter grade (Enter 'X' to exit) 
A
Enter the letter grade (Enter 'X' to exit) 
X
Total Grade Points: 4.00
GPA: 4.00


 
Exit code: 0 (normal program termination)

Topic archived. No new replies allowed.