-
Input in C++
I'm fairly new to c++, but I know a lot of the basics. I have decided to create a console game of some kind. At first, I wanted to create a roguelike, but after weeks of reading source files, tutorials, and books, I still have absolutely no clue how to do that. So I decided to make a game without even simple ascii graphics, a text-based game. After working on it for a while, I realized that the method of input I had been using, cin, wasn't good enough. I need a way to have a person hit a key, and the program to immediately respond, instead of waiting for the person to type whatever they want and then hit enter. How would I go about doing this?
-
-
one popular method:
Code:
char c;
cout << "Makes a selection:\n\n"
<< "[P]ick up\n"
<< "[I]ventory\n"
<< "> ";
cin >> c;
// then use a switch statement to control what happens next
switch ( c )
{
case 'P':
if ( item <= 0 )
{
cout << "You pick it up";
}
else if ( item >= 1 )
{
cout << "You already have it";
}
break;
case 'I':
inven(); // call a function to deal with this
return here(); // return to this section of code when inven is done
break;
}
this is one idea, there are many others. You could divise a class that deals with user input
but depending how far ahead in C++ you are that may be over your head. Remember, if the user hits an icoreect key, you must deal with this issue using the deafult switch command, but that is again only one way. Good luck in your creation
-
One other point, if you want to avoid the "hit enter" then you could assign c to getchar()
This would eliminate the need to press enter. getch() is defined in the conio.h library which is a little non-standard, but it is an efficinet way to achieve the result
-
> which is a little non-standard
Can you say completely? :)
-
ok ok, I got it wrong -< il go sit the corner with a dunce hat on ( chuckes...!!! lol )
-
>a little non-standard
As in half dead or a little pregnant? :rolleyes:
-
Awesome, thanks everyone! I read that page from the FAQ, and ended up using this to accomplish my goal:
Code:
#include <conio.h>
while ((ch = getch())){
if (ch == 'n')
goto newgame;
}
-
don't use goto either! its horrid! use function call instead
Code:
if ( ch == 'n' )
{
newgame();
}
goto is frowned upon in C++ as a throw back to spagetti code, a mess of code jumps!
using function calls make the system safer and your code looks much better.
-
>don't use goto either! its horrid!
Hmm, can you explain in detail why goto shouldn't be used in this particular situation? :)
>use function call instead
Actually, a loop seems to be more appropriate, judging by the very incomplete snippet given.
>goto is frowned upon in C++ as a throw back to spagetti code, a mess of code jumps!
That's a cop out. It's easy to say "never use goto" and throw around the phrase "spaghetti code", but that's not even close to the whole story. I get the feeling you're telling others what you were told, and "don't use goto" comes from a teacher rather than personal experience.
-
I did not mean to offend prelude, i was just told that goto should be avoided in all cases. There is usually always another way to solve a problem without having to use goto. I was only making a suggestion, it is not the absolute
-
>I did not mean to offend prelude
Rest assured, if you offend me, there won't be any doubt. ;)
>i was just told that goto should be avoided in all cases
Yea, that's the typical overreaction to a lot of holy war issues. It's not your fault at all. Though I will recommend a guideline that I try to follow religiously: Never use absolutes unless you're absolutely sure. If someone says "always" or "never", they're probably wrong.
-
I understand. Thank you for correcting me on the issue. Does the kwyword goto go back further than the BASIC language? I am just interested to know
-
-
Thanks dave, that was a great link