Need help with C++
I just started learning C++ a couple months ago and I want to start making small games like pong or pac-man. But I am having some trouble with input. I am using Turbo C++ and I need to know what the C++ equivelant of readkey in PASCAL or input$ in BASIC is. Or if you have any suggestions on a better way to get input form the keyboard without it being echoed to the screen and without it waiting for the enter key to be pressed then I''m open to suggestsions.
I already know pretty much whatever there is to know about cin. But when you use cin it waits for the enter key to be pressed before it will stop getting input. It also prints what you are entering on the keyboard on the screen. I want a method that doesn''t do this.
if you're using a console for a dos or windows based pc, just use kbhit.
here's some code you can use
Edited by - Nytegard on December 6, 2001 7:52:13 PM
here's some code you can use
char kbhit1(void){#ifndef WIN32 int c; struct termios new1,save1; /* Get the current terminal settings */ if (tcgetattr(0,&save1)!=0) { fputs("Failed to init",stderr); return 2; } /* We'll use save as a copy to restore the original * settings later */ new1=save1; /* Set up terminal so that characters are not * echoed and turn off canonical mode. */ new1.c_cc[VMIN]=1; new1.c_lflag&=~ICANON; new1.c_lflag&=~ECHO; /* Send the new settings to the terminal */ tcsetattr(0,TCSANOW,&new1); while(1) { c=getchar(); if (tcsetattr(0,TCSANOW,&save1)!=0) { printf("Failed to restore terminal",stderr); } return(c); }#endif return 0;}/* polls stdin to see if there is any input availible */int kbhit(){ int choice;#ifdef WIN32 while(!_kbhit()); choice = getch(); return choice;#else choice = kbhit1(); return choice;#endif return 0;
Edited by - Nytegard on December 6, 2001 7:52:13 PM
ok man...im lost.....its the reason i stayed with basic for so long....if you ask anyone about anything in C++ ....they offer you advanced little snippits of code that really dont explain a thing...OK. the question is ...in qbasic you can capture keyboard events by using either inkey$ or input(with a $ for strings) ....simple.....now is there a way .....a easy way.....like maybe a
#include *\ replaced with a relevant one
followed by a :
{
int a; *\ dont have to waste time typing this in qbasic
input a;
printf ("%d \n,a"); *\ whats the f mean?
}
but what i really want to know is how to stop my program ending b4 ive looked at whats happing in the run window...(terminal?)preferably by using a keyboard event trapper;
(ie) a$ = inkey$
if a$ = "q" then stop
if a$ <> "q" then (return to a bizarre looking c++ loop and go again untill a$ does = "q"
im really new to c++.....and im tired of not being able to do stuff i want to do in qbasic cept wireframes....please someone help ....
ps.....is there a line / pset equivent in c++....if so....what libraries will i need to include.......i dont even know what libraries do what......sad eh.....but i gues the has to be there (y/n)?
#include *\ replaced with a relevant one
followed by a :
{
int a; *\ dont have to waste time typing this in qbasic
input a;
printf ("%d \n,a"); *\ whats the f mean?
}
but what i really want to know is how to stop my program ending b4 ive looked at whats happing in the run window...(terminal?)preferably by using a keyboard event trapper;
(ie) a$ = inkey$
if a$ = "q" then stop
if a$ <> "q" then (return to a bizarre looking c++ loop and go again untill a$ does = "q"
im really new to c++.....and im tired of not being able to do stuff i want to do in qbasic cept wireframes....please someone help ....
ps.....is there a line / pset equivent in c++....if so....what libraries will i need to include.......i dont even know what libraries do what......sad eh.....but i gues the has to be there (y/n)?
When I was learning the basics of C, instead of struggling with scanf() and getch(), I just used the C++ I/O function cin and cout. It's the most simple type of I/O in C++, IMHO. For input, you simply type:
Remember to include iostream.h. Here's an example program using cin:
There's a more efficient way to use the cout's, but for this, I tried to keep it simple. Note that everything you print in cout will be on the same line until you print \n. Use this to your advantage.
Edited by - tuxx on December 8, 2001 11:12:40 AM
cin >> var; // store input in the variable var
Remember to include iostream.h. Here's an example program using cin:
#include <iostream.h>int main(int argc, char* argv[]){ char input; // character variable to hold the input cout << "Press a key\n"; /* cout is used to print to the screen; \n means 'new line' */ cin >> input; // store the input into the variable input cout << "You pressed "; // print 'You pressed' cout << input; // print the value of input cout << "."; // print a . cout << "\n"; // new line return 0;}
There's a more efficient way to use the cout's, but for this, I tried to keep it simple. Note that everything you print in cout will be on the same line until you print \n. Use this to your advantage.
Edited by - tuxx on December 8, 2001 11:12:40 AM
[email=dumass@poppet.com]dumass@poppet.com[/email]
December 08, 2001 10:55 AM
I don''t know what compiler you use,
but there''s a non standart function to get console keyboard input
called something like getch ( be aware of getche this echos the character on the screen ).
The function is usually delared in a non standart header called
conio.h.
Both return the character read.
But be careful with functon keys,
they are handled a bit different, at least in the implementation
shipped with my compiler. Look up your help for that.
You can use the functions like this
char c;
c = getch();
If you don''t like it like this,
you can write wrap ''em in your own functions or macros
or typedef them or all together
Hope that helped you!
but there''s a non standart function to get console keyboard input
called something like getch ( be aware of getche this echos the character on the screen ).
The function is usually delared in a non standart header called
conio.h.
Both return the character read.
But be careful with functon keys,
they are handled a bit different, at least in the implementation
shipped with my compiler. Look up your help for that.
You can use the functions like this
char c;
c = getch();
If you don''t like it like this,
you can write wrap ''em in your own functions or macros
or typedef them or all together
Hope that helped you!
quote: Original post by bosscheese
I already know pretty much whatever there is to know about cin. But when you use cin it waits for the enter key to be pressed before it will stop getting input. It also prints what you are entering on the keyboard on the screen. I want a method that doesn''t do this.
By the sounds of it, you want to check for a keypress each game loop. So functions like cin, scanf and all of those are of no use.
If you are using windows, use GetAsyncKeyState(), you can look up the character codes in MSDN. So, for instance, say you want to exit when the escape key is pressed, you would do something like:
if( GetAsyncKeyState( VK_ESCAPE ) ){ // Exit the application}
If at first you don't succeed, redefine success.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement