You are here:

C++/Hey again.

Advertisement


Question
Hi.

I understand c++ upto using strings and functions. What about like asking the user to output male or female or m or f ? Like if you input something else, it would loop again and ask for the gender?

Isn't there a way to find out without using algorithms but just using if, while or do loops and using strings?

and instead of std:: "using namespace std;" works too.

Answer
> Isn't there a way to find out without using algorithms but just using if, while or do loops and using strings?

certainly yes. algorithms that operate on sequences internally implement a loop to iterate through the sequence. for example, instead of writing

   if( std::find_if( name.begin(), name.end(), ::isdigit ) != name.end() )
       std::cout << '"' << name << '"' << " contains a digit\n" ;

we could have written
   
   bool ok = true ;
   for( std::size_t i=0 ; i<name.size() ; ++i )
        if( ::isdigit( name[i] ) ) ok = false ;
   if( !ok )
       std::cout << '"' << name << '"' << " contains a digit\n" ;

> asking the user to output male or female or m or f ?
> Like if you input something else, it would loop again and ask for the gender?

this is easy:

   char gender ;
   std::cin >> gender ;

   while( ( gender != 'm' ) && ( gender != 'f' ) )
   {
         std::cout << "gender is incorrect\nplease renter the gender: " ;
         std::cin >> gender ;
   }

> and instead of std:: "using namespace std;" works too.
yes, indeed it does.
the "using namespace std;" directive pulls the identifiers in the std namespace into the current namespace so that you don't have to explicitly specify that you are referring to an identifier from the std namespace.
it should be used judiciously in the smallest possible scope (and never in header files).  

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.