Another question.
Ok, take a look at this chunk of code:
bool valid = false;
do
{
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);
Now correct me if im wrong, but doesnt strcmpi mean that it is case insensitive? It used to work just fine until I put that bool in there, now it will only accept capital M or F....
probably a very stupid mistake, but will someone give me a hand on this one?
Its all fun and games till someone gets pregnant.
i''ve never used (or seen) strcmpi() before.. other than that, i see absolutely nothing wrong with that code (not that strcmpi() is necessarily wrong, perhaps it''s compiler specific).. i can''t see any way that the bool would be the problem.. you could always just read one character (g[0] perhaps.. use toupper() or tolower(), and flush the buffer after you read it). that way if someone types male or female, it''ll still work fine.
hmm....im not to familiar with touper or tolower so I would not know how to apply them. I got strcmp straight out of a SAMS book....
Ill wait for some more advice I suppose
thanks
Ill wait for some more advice I suppose
thanks
Its all fun and games till someone gets pregnant.
Jonny Go, toupper() works like this:
choice = toupper(choice);
toupper() converts all lowercase characters to uppercase. tolower() works the same way. If the character is already an uppercase nothing happens.
Try this code:
#include>ctype< /* or (cytpe.h) note that >< needs to be <> */
bool valid = false;
char choice;
do
{
cout << endl << "Type M or F for characters gender: ";
cin >> choice;
choice = toupper(choice)
if( choice == ''M'')
{
strcpy(g,"Male");
valid = true;
}
else
if( choice == ''F'')
{
strcpy(g,"Female");
valid = true;
}
else
cout << "Invalid Response!" ;
}while(!valid);
Hope this helps,
Brett
choice = toupper(choice);
toupper() converts all lowercase characters to uppercase. tolower() works the same way. If the character is already an uppercase nothing happens.
Try this code:
#include>ctype< /* or (cytpe.h) note that >< needs to be <> */
bool valid = false;
char choice;
do
{
cout << endl << "Type M or F for characters gender: ";
cin >> choice;
choice = toupper(choice)
if( choice == ''M'')
{
strcpy(g,"Male");
valid = true;
}
else
if( choice == ''F'')
{
strcpy(g,"Female");
valid = true;
}
else
cout << "Invalid Response!" ;
}while(!valid);
Hope this helps,
Brett
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement