C++/same problem again.
Expert: vijayan - 12/7/2008
Question
i'm really sorry. But the problem is I want the user to input characters with only m and f as well as strings such as male, female, fEMALE, MALe and so on.
You've showed me this:
transform( gender.begin(), gender.end(), gender.begin(), ::tolower ) ;
But I have no idea what that is. Isn't it possible to check the strings using s[0] and s[s.length()]
Because I want to just use it using strings and loops and functions. Nothing else ;/
Answermodify
if( ( gender != "male" ) && ( gender != "female" ) )
{
std::cout << "gender is incorrect\n" ;
// and repeat the loop
}
to
bool ok = true ;
if( gender.empty() ) ok = false ;
else if( ( gender.size() == 1U ) && ( gender[0] != 'm' ) && ( gender[0] != 'f' ) ) ok = false ;
else if( ( gender != "male" ) && ( gender != "female" ) ) ok = false ;
if( !ok )
{
std::cout << "gender is incorrect\n" ;
// and repeat the loop
}
> use it using strings and loops and functions.
std::transform( gender.begin(), gender.end(), gender.begin(), ::tolower ) ;
converts all characters in the string to lower case. you could instead write
for( std::size_t i=0 ; i < gender.size() ; ++i )
gender[i] = ::tolower( gender[i] ) ;