GPA C++ Program that uses 1 function and 2 logical structures. This program I am working on is going to be able for students to enter A,B,C,D,F
for eight classes and it should submit them there GPA. but my problem is how to get them to have 8 different selction areas and give the output correctly does any one have a sample program for me to base this off of.
Where the part of actually calculating your GPA if have about 64 or more if statements I need to slim this down and I need help
thanks for the help in the past
I would, instead of using 64 if statements for all the possible combinations, just calculate the average yourself. Also I would consider making the grades an array of char's; it would probably make it easier to expand/understand.
1 2 3 4 5 6 7 8
float result = 0;
for(int i = 0; i <= array_size; ++i) {
if(grades[i] == "A") result += 4;
elseif(grades[i] == "B") // etc...
}
result /= array_size;
cout.precision(2);
cout<<"Your GPA was "<<result<<endl;
This is the first method I thought of, might not be the best but it should work. Also, you repeat if statement checks after every entry twice, so if a person enters an incorrect letter twice, then they go on to the next question anyway. You should use a while loop:
1 2 3 4 5 6 7 8 9
bool cont = false;
while(!cont) {
cout<<"Please enter your grade: ";
cin>>char1;
if (char1 != "A" && char1 != "B" && char1 != "C" && char1 != "D" && char1 != "F") {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont = false;
} else cont = true;
}
Remove the bool from in front of cont any times you refer to it after it has been declared. Otherwise the compiler thinks you are trying to declare a new variable.
Okay so I got some of the kinks out of it and changed the theme now my problem is how to get the average to turn into the gpa without a lot of cout statements
I included the basic gpa chart for our school district
//*/
// Name: Seniors
// Copyright: J.A. Fair High School
// Author: Truate Bearden
// Date: 05/05/08 10:53
// Description: This is a C++ program that displays your GPA
// after entering the 8 grades you have
//*/
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
int char1, char2, char3, char4, char5, char6, char7, char8, total;
float average, gpa;
//Starts of with directions and the first question
cout<<"Please enter your Grades"<<endl;
cout<<"Please enter 'A', 'B', 'C', 'D', 'F'"<<endl;
cout<<"Enter Grade 1?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont1 = false;
while(!cont1) {
cout<<"";
cin>>char1;
if (char1 >= 100 )
{
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont1 = false;
} else cont1 = true;
}
cout<<"Enter Grade 2?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont2 = false;
while(!cont2)
{
cout<<"";
cin>>char2;
if (char2 >= 100 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont2 = false;
} else cont2 = true;
}
cout<<"Enter Grade 3?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont3 = false;
while(!cont3) {
cout<<"";
cin>>char3;
if (char3 >= 100 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont3 = false;
} else cont3 = true;
}
cout<<"Enter Grade 4?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont4 = false;
while(!cont4) {
cout<<"";
cin>>char4;
if (char4 >= 100 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont4 = false;
} else cont4 = true;
}
cout<<"Enter Grade 5?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont5 = false;
while(!cont5) {
cout<<"";
cin>>char5;
if (char5 >= 100 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont5 = false;
} else cont5 = true;
}
cout<<"Enter Grade 6?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont6 = false;
while(!cont6) {
cout<<"";
cin>>char6;
if (char6 >= 100 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont6 = false;
} else cont6 = true;
}
cout<<"Enter Grade 7?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont7 = false;
while(!cont7) {
cout<<"";
cin>>char7;
if (char7 >= 100 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont7 = false;
} else cont7 = true;
}
cout<<"Enter Grade 8?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont8 = false;
while(!cont8) {
cout<<"";
cin>>char8;
if (char8 >= 100 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont8 = false;
} else cont8 = true;
}
//This is the long list of all possible answers / cout statements that can occur
//With the answers you have given
cout<<"Your answers where"<<endl;
cout<<""<<endl;
cout<<char1<<" "<<char2<<" "<<char3<<" "<<char4<<" "<<char5<<" "<<char6<<" "<<char7<<" "<<char8<<endl;
cout<<""<<endl;
total = char1 + char2 + char3 + char4 + char5 + char6 + char7 + char8;
average = (total / 8);
cout<<"Your total grade average is "<<average<<endl;
/*/if (average = );/*/
{
//All of your possible answers are insured to come out anyway you get this quiz
/*/
4.0 = 95-100
3.9 = 94
3.8 = 93
3.7 = 92
3.6 = 91
3.5 = 90
3.4 = 89
3.3 = 88
3.2 = 87
3.1 = 86
3.0 = 85
2.9 = 84
2.8 = 83
2.7 = 82
2.6 = 81
2.5 = 80
2.4 = 79
2.3 = 78
2.2 = 77
2.1 = 76
2.0 = 75
1.9 = 74
1.8 = 73
1.7 = 72
1.6 = 71
1.5 = 70
1.4 = 69
1.3 = 68
1.2 = 67
1.1 = 66
1.0 = 65
0.9 = 64
0.8 = 63
0.7 = 62
0.6 = 61
0.5 = 60
0.0 = 59
/*/
}
system("PAUSE");
return 0;
}
//*/
// Name: Seniors
// Copyright: J.A. Fair High School
// Author: Truate Bearden
// Date: 05/05/08 10:53
// Description: This is a C++ program that displays your GPA
// after entering the 8 grades you have
//*/
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
int char1, char2, char3, char4, char5, char6, char7, char8, total;
float average, gpa;
//Starts of with directions and the first question
cout<<"Please enter your Grades"<<endl;
cout<<"Please enter 100 - 0"<<endl;
cout<<"Enter Grade 1?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont1 = false;
while(!cont1) {
cout<<"";
cin>>char1;
if (char1 >= 101 )
{
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont1 = false;
} else cont1 = true;
}
cout<<"Enter Grade 2?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont2 = false;
while(!cont2)
{
cout<<"";
cin>>char2;
if (char2 >= 101 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont2 = false;
} else cont2 = true;
}
cout<<"Enter Grade 3?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont3 = false;
while(!cont3) {
cout<<"";
cin>>char3;
if (char3 >= 101 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont3 = false;
} else cont3 = true;
}
cout<<"Enter Grade 4?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont4 = false;
while(!cont4) {
cout<<"";
cin>>char4;
if (char4 >= 101 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont4 = false;
} else cont4 = true;
}
cout<<"Enter Grade 5?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont5 = false;
while(!cont5) {
cout<<"";
cin>>char5;
if (char5 >= 101 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont5 = false;
} else cont5 = true;
}
cout<<"Enter Grade 6?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont6 = false;
while(!cont6) {
cout<<"";
cin>>char6;
if (char6 >= 101 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont6 = false;
} else cont6 = true;
}
cout<<"Enter Grade 7?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont7 = false;
while(!cont7) {
cout<<"";
cin>>char7;
if (char7 >= 101 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont7 = false;
} else cont7 = true;
}
cout<<"Enter Grade 8?"<<endl;
//It will display you entered an incorrect character if you enter the incorrect
//character but will cut the next question if you enter it correctly
bool cont8 = false;
while(!cont8) {
cout<<"";
cin>>char8;
if (char8 >= 101 ) {
cout<<"You entered an incorrect character please re-enter your grade."<<endl;
cont8 = false;
} else cont8 = true;
}
//This is the long list of all possible answers / cout statements that can occur
//With the answers you have given
cout<<"Your answers where"<<endl;
cout<<""<<endl;
cout<<char1<<" "<<char2<<" "<<char3<<" "<<char4<<" "<<char5<<" "<<char6<<" "<<char7<<" "<<char8<<endl;
cout<<""<<endl;
total = char1 + char2 + char3 + char4 + char5 + char6 + char7 + char8;
average = (total / 8);
cout<<"Your semester grade average is "<<average<<endl;
if (average >= 94 ){
cout<<"Your GPA is 4.0"<<endl;
}
if (average >= 91 && average <= 94 ) {
cout<<"Your GPA is 3.750"<<endl;
}
if (average >= 88 && average <= 91 ) {
cout<<"Your GPA is 3.500"<<endl;
}
if (average >= 85 && average <= 88 ) {
cout<<"Your GPA is 3.250"<<endl;
}
if (average >= 82 && average <= 85 ) {
cout<<"Your GPA is 3.0"<<endl;
}
if (average >= 79 && average <= 82 ) {
cout<<"Your GPA is 2.750"<<endl;
}
if (average >= 76 && average <= 79 ) {
cout<<"Your GPA is 2.500"<<endl;
}
if (average >= 73 && average <= 76 ) {
cout<<"Your GPA is 2.250"<<endl;
}
if (average >= 70 && average <= 73 ) {
cout<<"Your GPA is 2.0"<<endl;
}
if (average >= 77 && average <= 70 ) {
cout<<"Your GPA is 1.750"<<endl;
}
if (average >= 64 && average <= 67 ) {
cout<<"Your GPA is .1.500"<<endl;
}
if (average >= 61 && average <= 64 ) {
cout<<"Your GPA is 1.250"<<endl;
}
if (average >= 58 && average <= 61 ) {
cout<<"Your GPA is 1.00"<<endl;
}
if (average >= 55 && average <= 58 ) {
cout<<"Your GPA is 0.750"<<endl;
}
if (average >= 52 && average <= 55 ) {
cout<<"Your GPA is 0.500"<<endl;
}
if ( average <= 52 ) {
cout<<"Your GPA is 0.0"<<endl;
}
{
//All of your possible answers are insured to come out anyway you get this quiz
/*/
4.0 = 95-100
3.9 = 94
3.8 = 93
3.7 = 92
3.6 = 91
3.5 = 90
3.4 = 89
3.3 = 88
3.2 = 87
3.1 = 86
3.0 = 85
2.9 = 84
2.8 = 83
2.7 = 82
2.6 = 81
2.5 = 80
2.4 = 79
2.3 = 78
2.2 = 77
2.1 = 76
2.0 = 75
1.9 = 74
1.8 = 73
1.7 = 72
1.6 = 71
1.5 = 70
1.4 = 69
1.3 = 68
1.2 = 67
1.1 = 66
1.0 = 65
0.9 = 64
0.8 = 63
0.7 = 62
0.6 = 61
0.5 = 60
0.0 = 59
/*/
}
system("PAUSE");
return 0;
}
Argghhh. You should simplify your code. 260 lines is too many. If you simplify your code it will be easier for you to find problems and solutions.
I have coded up a small sample that will read in the results, and display them back to your. Including the total. This is a good starting point that will help you reduce your application to something more maintainable.