Struggling really hard with this, I need to enter an ID number thats in a text file and output the associated grades that are on the same line of the text file
This is the code I have, it prints the correct average but it prints the test scores of the next ID numeber
int searchID;
int studentID;
bool found = false;
double exam_1;
double exam_2;
double exam_3;
double average = 0;
cout << "Enter student ID to search in file " <<
fileName << " : ";
//read searchID value
cin >> searchID;
//read data from file until the search id is found
while (fin >> studentID >> exam_1 >> exam_2
>> exam_3 && !found)
{
if (searchID == studentID)
{
average = (exam_1 + exam_2 + exam_3) / 3.0;
found = true;
}
}
//close the file stream,fin
fin.close();
//check if search id is found
if (found)
{
cout << left << setw(10) << "Exam 1" << setw(10) << exam_1 << endl;
cout << left << setw(10) << "Exam 2" << setw(10) << exam_2 << endl;
cout << left << setw(10) << "Exam 3" << setw(10) << exam_3 << endl;
cout << fixed << setprecision(2)
<< "Average : " << average << endl;
}
else
cout << searchID << " student id is not found." << endl;
system("pause");