0

My current code adds extra portion to the final result. I am reading a csv file and adding certain elements to variables. My end result always adds an extra element to the screen? What am i doing wrong?

Code:

int main()
{
    ifstream file("data.csv");
    //checks to see if file is valid
    if (!file.is_open())
    {
        cout << "Invalid file" << endl;
    }
    string name;
    string age;
    string college;
    while (file.good())
    {
        getline(file, name, ',');
        getline(file, age, ',');
        getline(file, college, '\n');
        cout << "Name: " << name << '\n';
        cout << "Age: " << age << endl;
        cout << "College: " << college << endl;
        cout << "------------------" << endl;
    }
    //closes the file and program terminates
    file.close();
    return 0;
}
  • 1
    `cout << "------------------" << endl;`. - You, pretty explicitly, add a newline and a stream flush there. – Jesper Juhl Jun 01 '20 at 18:12
  • 1
    In the future, post sample output from your program and an example of what you are expecting. – jwdonahue Jun 01 '20 at 18:15
  • 1
    `while (file.good()) {` is similar to [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Jun 01 '20 at 18:16
  • Unrelated: `file.close()` isn't needed. The file will be closed when `file` goes out of scope. – Ted Lyngmo Jun 01 '20 at 18:17
  • Hi You'll need to show your output so that we can be sure about where the "extra element" you don't like is, but you should be consistent with the use of `\n` and not `endl`. – Spencer Jun 01 '20 at 18:19
  • @TedLyngmo Perhaps it's not needed in the minimal sample code, but in the real program, maybe it needs to be closed this way. – Spencer Jun 01 '20 at 18:22
  • @Spencer So it might be it - but it's very common to declare and open an `fstream` at the beginning of a scope and let it be open until the end of the scope. In those cases, `close()` is never needed. – Ted Lyngmo Jun 01 '20 at 18:25
  • 1
    @Spencer -- yes, you can make up situations where `file.close()` is needed. In the code in the question, `file.close(); return 0;`, the call to `close()` is not needed. – Pete Becker Jun 01 '20 at 19:13

1 Answers1

1

file.good() will return true after you've read the last line in the file so while (file.good()) will loop one extra round, but the getlines will fail.

Replace the loop with:

while (getline(file, name, ',') &&
       getline(file, age, ',') &&
       getline(file, college, '\n'))
{
    cout << "Name: " << name << '\n';
    cout << "Age: " << age << endl;
    cout << "College: " << college << endl;
    cout << "------------------" << endl;
}

getline will return file& and an ifstream returns true or false in boolean contexts depending on if it's in a good state or not.

Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50