I need to get input without having to press enter.
Something like:
cout<<"Enter a Number"<<endl;
--Something goes here--
switch(number){
case 1:
cout<<"This is number 1"<<endl;
break;
case 2:
cout<<"This is number 2"<<endl;
break;
}
Thanks
I need to get input without having to press enter.
Something like:
cout<<"Enter a Number"<<endl;
--Something goes here--
switch(number){
case 1:
cout<<"This is number 1"<<endl;
break;
case 2:
cout<<"This is number 2"<<endl;
break;
}
Thanks
--Something goes here--
number = getch();
--Or if you want the number echoed as it's typed--
number = getche();
--To use, include <conio.h>
Actually, to use the switch() statement:
Code:cout<<"Enter a Number"<<endl; //--Something goes here-- int number; number = getch(); switch(number){ case '1': cout<<"This is number 1"<<endl; break; case '2': cout<<"This is number 2"<<endl; break; }
if you had a picture on the screen that you wanted the user to move around, you could use something like that to do it. like put it in a loop. then just change the position and re-draw acording to what they put in. something like that, eh? for msvc++ 6 do you still have to use the _getch(); ? or _getche(); ?
If you continue to mix old C style I/O and C++'s stream I/O classes you will eventually run into problems which will be difficult to find. Use one, or the other, not both.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
So Adrian, are you saying do this?
Code:#include <stdio.h> #include <conio.h> int main(void) { int number; printf("Enter a Number\n"); number = getche(); switch(number){ case '1': printf("This is number 1\n"); break; case '2': printf("This is number 2\n"); break; } return 0; }
Last edited by swoopy; 11-29-2001 at 02:51 PM.
I keep getting very strange results when I mix getch() within couts. It keeps cutting off the previous couts. Does anyone know why?
Using cout wil place output in a buffer that is normally flushed automatically when cin is used to obtain input. getch() isn't buffered and cout's buffer will not be flushed when it's used to get input. If you're using both you should use flush or endl before using getch(). -
cout << some_output << flush;
ch=getch();
zen