Hello everyone! I am new to C++ and I'm having major issues with arrays and loops, especially in this assignment.
I have a text file with a series of numbers on it:
1 90
2 100
3 89
4 72
5 68
1 92
2 98
3 85
4 77
4 82
1 97
1 87
1 92
2 86
2 76
The first column is student ids, the second is their corresponding grades. I know for a fact there are a total of 15 grades and 20 numbers all together. According to the assignment, my program needs to have two arrays: one of type int called studentId and the other is of type double called grades. The maximum number of elements supported by the arrays is 20 elements.
Here is part of my code:
[// create the arrays. Maximum number of grades supported is 20
const int MAX_GRADES = 20;
int studentId[MAX_GRADES];
double grades[MAX_GRADES]; // this will contain the actual number of grades read in from the input file
int numberGrades;
ifstream inputFile;
inputFile.open("grades.txt");
int count = 0;
// Read the numbers from file into array
while (count < maxGrades && inputFile)
{
inputFile >> studentId[count] >> grades[count];
count++;
}
//close file
inputFile.close();
//display test to make sure above code is correct
for (count = 0; count < maxGrades; count++)
{
cout << studentId[count] << " " << grades[count] << endl;
}
]
I understand the last 5 lines on the output: each array is trying to fill in the remaining elements with random numbers. I have been slaving over this for two days now and I'm not sure how to solve this problem. Can anybody please help?
Thanks! Makes sense now!
I have another question:
Since the text I am given isn't in order, I have to sort it out without touching the text file. So if I choose to display grades for student ID 1, I have to display 90, 92, 97, 87, and 92. Logically speaking for every element inside the studentID array with the value 1, that subscript should match the corresponding grade in the grades array