0

//Update, I ended up figuring it out. If you're curious, I described it in the comments, thanks again everyone!

I am making a program to do several things for a Registrar's office, such as register students, delete students, etc. In the code provided, I am essentially taking in a vector that holds the names of the courses, looking through it to match it with a student id, and then appending certain variables together, putting them into a vector, then printing it out!

The problem: This code compiles 100% fine with no problems, and prints the student ID, and the courses they're registered to.

However, when I try and run this multiple times again, it will start doubling what it prints. So it seems to me that it has something to do with the vector still holding its values after the function is done. So then I decided to try and use the .erase and .clear function vector members, even making a loop to delete the variables, and it will NOT delete what is inside the container.

Does anyone have any ideas?

Here is an example of what happens the first time, then the second in the console:

1st time:
"Report Card for student: 10090"
"__________________________    "
"CS101  70                     "
"CS102  65                     "
"CS212  85                     "
"--------------------------    "

2nd time:
"Report Card for student: 10090"
"__________________________    "
"CS101  70                     "
"CS102  65                     "
"CS212  85                     "
"CS101  70                     "
"CS102  65                     "
"CS212  85                     "
"--------------------------    "

Any help is VERY much appreciated! Thank you.

void printReport(vector<string>& vect){
    string studentID;
    string line;
    string mark;
    string temp;
    string temp1;
    string tempholder;
    vector<string> reportCardVect;
    ifstream classFile;

    cout << "Please enter the ID of the student who's grade report you would like to print: " << endl;
    cin >> studentID;


    for (int i = 0; i < vect.size(); i++){
        tempholder = vect[i];
        classFile.open(tempholder.append(".txt"));

        while (!classFile.eof()){
            getline(classFile, line);

            temp = line;

            line.erase(line.begin() + 5, line.end());

            if (studentID == line){
                temp.erase(temp.begin(), temp.begin() + 6);
                temp1 = vect[i];
                temp1.append("  ");
                temp1.append(temp);
                reportCardVect.push_back(temp1);
                break;
            }
        }
        classFile.close();
    }
    system("cls");

    cout << "Report Card for student: " << studentID << endl;
    cout << "_________________________" << endl;
    for (int i = 0; i < reportCardVect.size(); i++)
        cout << reportCardVect[i] << endl;
    cout << "-------------------------" << endl;
    cout << endl;


    for (int i = 0; i < reportCardVect.size(); i++){
        reportCardVect[i].erase();
    }

    system("pause");
}
Yakubiw
  • 1
  • 2
  • Your code is printing all the contents out before anything is erased. Also you probably don't want to be making any mutable changes to anything you pass in to a print function, so you really want to pass in `vect` by const reference. – shuttle87 Nov 28 '14 at 00:20
  • You're not erasing elements from the vector, you're erasing the first char from every string in it. – Barry Nov 28 '14 at 00:35
  • The program works fine in my computer. Actually, it is not necessary to erase the vector at the end of printReport(), since the vector is reclaimed automatically. – Min Fu Nov 28 '14 at 01:14
  • I ended up figuring out the problem; in addition to this function, I ran another function before it to populate the vector that I put into this function, I just moved where I was executing the other function and it worked, thanks for your comments everyone! – Yakubiw Nov 28 '14 at 01:23
  • Could you check the input vect? – Min Fu Nov 28 '14 at 02:07
  • `while (!classFile.eof())` as you're using it is not correct. [**read this**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig Nov 28 '14 at 02:22

0 Answers0