Return char if input is char, else - int?

Member
Posts: 21
Joined: 2009.05
Post: #1
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.

For example:
"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. Rasp So, now, every one in a while, I need to get the player input and store it as an integer.

For example:
Code:
scanf( "%d", &counter );
if ( counter >= 1 && counter <= 4 )
{
    HeroClass( counter );
       HeroName( counter );
}
Thus, I get the integer and use it in a function. The reason why this is not a good method is: no one can use any of my other commands, such as quit, restart, etc. So, is there a way to combine my 'get user char input' function, with the above get user integer input code?

Here is an example of my character input function:
Quote://*******************************************
// Function: PlayerQuestion
//*******************************************
char *PlayerQuestion( char playerInput[] )
{
//Input Settings: Normal
if ( inputSwitch == 0 )
{
printf( ":-: " );
scanf( "%s", playerInput );
Flush();
}

//Input Settings: Hero Specific Details
if ( inputSwitch >= 1 && inputSwitch <= 4 )
{
printf(
"|| %s: HP %d/%d MP %d/%d || :-: ",
hero[ inputSwitch ].hName, hero[ inputSwitch ].hCurrentHealth, hero[ inputSwitch ].hMaxHealth, hero[ inputSwitch ].hCurrentMana, hero[ inputSwitch ].hMaxMana
);

scanf( "%s", playerInput );
Flush();
}

//Input Settings: All Heroes Health
if ( inputSwitch == 5 )
{
printf(
"||%d/%d||%d/%d||%d/%d||%d/%d|| :-: ",
hero[ 1 ].hCurrentHealth, hero[ 1 ].hMaxHealth,
hero[ 2 ].hCurrentHealth, hero[ 2 ].hMaxHealth,
hero[ 3 ].hCurrentHealth, hero[ 3 ].hMaxHealth,
hero[ 4 ].hCurrentHealth, hero[ 4 ].hMaxHealth
);

scanf( "%s", playerInput );
Flush();
}

//Document Warrior
if ( strcmp( playerInput, "?Warrior" ) == 0 )
{
DocWarrior();
return " ";
}

//Document Ranger
if ( strcmp( playerInput, "?Ranger" ) == 0 )
DocRanger();

//Document Elemental-Mage
if ( strcmp( playerInput, "?Elemental-Mage" ) == 0 )
DocElementalMage();

//Document Crystal-Mage
if ( strcmp( playerInput, "?Crystal-Mage" ) == 0 )
DocCrystalMage();

//Shows all hero stats
if ( strcmp( playerInput, "Stats" ) == 0 && gameSwitch >= 1 )
DocStats();

if (strcmp( playerInput, "Input-Mode" ) == 0 && gameSwitch >= 1 )
ChangeInputMode();

//End Game
if ( (strcmp( playerInput, "Exit" ) == 0 ) || (strcmp( playerInput, "Quit" ) == 0 ) )
QuitGame();

//Restart Game
if ( strcmp( playerInput, "Restart" ) == 0 )
GameOpening();


return playerInput;
}

Thanks for whatever light you can shed on this small problem. Smile 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! Smile

~Achi
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
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.
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #3
I would just take the input as a string every time and then use isdigit() and atoi() as required.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  converting text input to a c char NelsonMandella 6 3,985 Feb 4, 2010 10:58 AM
Last Post: NelsonMandella
  (unsigned) char, bitsets when using - + / * wyrmmage 7 4,625 Jul 21, 2007 04:01 PM
Last Post: wyrmmage
  Char* to double, int, and back wyrmmage 18 6,567 Jun 24, 2007 11:09 AM
Last Post: akb825
  C/C++ Int To Char* Nick 4 10,905 Sep 12, 2005 10:02 AM
Last Post: Zekaric