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
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.
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.
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:
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)