While loop for student grage average
Nov 5, 2013 at 5:07am UTC
I'm trying to create a program using a while loop where the user can enter the number of students, and then enter then name, and 3 test grades for each student to calculate each students grade average and letter grade. My program runs, but ignores my loop and treats it sequentially. Any tips on where I am going wrong would be most appreciated!
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string studFirstName, studLastName;
int counter, NumStudents, studExam1, studExam2, studExam3, studScore, studAvg;
char grade;
cout << "Enter number of students:" ;
cin >> NumStudents;
cout << endl;
counter = 0;
while (counter <= NumStudents)
{
cout <<"Enter Student first name: " ;
cin>> studFirstName;
cout <<"Enter student last name: " ;
cin>> studLastName;
cout <<"Enter score for exam 1: " ;
cin>> studExam1;
cout <<"Enter score for exam 2: " ;
cin>> studExam2;
cout <<"Enter score for exam 3: " ;
cin>> studExam3;
studScore = studExam1 + studExam2 + studExam3;
studAvg = studScore / 3 ;
counter++;
}
cout <<"Student's Final Grade: " << studAvg << endl;
if (studAvg >= 90)
grade = 'B' ;
else if (studAvg >=80)
grade = 'B' ;
else if (studAvg >=70)
grade = 'C' ;
else if (studAvg >=60)
grade = 'D' ;
else
grade = 'F' ;
cout <<"Grade: " << grade << endl;
system ("pause" );
return 0;
}
Nov 5, 2013 at 5:16am UTC
why dont you put the final grade inside the loop?
Nov 5, 2013 at 5:25am UTC
All of this code should be in your while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
cout <<"Student's Final Grade: " << studAvg << endl;
if (studAvg >= 90)
grade = 'B' ;
else if (studAvg >=80)
grade = 'B' ;
else if (studAvg >=70)
grade = 'C' ;
else if (studAvg >=60)
grade = 'D' ;
else
grade = 'F' ;
cout <<"Grade: " << grade << endl;
Also, for the checking if the grade is above 90, I believe you meant to put 'A', where you put 'B' instead.
Last edited on Nov 5, 2013 at 5:25am UTC
Nov 5, 2013 at 5:30am UTC
Thanks everyone! I think I fixed it, the program runs great now:
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string studFirstName, studLastName;
int counter, NumStudents, studExam1, studExam2, studExam3, studScore, studAvg;
char grade;
cout << "Enter number of students: " ;
cin >> NumStudents;
cout << endl;
counter = 0;
while (counter < NumStudents)
{
cout << endl;
cout <<"Enter Student first name: " ;
cin>> studFirstName;
cout <<"Enter student last name: " ;
cin>> studLastName;
cout <<"Enter score for exam 1: " ;
cin>> studExam1;
cout <<"Enter score for exam 2: " ;
cin>> studExam2;
cout <<"Enter score for exam 3: " ;
cin>> studExam3;
studScore = studExam1 + studExam2 + studExam3;
studAvg = studScore / 3 ;
if (studAvg >= 90)
grade = 'A' ;
else if (studAvg >=80)
grade = 'B' ;
else if (studAvg >=70)
grade = 'C' ;
else if (studAvg >=60)
grade = 'D' ;
else
grade = 'F' ;
cout << studFirstName << " " << studLastName << endl;
cout << "Average Score: " << studAvg << endl;
cout <<"Student's Final Grade: " << grade << endl;
counter++;
}
system ("pause" );
return 0;
}
Topic archived. No new replies allowed.