Help! GPA C++ Program that uses 1 function and 2 logical structures.

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.
Last edited on


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
Last edited on
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;
    else if(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;
}
Last edited on
Whenever I used the piece of coding is say that the bool cont was redeclared and other stuff

http://img356.imageshack.us/my.php?image=questionbr7.png
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.
This is my corrected program I looked at Zhuges code and thought about using it but I dont understand the coding could you explain it to me

My other problem is I want to be able for the user to input a no# and it take place for the letter grade

Example:
A = 100 - 90
B = 89 - 80
C = 79 - 70
D = 69 - 60
F = 59 - 0

Last edited on
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

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//*/
//  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>
using namespace 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;

}

Last edited on
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//*/
//  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>
using namespace 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.

Try something 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
36
37
38
39
#include <iostream>

using namespace std;

int main() {
 int nuOfResults = 8;
 int resultsList[nuOfResults];

 cout<<"Please enter your Grades"<<endl;
 cout<<"Please enter 100 - 0"<<endl;
 
 // Loop 1-nuOfResults to read them in
 for (int i = 0; i < nuOfResults; ++i) {
   cout << "Enter grade " << (i+1) << ": ";   
   
   while(true) {
     int iTemp;
     cin>>iTemp;
     if ( (iTemp > 100 ) || (iTemp < 0) )
       cout<<"You entered an incorrect character please re-enter your grade."<<endl;
     else {
       resultsList[i] = iTemp;
       break;
     }         
   }
 }  
 
 // Now, We have a list of results.
 cout << "Your Results Were:" << endl;
 int iTotal = 0;
 for (int i = 0; i < nuOfResults; ++i) { 
   cout << "For Grade " << (i+1) << " the score was " << resultsList[i] << endl;
   iTotal += resultsList[i];
 }
 cout << "Total Score Was: " << iTotal << endl;

 return 0;
}
Topic archived. No new replies allowed.