03-10-2005, 02:52 PM | #1 (permalink) |
Tilted
Location: Pennsylvania
|
[C++] getch()/cin issues
Program illustrating the problem:
#include<iostream.h> #include<stdio.h> int main() { char temp1; temp1=getch(); cout<<"Line 1, temp1="<<temp1<<endl; temp1=getch(); cout<<"Line 2, temp1="<<temp1<<endl; cin>>temp1; cout<<"Line 3, temp1="<<temp1<<endl; return(0); } Output (parts in brackets are notes I'm adding now): [waits for keystroke][I type 'a'] Line 1, temp1=a [so far, so good, program waits for keystroke, I type 'b'] Line 2, temp1=b b [Here's the problem. The 'b' is just SITTING THERE. It shouldn't be displayed to screen, but it's there as if I had typed after the cin prompt. I can hit backspace and change it, but why is it displaying to the screen?][So I backspace and hit 'c',<enter>, here's the full output:] Line 1, temp1=a Line 2, temp1=b c Line 3, temp1=c Now, my problem is that after I use a getch() command, the next cin>> prompt will display the letter from the last getch(). How can I prevent that or work around it, other than completely eliminating cin>> statements from my code? I am using Microsoft Visual C++ version 6.0, from '98. Yes, it's old, but it still works. |
03-10-2005, 05:20 PM | #3 (permalink) |
Psycho
Location: South of the border
|
I think you had to use an ignore statement, but I forgot how to use it...
I think it went something like: cin.ignore(100, '\n'); try placing that before or after the cin statement(i dont quite remember). I may be wrong, but give it a shot.
__________________
"The weak are food for the strong, so die and let me feast!" - Makoto Shishio (RK) |
03-10-2005, 06:16 PM | #4 (permalink) |
Tilted
Location: Pennsylvania
|
cin.ignore(100, '\n');
What's the 100 mean? And are you telling it to ignore newline inputs, or what does the second argument mean? I'll do a bit of research, but it's always good hearing from someone who knows it. Clarification: After I hit the letter 'b', the Line 2 statement appears, ("Line 2, temp1=b") AND 'b' shows up on the following line at the same time. What I want to happen is for the 'b' to NOT show up on the following line. It won't show as long as I use getch() statements, but as soon as a cin>> appears, the last thing I type shows up. And yes, when I type 'b' it shows up where I have 'c' in its own line in the second section of output. EDIT: Found stuff about cin.ignore(): std::cin.ignore() can be called three different ways: No arguments: A single character is taken from the input buffer and discarded: std::cin.ignore(); //discard 1 character One argument: The number of characters specified are taken from the input buffer and discarded: std::cin.ignore(33); //discard 33 characters Two arguments: discard the number of characters specified, or discard characters up to and including the specified delimiter (whichever comes first): std::cin.ignore(26, '\n'); //ignore 26 characters or to a newline, whichever comes first off of this page , very useful. I'll try this and see what happens...but I'm going to sleep now. Last edited by TheFrogel; 03-10-2005 at 06:25 PM.. |
03-11-2005, 11:20 AM | #5 (permalink) |
Tilted
Location: Pennsylvania
|
Nope, doesn't work. cin.ignore() only ignores input entered, it won't prevent output from displaying to the screen. I'm thinking that it's an issue between conio.h and iostream.h, that c++ doesn't like me playing with both at the same time (getch() is a conio.h function, while cin and cout are iostream.h functions)
But does anyone know a workaround or a function OTHER than getch() that will just get a keystroke from the keyboard WITHOUT it displaying to the screen? |
03-11-2005, 04:49 PM | #6 (permalink) |
Crazy
Location: San Diego, CA
|
The problem is that you're combining C and C++ so the result are going to be irregular. Either use the C version of cin (scanf) or the C++ version of getch (cin.getchar() I believe, haven't looked it up).
Good luck
__________________
"Don't believe everything you read on the internet. Except this. Well, including this, I suppose." -- Douglas Adams |
03-11-2005, 07:16 PM | #7 (permalink) |
Tilted
Location: Pennsylvania
|
cin.getchar() does not exist, at least in what I'm using. cin.get() does, and I tried messing around with that, and it works fine, except that you need to hit 'enter' at the end, which kills the point for me. I want it to be able to accept the letter immediately, without having to hit enter. I'm not sure what cin (scanf) is, cin.scanf (or any scan) isn't recognized by the compiler I'm using. Thanks for the info on cin.get(), I'm gonna keep messing with that and see what happens.
EDIT: You know what? If anyone knows how to simulate the keystroke 'backspace' or the left arrow key, it would work just as well. Please? Last edited by TheFrogel; 03-11-2005 at 08:14 PM.. |
Tags |
getch or cin, issues |
|
|