0

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Iowa15
  • 3,027
  • 6
  • 28
  • 35

5 Answers5

5

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Well, thank you for all your input. You have all been extremely helpful and educational. I think I have enough methods here to say my question is answered. I will try these out in the morning. – Iowa15 Aug 14 '11 at 05:44
  • It reads one line, and takes the first `char` from it. That's all. If the line is empty, `char1` will be left untouched, so you need to check `is.good()`. If the line only has spaces, `char1` will also be left untouched and `is.good()` will be false. – Mat Aug 14 '11 at 05:44
  • Sounds like what I need. Thanks, I'll mark it as the answer for now. – Iowa15 Aug 14 '11 at 05:47
4

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

ahoo
  • 1,321
  • 2
  • 17
  • 37
2

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'

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • But this doesn't flush the input buffer. It just grabs the first character the the code after this is still subject to my problem. – Iowa15 Aug 14 '11 at 05:34
  • Are you trying to capture the first character when you type it? If so, there's a C-way to do it using getchar. – Foo Bah Aug 14 '11 at 05:36
  • What I am trying to do is get input from the keyboard in a way that if I type: 232 253 and then press enter, the 253 isn't kept in the input buffer. Because what is happening is that a space like that is causing the second half of text to be stored to the next variable I "cin". Note: there is a space between 232 and 253 above. – Iowa15 Aug 14 '11 at 05:39
  • Well, thank you for all your input. You have all been extremely helpful and educational. I think I have enough methods here to say my question is answered. I will try these out in the morning. – Iowa15 Aug 14 '11 at 05:45
1
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

steo
  • 4,586
  • 2
  • 33
  • 64
Corey
  • 11
  • 1
1

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?.

Community
  • 1
  • 1
Gnawme
  • 2,321
  • 1
  • 15
  • 21
  • Well, thank you for all your input. You have all been extremely helpful and educational. I think I have enough methods here to say my question is answered and I will try these out in the morning. – Iowa15 Aug 14 '11 at 05:46