cin and getline skipping input [duplicate]
If you're using getline
after cin >> something
, you need to flush the newline out of the buffer in between.
My personal favourite for this if no characters past the newline are needed is cin.sync()
. However, it is implementation defined, so it might not work the same way as it does for me. For something solid, use cin.ignore()
. Or make use of std::ws
to remove leading whitespace if desirable:
int a;
cin >> a;
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
//discard characters until newline is found
//my method: cin.sync(); //discard unread characters
string s;
getline (cin, s); //newline is gone, so this executes
//other method: getline(cin >> ws, s); //remove all leading whitespace
cin.getline() is skipping an input in C++ [duplicate]
This:
cin>>n;
Is reading the number only.
It leaves the trailing '\n'
on the stream.
So your first call to getline()
is reading an empty line containing just a '\n'
It is best not to mix the use of operator>>
and std::getline()
. You have to be very careful on whether you have left the newline on the stream. I find it easiest to always read a line at a time from a user. Then parse the line separately.
std::string numnber;
std::getline(std::cin, number);
int n;
std::stringstream numberline(number);
numberline >> n;
getline() function is skipping inputs
Look at your program this way
int T;
cin >> T;
Console input: 5\n
You may have noticed the problem already. You think what you get is a 5, but its a 5 + a line break.
Console input: Name\n
Then you call getline()
cin buffer is not: Name\n,
it's actually: \nName\n
Therefore, with the first getline you are reading a single "\n"
and with the second one, you are finally reading "Name\n"
There's ways to approach this issue. One is doing this trick
while (isspace(cin.peek())) cin.ignore(); //dodge spaces, line breaks.
getline(cin, nomP);
getline(cin, nomC);
I only use windows, but maybe the line breaks could be \r\n in another OS, that's why doing a single cin.ignore() may not be enough. So the trick still works.
But there's a better way: make a function, which returns only when it has read a non empty line. Something like:
string my_getline()
{
string result;
while (!getline(cin, result) || result.empty());
return result;
}
string nomP = my_getline();
string nomC = my_getline();
With RVO this is as fast as doing getline(cin,nomP), and more simple.
Why is std::getline() skipped? [duplicate]
Duplicate question answered here.
basically, cin>>
doesn't remove new lines from the buffer when the user presses enter. getline()
mistakes this for user input along with enter.
You can use cin.ignore()
to get rid of those extra characters before using getline()
.
std::getline skipping input from std::cin after last occurrence of delimiter, but not with input from std::istringstream
My question is: why does reading from
std::cin
with a delimiter skip the input after the last occurrence of the delimiter, but reading fromstd::istringstream
does not?
It doesn't.
In your first example:
while(std::getline(std::cin, s, ' ')){
std::cout << s << std::endl;
}
You are specifically reading items from newline that are literally delimited by a single space. Because the line is (ostensibly) ended with a newline, it will never finish extracting from the input string as it is expecting either ' '
or an EOF.
In your second example:
while (std::getline(std::cin, line)) {
std::istringstream iss(line);
while (std::getline(iss, s, ' ')) {
std::cout << s << std::endl;
}
}
The std::getline
in the first while will strip the newline from your example sentence. Then items are extracted according to some basic rules.
Here are the rules (from cppreference):
Extracts characters from input and appends them to str until one of the following occurs (checked in the order listed)
a) end-of-file condition on input, in which case, getline sets eofbit.
b) the next available input character is delim, as tested by Traits::eq(c, delim), in which case the delimiter character is extracted from input, but is not appended to str.
c) str.max_size() characters have been stored, in which case getline sets failbit and returns.
Related Topics
What Happens If I Define a 0-Size Array in C/C++
Use of 'Const' For Function Parameters
C++ Convert Hex String to Signed Integer
What Is Ownership of Resources or Pointers
Why Is My Power Operator (^) Not Working
How to Enforce Move Semantics When a Vector Grows
Check If a Class Has a Member Function of a Given Signature
Multicharacter Literal in C and C++
What Is Std::Move(), and When Should It Be Used
Size of Character ('A') in C/C++
Conditions For Automatic Generation of Default/Copy/Move Ctor and Copy/Move Assignment Operator
Fast Bignum Square Computation
How Does Delete[] "Know" the Size of the Operand Array
Array Size At Run Time Without Dynamic Allocation Is Allowed
What's the Difference Between Constexpr and Const