0

Greetings so I'm making a program for my CS1 class, and in this program, for some odd reason it'll randomly repeatedly print some values.

For example, I tried 2 students for the printReportCard function and it printed the first one twice and the second one once. Then I tried 3 students, and it printed each one twice in total printing 6 report cards. I have no idea why it's doing this, I've even tried printing vector size and it prints the correct number but for some reason iterates more than once over some report cards. I then checked the text file before it formats it and it had the proper amount of reports but only during that std::for_each is where it goes wrong oddly enough.

code:

void printReportCard(int numStudents)
{
    fstream reportCards;
    reportCards.open("reportCard.txt");

    string current_card = "2022 First Semester Report Card\n\n";

    string name, currentClass, currentGrade;
    for (int student = 1; student <= numStudents; student++)
    {
        getline(reportCards, name);
        current_card += "Student Name: " + name + "\n";
        current_card += "----------------------------------------------\nCourse:\t\t\tGrade:\n----------------------------------------------\n";

        for (int i = 0; i < 7; i++)
        {
            reportCards >> currentClass >> currentGrade;
            current_card += currentClass + getTabs(currentClass) + currentGrade + "\n";
        }

        string gpa, honorRoll;
        reportCards >> gpa >> honorRoll;

        if (honorRoll == "!")
        {
            honorRoll = "You are not on the Honor Roll.";
        }
        else
        {
            honorRoll = "You are on the " + honorRoll + " Honor Roll.";
        }

        current_card += "\nSemester GPA: " + gpa;
        current_card += "\n\nHonor Roll: " + honorRoll + "\n\n";

        reportCardsVector.push_back(current_card);
        system("CLS");
    }

    reportCards.close();

    system("PAUSE");
    system("CLS");

    reportCards.open("reportCard.txt", ios::trunc | ios::out);
    for_each(reportCardsVector.begin(), reportCardsVector.end(), [ & ](const string VALUE) {
        reportCards << VALUE << "\n";
        cout << VALUE << endl;
    });
    reportCards.close();
}

text file before format:

Student One
classOne f
classTwo f
classThree f
classFour f
classFive f
classSix f
classSeven f
0
!
Student Two
classOne a
classTwo a
classThree a
classFour a
classFive a
classSix a
classSeven a
High
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Kuno
  • 15
  • 3
  • You need to reset `current_card` at the beginning of each loop iteration, otherwise it just keeps getting longer and longer. The most obvious way would be to move `string current_card = "2022 First Semester Report Card\n\n";` inside the loop at the beginning. You should read [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) as well for when you run into that. – Retired Ninja Apr 21 '22 at 13:21
  • you read a single line then from that one line you try to extract 7 records. It is not clear how this is supposed to work when the format of the file is as shown. Anyhow, please try to create a [mcve] – 463035818_is_not_an_ai Apr 21 '22 at 13:21
  • is the contents of the file perhaps only 2 lines? with all information for one student in a single line? – 463035818_is_not_an_ai Apr 21 '22 at 13:26

1 Answers1

1

"You need to reset current_card at the beginning of each loop iteration, otherwise it just keeps getting longer and longer. The most obvious way would be to move string current_card = "2022 First Semester Report Card\n\n"; inside the loop at the beginning. You should read Why does std::getline() skip input after a formatted extraction? as well for when you run into that." - Retired Ninja

Thank you, Retired! This worked, and also thanks for the heads up I fixed that by adding reportCards >> std::ws inside the call to std::getline.

Kuno
  • 15
  • 3