View Single Post
Old 03-10-2005, 02:52 PM   #1 (permalink)
TheFrogel
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.
TheFrogel is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76