character input woes
this should be a very easy thing to do but i''ve been stumped by this for a while now.
I''m going to end up Breaking something (ever seen the bad day mpeg going about on the internet if i can''t figure it out!
ok i''m making a small prog using the windows console.
I want the user to enter a y/n in response to a prompt so i can then carry out an action based on their response.
char action ;
cout << "continue (y/n) : "
cin >> action ;
_toupper (action) ;
switch (action)
{
case ''Y'' : cout << "enter filename" ;
cin >> filename ;
...
...
...
...
}
so whenever i enter more than one character at the prompt
there still seems to be characters in the buffer when i get to the line :
cin >> filename ;
therefore filename is populated with the leftover characters from the last input operation.
How can i prevent this happening?
someone please help or my monitor will likely end up out the window.
Try using getc(), it only grabs one char.
OneEyeLessThanNone
That''s right, type that out every time.
OneEyeLessThanNone
That''s right, type that out every time.
Thanks but surely that won''t solve my problem because there will still be characters in the buffer after just reading one?
i used getchar and the same thing happened
i used getchar and the same thing happened
June 03, 2000 07:38 PM
Looks like you forgot to flush.
You can flush the input buffer in C++ by doing a cin.flush(). The same thing goes for cout or any other iostream object that you create. Flushing the input buffer clears any data that is currently in the buffer. Flushing the output buffer will force the output to be written to whatever it needs to get written to. This is very useful if you want to be certain that data gets written to disk (say for a debug log) even if your program terminates abnormally.
To flush the buffer using standard C calls you can do an fflush([FILE]) call.
Hope this gets you where you need to go.
-Graeme
You can flush the input buffer in C++ by doing a cin.flush(). The same thing goes for cout or any other iostream object that you create. Flushing the input buffer clears any data that is currently in the buffer. Flushing the output buffer will force the output to be written to whatever it needs to get written to. This is very useful if you want to be certain that data gets written to disk (say for a debug log) even if your program terminates abnormally.
To flush the buffer using standard C calls you can do an fflush([FILE]) call.
Hope this gets you where you need to go.
-Graeme
Hi, thanks i''ve tried using cin.flush() but i get this error message
error C2039: ''flush'' : is not a member of ''istream_withassign
arghhhh i''m going to go mental if i can''t get this sorted.
error C2039: ''flush'' : is not a member of ''istream_withassign
arghhhh i''m going to go mental if i can''t get this sorted.
June 03, 2000 08:10 PM
Doh, you''re right. It''s been a while since I''ve used iostream classes extensively. flush() is only defined for ostream (output) classes. You might try sync(); although I can''t recall using it myself. Another way would be for you to not use the extraction operator and just do a cin.getline(...) which takes a pointer to the buffer as its first argument and a number of max characters to get as its second. Its third argument is the delimiter which is defaulted to ''\n''. Then just get the whole line into your buffer and parse buffer[0] for the ''y'' or ''n''. You could also use the same buffer for the file name if you make it big enough. This would ensure that you don''t try to open a file with a newline at the end of it.
Anyway, hopefully my information is correct this time
-Graeme
Anyway, hopefully my information is correct this time
-Graeme
June 03, 2000 09:06 PM
Just as a side question, can you use cin for input in a windows/directx environment?
Thanks
Thanks
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement