Hey guys! i Have a prblm writing a program, i hope i can find help here.... in the prgram i have to take user input on gender as 'm' or 'f' or 'male' or 'female' or 'Male' or 'FmAle'.... what i mean is i the input should not be case sensetive... can anyone please help:)

>> can any one help?
Yes -- but I can't see what you have attempted so you will just have to post your code here.

>>the input should not be case sensetive
Convert the input string to all upper or lower case. You can use the toupper() and tolower() macros for that -- they work on only one character so you will have to do it in a loop.

#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;

int main(int argc, char *argv[])
{char g;
int i;
str s;
cout<<"Enter your gender :"<<flush;
cin>>g;


for(i=0;i<s.length();i++)
g=tolower(g);

if (g=='m' && 'male')
cout<<"hello Mr."<<endl;

else
if(g=='f'&&'female')
cout<<"Hello Ms."<<endl;

i am really new to C++ ...thas why cudnt strt on my own..thx for the help neways... but i have been able to do this much only.... what cpmmand do i use to change a sstring to lower case?


system("PAUSE");
return EXIT_SUCCESS;
}

hi guys!! i am trying to write a program in which i have to input gender in a non case sensetive manner.... and out mr or mrs...on the bases of the input.... i have to take input in 'm' 'f' or 'male' 'female' in any case. i ave tried this program but it doesnt wrkd... can ne1 hlp me out...

#include <cstdlib>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>

using namespace std;

string toLowerCase (string s)

{
    string g=s;
    toLowerCase(g);
    return g;
}
int main(int argc, char *argv[])
{
    string s;
    cin>>s;
    cout<<toLowerCase(s)<<endl;
int i;



cout<<"Enter your gender :"<<flush;
cin>>s;
cout<<s<<endl;


{if (i=='m' && 'male')
cout<<"hello Mr."<<endl;

else
if(i=='f'&&'female')
cout<<"Hello Ms."<<endl;
}


    system("PAUSE");
    return EXIT_SUCCESS;
}
string toLowerCase (string s)

{
    string g=s;
    toLowerCase(g);
    return g;
}

That's not how to convert a string to lower case. Use the tolower() macro in a loop and convert each character

string toLowerCase (string s)
{
     string g = s;
     for(int i = 0; i < g.length(); i++)
        g[i] = tolower[g[i]);
     return g;
}

or you could do it this way in c++

string toLowerCase (string s)

{
    string g=s;
    transform(g.begin, g.end(), g.begin(), tolower);
    return g;
}

Also: why do you check i?
Instead of if(i=='m' && 'male') try if(s=="m" || s=="male") .

First correct your toLowerCase as AncientDragon suggested, then fix "minor" things like the one I pointed out to you (and others like double s input)

thanks guysguess i got it almost... now m tryin to put the if else condition for outputing MR. if it is male...or else show an error message...but this doesnt wrk... can u point out the prblm/??

<code>
#include <cstdlib>
#include <iostream>


using namespace std;
string toLowerCase (string s)
{
string g = s;
for(int i = 0; i < g.length(); i++)
g = tolower(g);
return g;
}
int main(int argc, char *argv[])
{
string s;


cin>>s;
cout<<toLowerCase(s)<<endl;


if( s=="m"  || "male")
cout<<"hello Mr."<<endl;
else
cout<<"error"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
</code>

Edited 3 Years Ago by happygeek: fixed formatting

if( s=="m" || "male")

Should be

if( s=="m" || s=="male")

You have to compare it to the s variable again, else it will evalute "male" as a char array, which will always return true.

Why bother with the whole word when you only need the first letter ('m' or 'f')?

if(s[0]=='m' || s[0]=='M')
// do what you want for male

else
// do what you want for female
This article has been dead for over six months. Start a new discussion instead.