-1

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");
Cant_Focus
  • 17
  • 2
  • 1
    Unrelated: [Don't use `system("pause")`](https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) – ChrisMM Dec 04 '19 at 18:41
  • 1
    What is `fileName`? What is `fin`? Please try to create a [mcve] to show us, preferably one which shows all variables and how they are initialized and if possible just for us to copy-paste to be able to replicate the problem you have. – Some programmer dude Dec 04 '19 at 18:41
  • 1
    A hint about your problem though: Learn about [*short-circuit evaluation*](https://en.wikipedia.org/wiki/Short-circuit_evaluation), and think about how the order you do things in the loop condition might be affected by it. – Some programmer dude Dec 04 '19 at 18:42
  • 2
    `while (fin >> studentID >> exam_1 >> exam_2 >> exam_3 && !found)` -- Read that line out loud. What does it tell you? Read a new line, and if you found something previously, don't loop. Is that what you want, to read a new line in, destroying the previous results? Shouldn't you check if you found something first,, and if not found, then read the new line? It all boils down to simple logic -- it's really nothing to have a struggle with. – PaulMcKenzie Dec 04 '19 at 18:43
  • @PaulMcKenzie You are correct, sorry I didnt notice, and yes I was Struggling which is perfectly okay – Cant_Focus Dec 04 '19 at 23:42

1 Answers1

0

Your got the loop condition wrong. Lets say one iteration found the ID, found is set to true, then for the next iteration the condition is evaluated:

while (fin >> studentID >> exam_1 >> exam_2 >> exam_3 && !found)

No matter what is the value of found you read the next entry in the file. Change it to

while (!found && fin >> studentID >> exam_1 >> exam_2 >> exam_3)

&& short-circuits, ie when !found is false, the rest of the condition is not evaluated.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185