Hi!
I would like to know if there is any way in C++ to scan the keyboard without having to press the Enter key?
c=cin.get; // doesn't work
c=cin.get(); // waits for the Enter key
Thanks
Johan
Hi!
I would like to know if there is any way in C++ to scan the keyboard without having to press the Enter key?
c=cin.get; // doesn't work
c=cin.get(); // waits for the Enter key
Thanks
Johan
cin.getch( ) doesnt wait for the enter key. Or so I've been told...
why not use getch()?
getch() halts the program until a key is pressed.
kbhit() is excellent but it doesn't tell you WHICH key was pressed and I haven't figured out how to reset kbhit() from 1 (which was the value when it was pressed) back to 0.
I really need an answer to this so if there is anyone who can help me I would be thankful.
/Johan
Just use it like this (I'm at school right now so I have not compiled this but I think it will work fine)
I hope this helps you guys out a bit ^_^
Sorry if there are any errors I don't have a compiler to test on at school right now. ^_^Code:#include <stdlib.h> #include <stdio.h> #include <dos.h> //Not really sure if you //Need to include dos.h int main() { printf("Looping press q to quit.\n"); //Message. bool running=true; //Make running true int n; //make counter var. char k; //if the prog does not //run make this int. while(running) //Loop while running. { if(!kbhit()) //Wait for user to hit kb. { k=getch(); //Get user input. switch(k) //Look at input. { case "q": //User wants to quit. running=false; //Quit. break; //Done with switch. } } n++; //add to counter. printf("Looped %d times\n",n); //Show counter. } return 0; }
Tazar The Demon
-------------------------------------
Please check my site out at: http://www.lrs.8m.com thanks =)
-------------------------------------
Note:
I will help newbies and any other person that I can just ask I'll try to help you out =)
My MSN Messager name is: [email protected] see if I'm online maybe we can talk. =)
few problems I think I've fixed:
Edit - works nowCode:#include <stdlib.h> #include <stdio.h> #include <conio.h> #define TRUE 1 #define FALSE 0 int main() { int running=TRUE; //Make running true int n = 0; //make counter var. char k; //if the prog does not printf("Looping, press q to quit.\n"); //Message. while(running) //Loop while running. { if(kbhit()) //Wait for user to hit kb. { k=getch(); //Get user input. switch(k) //Look at input. { case 'q': //User wants to quit. running=FALSE; //Quit. break; } //Done with switch. } n++; //add to counter. printf("Looped %d times\n",n); //Show counter. } return 0; }
Last edited by Brian; 02-27-2002 at 12:29 PM.
I have a question now...
By combining kbhit( ) and getch( ) while switching what the user entered, how would you catch the UP or LEFT arrow being hit? I did a test program to see what # and character they outputted and each arrow key (UP, LEFT, RIGHT, & DOWN) outputted 224 and the character for the greek alpha. Then it locks up the program.
Here is the code I used to test it out (pretty much same as above):
Any ideas?Code:#include <iostream> #include <cstdlib> #include <conio.h> using namespace std; void goodbye(void) { cout << "Goodbye!\n"; } int main() { int running = 1; int c; while( running ) { if( !kbhit() ) // If the keyboard hasn't been hit.. { c = getch(); // Snatch the character from user cout << (int)c << " " << (char)c << endl; if( c == 'q' ) running = 0; } } atexit(goodbye); return 0; }
Thanks
The problem is that the arrow keys and all F keys are extended and return 0 then the number for the key
I changed your code to work and it works fine now
Hope it helps ^_^Code:#include <iostream.h> #include <stdlib.h> #include <conio.h> void goodbye(void) { cout << "Goodbye!\n"; } int main() { int running = 1; int c; while( running ) { if( !kbhit() ) // If the keyboard hasn't been hit.. { c = getch(); // Snatch the character from user if(c==0)c = getch(); // Snatch the character from user cout << (int)c << " " << (char)c << endl; if( c == 'q' ) running = 0; } } atexit(goodbye); return 0; }
Tazar The Demon
-------------------------------------
Please check my site out at: http://www.lrs.8m.com thanks =)
-------------------------------------
Note:
I will help newbies and any other person that I can just ask I'll try to help you out =)
My MSN Messager name is: [email protected] see if I'm online maybe we can talk. =)