Advertisement

School me!

Started by February 27, 2000 09:19 PM
6 comments, last by Jonny Go 24 years, 9 months ago
Ok, Ive written a very simple program that generates a character for you based on the information the user supplies. Its going to be used for RPG''s (the ones that involve imagination and dice, not computer RPG''s!) Its insanely basic and with out a skill system or anything, actually its not even written for a particular RPG system yet, but im gettin there! Anyways, I need a seasoned programmer to look through this small chunk of code and tell me how to make it loop until the proper information is entered: cout << endl << "Type M or F for characters gender: "; cin >> g; if( !strcmpi(g,"M")) strcpy(g,"Male "); else if( !strcmpi(g,"F")) strcpy(g,"Female"); else cout << "Invalid Response!" <<endl; The book I have does a pretty shabby job on explaining loops, and I also find it easier to learn when someone shows me directly how to do something rather than a book. Thanks. Please note, I will have a million more questions in the near future. Its all fun and games till someone gets pregnant.
Its all fun and games till someone gets pregnant.
you could do this:

BOOL valid_input = FALSE;do{    if( !strcmpi( g, "M" ) )    {        strcpy( g, "Male " );        valid_input = TRUE;    }    else if( !strcmpi( g, "F: ) )    {        strcpy( g, "Female " );        valid_input = TRUE;    }    else    {        cout << "Invalid Response!\n";    }}while( valid_input = FALSE ); 


Advertisement
bool valid = false;
do//start loop
{
cout << endl << "Type M or F for characters gender: ";
cin >> g;

if( !strcmpi(g,"M")){
strcpy(g,"Male "); valid = true;}
else if( !strcmpi(g,"F")){
strcpy(g,"Female"); valid = true;}

else
cout << "Invalid Response!" ;

}while(!valid);//keep going if its invalid

thats funny. out of all the possible ways we both did it the same way.
Excellent! Consider it appreciated!
Its all fun and games till someone gets pregnant.
Which is faster , g[0]=='M' && g[1]=='\0' or strcmp (g,"M") ?

Just wondering ....


Edited by - Strauss on 2/27/00 9:54:12 PM
Advertisement
if i were to guess it would be the first one cuase strcpy does the exact same thing plus the overhead of a function call.
>>>>
Which is faster , g[0]=='M' && g[1]=='\0' or strcmp (g,"M") ?
>>>

Makes no difference to but
if strcmp (g,"M") is a function call then you have to push stuff onto the stack and that takes time.
if it's a macro then iot should be the same as g[0]=='M' && g[1]=='\0'
For clarity and readability go with strcmp (g,"M")
It's better because in a week you're not going to remember exactly what those equal signs mean

ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
Check out my web-site

Edited by - ZoomBoy on 2/27/00 10:24:37 PM

This topic is closed to new replies.

Advertisement