You are here:

C++/same problem again.

Advertisement


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 ;/

Answer
modify

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] ) ;  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.