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