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

getline not asking for input? [duplicate]

You have to be careful when mixing operator>> with getline. The problem is, when you use operator>>, the user enters their data, then presses the enter key, which puts a newline character into the input buffer. Since operator>> is whitespace delimited, the newline character is not put into the variable, and it stays in the … Read more

std::cin.getline( ) vs. std::cin

Let’s take std::cin.getline() apart. First, there’s std::. This is the namespace in which the standard library lives. It has hundreds of types, functions and objects. std::cin is such an object. It’s the standard character input object, defined in <iostream>. It has some methods of its own, but you can also use it with many free … Read more

When and why do I need to use cin.ignore() in C++?

Ignore is exactly what the name implies. It doesn’t “throw away” something you don’t need instead, it ignores the amount of characters you specify when you call it, up to the char you specify as a breakpoint. It works with both input and output buffers. Essentially, for std::cin statements you use ignore before you do … Read more

Need help with getline() [duplicate]

The getline(cin, str); reads the newline that comes after the number read previously, and immediately returns with this “line”. To avoid this you can skip whitespace with std::ws before reading the name: cout << “Enter number:”; cin >> number; cout << “Enter name:”; ws(cin); getline(cin, str); … Note that this also skips leading whitespace after … Read more