简体   繁体   中英

How can I clear the input buffer in a c++ application?

I make a lot of simple console c++ applications and one problem I am facing is the input buffer. I have tried cin.ignore and flush(), but they don't seem to be working for me.

If I have code such as:

cin >> char1;
cin >> char2; 

and I press: 1 (space) 2 (Enter), with just one enter, the 1 is stored to char1 and the 2 is stored to char2.

Sorry if I am a little fuzzy on what I am asking. I will try to edit this question if people don't understand.

You could use getline to read the whole line at once, then std::string at if you need the first char, or use an isstringstream if you need the first number.

char char1;
std::string input;

getline(std::cin, input);
if (!std::cin.good()) {
  // could not read a line from stdin, handle this condition
}

std::istringstream is(input);
is >> char1;
if (!is.good()) {
  // input was empty or started with whitespace, handle that
}

Wrap that in a function if you do it often. With the above, if you hit enter directly (no characters entered), or if you enter data starting with whitespace, is will be !good() so char1 will not have been set.

Alternatively, after you've checked that cin is still good, you could simply:

if (input.empty()) {
  // empty line entered, deal with this
}
char1 = input.at(0);

With that, if the string is non-empty, char1 will be set to the first char if input , whatever that is (including whitespace).

Note that:

is >> char1;

will only read the first char, not the first number (same with the input.at() version). So if the input is 123 qze , char1 will receive '1' (0x31 if ASCII), not the value 123. Not sure if that is what you want or not. If it's not what you want, read to an int variable, and cast properly afterwards.

look this:

int a,b;
cin>>a;
cin.ignore(INT_MAX,'\n');
cin>>b;

this would read in and ignore everything until '\n' to ignore a single line

If you just want the first character from the line, read the entire line and then split it up:

std::string s;
std::cin >> s;
char1 = s[0]; // this reads the lead character

If you want the first non-whitespace character, use an istringstream and grab a character:

#include <sstream>
std::istringstream iss(s);
iss >> char1;

The "C" way of doing it on linux requires termios and looks something like this:

#include <termios.h>

struct termios t;
tcgetattr(0, &t); 
t.c_lflag &= ~ICANON; // unbuffered
tcsetattr(0, TCSANOW, &t); // make the change now
char1=getchar()
char2=getchar()

now if you try typing "12" then char1 = '1' and char2='2'

cin >> char1;

cin >> char2;

fflush(stdin);

if user enters: aaaaa bbbbb ccccc

it will input aaaaa into char1

and put bbbbb into char2

the rest will be ignored

If you're working with cstdio, then:

rewind( stdin );

should do the job. Otherwise, I think your question was answered in How do I flush the cin buffer? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM