How to recover from failing cin.get(str,n,'\n')?

  • Thread starter Alexander Malkis
  • Start date
A

Alexander Malkis

How to recover from failing cin.get(str,n,'\n')?
The user pressed "Enter" on cin.get(...).I would like to let him try
once more but the system doesn't stop on the next cin.get. Code:

cin.get(str,n,'\n');
cout<<"haha\n";
char c;
/*
Now get '\n'. Doesn't work if the first time nothing else was input.
Have this line or leave it -it doesn't change the outcome in this case.
*/
cin.get(c);
cin.get(str,n,'\n'); //here doesn't wait if the first time Enter was
pressed.
 
M

Mike Wahler

Alexander Malkis said:
How to recover from failing cin.get(str,n,'\n')?
The user pressed "Enter" on cin.get(...).I would like to let him try
once more but the system doesn't stop on the next cin.get. Code:

cin.get(str,n,'\n');
cout<<"haha\n";
char c;
/*
Now get '\n'. Doesn't work if the first time nothing else was input.
Have this line or leave it -it doesn't change the outcome in this case.
*/
cin.get(c);
cin.get(str,n,'\n'); //here doesn't wait if the first time Enter was
pressed.

if std::istream::get(T*, std::streamsize, T) does
not extract any elements, it sets std::istream::failbit.
(This function does not extract the delimiter T).
No further i/o will be performed until the stream state
is reset.

#include <ios>
#include <iostream>
#include <limits>


int main()
{
char str[100] = {0};
std::streamsize n(sizeof str);

std::cin.get(str, n, '\n');
std::cout << "one\n";

std::cin.clear();

std::cin.ignore
(std::numeric_limits<std::streamsize>::max(), '\n');

std::cin.get(str, n, '\n');
std::cout << "two\n";


return 0;
}

Rather than a simple 'std::cin.get()' to remove the newline,
I've used 'std::istream::ignore' instead, which will remove
any pending characters, the number of which might have exceeded
the value of the type 'streamsize' argument.

You could avoid all these machinations by using the
'std::getline()' nonmember function declared by <string>,
which stores the input in a 'std::string' object, and
does extract (but does not store) the newline character.

#include <iostream>
#include <string>

int main()
{
std::string line;
std::getline(std::cin, line);
std::getline(std::cin, line);
return 0;
}

-Mike
 
M

Mike Wahler

Mike Wahler said:
You could avoid all these machinations by using the
'std::getline()' nonmember function declared by <string>,
which stores the input in a 'std::string' object, and
does extract (but does not store) the newline character.

Should be:

does extract (but does not store) the delimiter
(a newline by default) character.

-Mike
 
A

Alexander Malkis

Actually, I used
cin.getline(char_type* __s, streamsize __n, char_type __delim);
somewhere from std_istream.h
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top