Cin.Getline() Is Skipping an Input in C++

cin and getline skipping input

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++

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.

While loop skips cin.getline() for C-string

This happens, because the failbit is set, see std::basic_istream::getline

Behaves as UnformattedInputFunction. After constructing and checking the sentry object, extracts characters from *this and stores them in successive locations of the array whose first element is pointed to by s, until any of the following occurs (tested in the order shown):

  • end of file condition occurs in the input sequence (in which case setstate(eofbit) is executed)
  • the next available character c is the delimiter, as determined by Traits::eq(c, delim). The delimiter is extracted (unlike basic_istream::get()) and counted towards gcount(), but is not stored.
  • count-1 characters have been extracted (in which case setstate(failbit) is executed).

This means, when more than N characters are entered by the user, the delimiter is not found and N+1-1 characters have been extracted.

How can I stop cin from skippping a line?

It seems the problem is related to entering a boolean value.

Here is shown how to enter boolean values

#include <iostream>
#include <iomanip>

int main()
{
bool is_student;

std::cin >> is_student; // accepts 1 as true or 0 as false
std::cout << is_student << '\n';

std::cin >> std::boolalpha >> is_student; // accepts strings false or true
std::cout << is_student << '\n';
}


Related Topics



Leave a reply



Submit