Getline (cin, aString) accepting input without other input

My code looks like this:

string aString; cin >> aString; cout << "This is what cin gets:" << aString << endl; getline(cin, aString); cout << "This is what getline(cin, <string>) gets:" << aString << endl; 

Every time I ran it, I entered data like "12", I get "12" and "".

I am wondering why getline will be received without user input.

I can understand when I entered something like "12 24", cin will get "12", and getline should get everything else. (Also, if one could answer, the gap between them is considered the end for cin, so why is it passed to getline?)

We just start with a line in C ++, so please don't make it too complicated. Thank you.

+6
source share
3 answers

When you mix standard stream extraction using getline, you sometimes get getline returning an empty string. The reason for this is that if you read the input with →, the newline entered by the user to signal that they are done is not removed from the input stream. Therefore, when you call getline, the function will read the remaining newline and return an empty string.

To fix this, either use getline sequentially for input, or use the ws stream manipulator to extract an extra space after reading:

 cin >> value >> ws; 

This will result in a new line, fixing the problem.

Hope this helps!

+12
source

here is what i get:

 std::string str; std::cin >> str; //hello world std::cout << str; //hello 

this is because the flow operator is tokenizing in white space

 std::string str; std::getline(std::cin, str); //hello world std::cout << str; //hello world 

you get the full string, getline () works until it finds the first end of the string and returns this value as a string.

However, if there are characters in the token (for example, '\ n') remaining in the stream, then they will be available when calling getline, you will need to clear the stream.

 std::cin.ignore(); 
+7
source

"cin → x" does not consume a newline character from the input stream, so the next line you extract with getline will contain an empty line. One way to solve this problem is to use getline to extract input line by line and use stringstream to tokenize each line. After you have extracted all the data from the string stream, the key is calling stringstream :: clear () to clear the EOF flag set in the string stream so that you can reuse it later in the code.

Here is an example:

 #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string line; stringstream ss; getline(cin, line); ss << line; int x, y, z; //extract x, y, z and any overflow characters ss >> x >> y >> z >> line; ss.clear(); //reuse ss after clearing the eof flag getline(cin, line); ss << line; //extract new fields, then clear, then reuse //... return 0; } 

Depending on the length of each line of input, getting the entire line at a time and processing it in memory is probably more efficient than doing console I / O on each marker that you want to extract from standard input.

+1
source

Source: https://habr.com/ru/post/909212/


All Articles