iDevGames Forums
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.

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


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.