Jump to content
  • Advertisement
Sign in to follow this  
rycon

Text input questions

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to programming and I am making my first tic tac toe game. I have a few questions about text input. I have a menu with 4 options in my game. How can I read just one character in from the keyboard. With cin you have to press return. Is there a way to just hit one key and have the program continue. Also what is a good way to screen the input data. The only valid inputs will be 1 - 4. Do i need to read this in as a char or could I use an int. If I use an int what happens if someone enters an 'a' for example. Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
continuing after a user presses one key is a bad idea. what if the user presses the wrong key (happens to me a lot)? it could possibly screw up your program...

Share this post


Link to post
Share on other sites
Well what I mean by continuing after pressing a key is this. The menu displays, user enters 1. The program then continues with that value, checking that it is a valid input value. I still will have input screening. I just want to eliminate the need to press enter. It is not a big deal, just seems cleaner to me.

Share this post


Link to post
Share on other sites
The incomparable Zahlman posted this before. You can also avoid the problem by just using char.

If you want to read a character without having to hit enter, just include conio.h and use getch().

input = getch();

If you want to prompt the user until they enter a valid input use an if statement that checks for values that are greater than '4' or less than '1'. Remember the single quotes if you decide to use char, or else you will be referring to the ASCII character with the value 1, rather than the character 1 which has a value of 47 or 48 or something.

[Edited by - skulldrudgery on May 15, 2005 3:57:10 PM]

Share this post


Link to post
Share on other sites
Thanks skulldrudgery, that is exactly what I was looking for. It works great now. Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by skulldrudgery
Tor else you will be referring to the ASCII character with the value 1, rather than the character 1 which has a value of 47 or 48 or something.


49. [smile]

And yeah. There aren't really any good standard ways to do this; the cin input stream is line-buffered - it's a property of that particular stream, not of the library functions. I don't suppose anyone knows a portable way to muck around with the deeper iostream stuff and change that? :s

Anyway, if you want to read one character at a time, then read it into a character, and process it like a character - even if it is intended to be a digit. Do conversion *after* you have checked that the "command" is valid.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!