checking for eof in string::getline

The canonical reading loop in C++ is: while (getline(cin, str)) { } if (cin.bad()) { // IO error } else if (!cin.eof()) { // format error (not possible with getline but possible with operator>>) } else { // format error (not possible with getline but possible with operator>>) // or end of file (can’t make … Read more

c++ Read from .csv file

You can follow this answer to see many different ways to process CSV in C++. In your case, the last call to getline is actually putting the last field of the first line and then all of the remaining lines into the variable genero. This is because there is no space delimiter found up until … Read more

Using getline() in C++

If you’re using getline() after cin >> something, you need to flush the newline character out of the buffer in between. You can do it by using cin.ignore(). It would be something like this: string messageVar; cout << “Type your message: “; cin.ignore(); getline(cin, messageVar); This happens because the >> operator leaves a newline \n … Read more

tech