Return char if input is char, else - int? - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Return char if input is char, else - int? (/thread-4025.html)
Return char if input is char, else - int? - Achithyn - Aug 5, 2006 12:04 PM
Greetings, (Edit: this is using C99)
I've been playing around with my little text RPG all morning, and have hit a wall. Right now, I have a function that handles all player input. I created this function because I wanted to give my player the ability to write other commands to the game, other than the ones being presented.
"How may I help you?"
--( B )-| Buy
--( S )-| Sell
--( L )-| Leave
I wanted my users to be able to type quit, and have the quit function activated. There are a lot of other little commands I don't want to write over and over, and that is why I have the function. So, now, every one in a while, I need to get the player input and store it as an integer.
Here is an example of my character input function:
Thanks for whatever light you can shed on this small problem. I've been at it for about an hour and cannot figure out a way to do it. I've played with unions, though don't know if that would be the correct way to go. Let me know if you have any questions - I'll keep trying till I figure it out or get an answer. Thanks again!
Return char if input is char, else - int? - ThemsAllTook - Aug 5, 2006 12:17 PM
scanf returns the number of successful conversions. If you're scanning for "%d" and the input can't be converted to an integer, scanf will return 0. That might help you for distinguishing entered numbers from other strings.
Return char if input is char, else - int? - backslash - Aug 6, 2006 03:43 AM
I would just take the input as a string every time and then use isdigit() and atoi() as required.