Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [C++] getch()/cin issues (https://thetfp.com/tfp/tilted-technology/85125-c-getch-cin-issues.html)

TheFrogel 03-10-2005 02:52 PM

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

vinaur 03-10-2005 04:18 PM

So after you type b the b appears on line 2 and on line 3 where c is now??

Memalvada 03-10-2005 05:20 PM

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.

TheFrogel 03-10-2005 06:16 PM

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.

TheFrogel 03-11-2005 11:20 AM

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?

Rangsk 03-11-2005 04:49 PM

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 :)

TheFrogel 03-11-2005 07:16 PM

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?


All times are GMT -8. The time now is 10:26 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


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