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;
Each time I ran it, I give inputs like, "12", I get "12" and "".
I am wondering why getline would received without user input.
I can understand when I enter something like "12 24", cin will get "12", and getline should get the rest. (Also, if one could answer, the space in between is treated as an end for cin, so why is it passed on to getline?)
Just starting out on string on C++ so please don't make it too hard. Thanks you.
ignore() function does the trick. By default, it discards all the input suquences till new line character. Other dilimiters and char limit can be specified as well.
C++ getline() The cin is an object which is used to take input from the user but does not allow to take the input in multiple lines. To accept the multiple lines, we use the getline() function. It is a pre-defined function defined in a <string.
getline (string) in C++ The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.
The cin. ignore() function is used which is used to ignore or clear one or more characters from the input buffer.
When you mix standard stream extraction with getline, you will sometimes have getline return the empty string. The reason for this is that if you read input with >>, the newline character entered by the user to signal that they're done is not removed from the input stream. Consequently, when you call getline, the function will read the leftover newline character and hand back the empty string.
To fix this, either consistently use getline for your input, or use the ws stream manipulator to extract extra white space after a read:
cin >> value >> ws;
This will eat up the newline, fixing the problem.
Hope this helps!
this is what I get:
std::string str;
std::cin >> str; //hello world
std::cout << str; //hello
this is because the stream operator tokenizes on white space
std::string str;
std::getline(std::cin, str); //hello world
std::cout << str; //hello world
you get the full line, getline() works until it find the first end of line and returns that value as a string.
However when tokenizing, if there are characters (for example a '\n') left in the stream then these will be accessed when getline is called, you will need to clear the stream.
std::cin.ignore();
"cin >> x" doesn't consume the newline character from the input stream, so the next line you retrieve with getline will contain an empty string. One way to solve this is to use getline to retrieve input line by line and use a stringstream to tokenize each line. After you've extracted all input from the stringstream, the key thing is to call stringstream::clear() to clear the EOF flag set on the stringstream to be able to reuse it later in the code.
Here's 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 input line, getting the whole line at a time and processing it in-memory is probably more efficient than doing console IO on every token you want to extract from the standard input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With