Sign in to follow this  

Text input questions

This topic is 4597 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
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
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

This topic is 4597 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this