Do While and Repetition
Feb 11, 2013 at 3:27am UTC
Hello everyone! I need some help and hopefully I can find a few answers...
I have this program written out, and essentially it is a GPA calculator that is supposed to loop.
How can I have it loop three times without getting an error?
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char grade;
int gradePoints = 0;
int inputCounter = 0;
float gpa = 0;
bool invaildInput = false ;
bool done = false ;
for (int x=0; x <3; x++)
{
while (!done)
{
do
{
cout << "Enter letter grade (Enter 'X' to exit) " ;
cin >> grade;
grade = toupper(grade);
if ((grade != 'A' ) && (grade != 'B' ) && (grade != 'C' )
&& (grade != 'D' )&& (grade != 'F' ) && (grade != 'X' ))
{
cout << "\nInvalid letter grade, please try again\n\n" ;
invaildInput = true ;
}
}
while (!invaildInput);
inputCounter += 1;
switch (grade)
{
case 'A' :
gradePoints += 4;
break ;
case 'B' :
gradePoints += 3;
break ;
case 'C' :
gradePoints += 2;
break ;
case 'D' :
gradePoints += 1;
break ;
case 'F' :
break ;
case 'X' :
done = true ;
inputCounter -= 1;
break ;
}
cout << "Enter letter grade (Enter 'X' to exit) " ;
cin >> grade;
grade = toupper(grade);
}
cout << setprecision(2) << fixed;
gpa = gradePoints / float (inputCounter);
cout << "Total Grade Points: " << gradePoints << endl;
cout << "GPA: " << gpa << "\n\n\n" ;
cout << "Press Enter to Repeat Again" ;
cin.ignore();
}
return 0;
}
Feb 11, 2013 at 3:50am UTC
What is the error?
Feb 11, 2013 at 3:57am UTC
Its human error, haha. I do not know how to get it to repeat 3 times, with each time it saying TEST #1, TEST #2, TEST #3 before entering in grade letter.
Feb 11, 2013 at 4:24am UTC
You are making things complicated with the while loops.
I am posting my quick changes.
Trying to help you since I do not have time to go over your logic.
But read it step by step and analyze it so you understand it for your own good
and make any changes that you like.
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
#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 letter grade (Enter 'X' to exit) " ;
cin >> grade;
counter++;
if (grade =='X' || grade =='x' )
break ;
switch (grade)
{
case 'A' :
gradePoints += 4;
break ;
case 'B' :
gradePoints += 3;
break ;
case 'C' :
gradePoints += 2;
break ;
case 'D' :
gradePoints += 1;
break ;
case 'F' :
gradePoints += 0;
break ;
case 'X' :
counter--;
break ;
default :
cout <<"Wrong input, try again." << endl;
break ;
}// end switch
}// end for loop
cout << setprecision(2) << fixed;
grade = toupper(grade);
gpa = gradePoints / static_cast <double >(counter);// this will convert counter to double
cout << "Total Grade Points: " << gradePoints << endl;
cout << "GPA: " << gpa << "\n\n\n" ;
return 0;
}// main
good luck
Feb 11, 2013 at 4:46am UTC
Thank you!
Topic archived. No new replies allowed.