Classes and for loops: num[i] only storing one set of inputs?
Jan 27, 2017 at 2:27am UTC
I'm working with creating a weighted average calculator using classes.
Everything is working fine, except that, in the main function, I'm trying to print out the grades inputted for each student, but it's only returning the same value for each student. I've included snippets of the class in question, and the main function.
E.g. I input grades 40 and 50 for student 1 and 60 and 70 for student 2, but the table prints out 60 and 70 for both students.
Line 45 is where the problem is: studentLoop.num[i]
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
class Student
{
public :
float num[100];
int count;
string name[10];
vector <float > gradeVector;
Student(int numberOfAssignments, int numberOfStudents);
};
Student::Student(int numberOfAssignments, int numberOfStudents)
{
for (int count = 0; count < numberOfStudents; count++)
{
cout << endl << "Please enter the name for Student " << count + 1 << " : " ;
cin >> name[count];
cout << endl << "Please enter " << name[count] << "'s grade for each assignment (out of 100):" << endl << endl;
for (int i = 0; i < numberOfAssignments; i++)
{
//Uses previous process for user-input grades
gradeVector.push_back(num[i]);
cout << i + 1 << ". Grade: " ;
cin >> num[i];
if (num[i] < 0 || num[i] > 100)
{
cout << endl << "*** Oops! Please re-enter a valid score between 0 and 100: " ;
cin >> num[i];
cout << endl;
}
}
}
}
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
int main()
{
char answer;
float numDecimal, weightDecimal;
int numberOfStudents;
do
{
float sum = 0.0;
cout << "Enter number of students: " ;
cin >> numberOfStudents;
Weights getWeights;
if (getWeights.sumOfWeights != getWeights.MAXWEIGHT)
{
cout << endl << "Oops! Weights must add up to 100." << endl;
}
else
{
Student studentLoop(getWeights.numberOfAssignments, numberOfStudents);
for (int i = 0; i < getWeights.numberOfAssignments; i++)
{
//Creates decimals from the input percentage grades
numDecimal = studentLoop.num[i] / 100;
weightDecimal = getWeights.weight[i] / 100;
//Calculates the weighted average using the equation [ (gradeA)*(weightA) + (gradeB)*(weightB) + ... ]
sum += (numDecimal * weightDecimal);
}
//Create table of input grades and weights for each student
for (int j = 0; j < numberOfStudents; j++)
{
cout << "-----------------------" << endl << studentLoop.name[j] << endl << "-----------------------" << endl;
cout << "Grades: " << "\tWeights: " << endl << endl;
for (int i = 0; i < getWeights.numberOfAssignments; i++)
{
cout << " " << studentLoop.num[i] << "\t\t " << getWeights.weight[i] << endl;
}
}
}
//Allows user to exit or begin a new play through
cout << "-----------------------" << endl << endl << "Would you like to begin again? Reply Y or N: " ;
cin >> answer;
cout << " " << endl;
} while ((answer == 'Y' ) || (answer == 'y' ));
return 0;
}
Last edited on Jan 27, 2017 at 2:29am UTC
Jan 27, 2017 at 8:06am UTC
The correlation of the names and the grades does not work that way. You should have an array of Student
which contains the grades.
Topic archived. No new replies allowed.