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:)

Dani AI

Generated

Good progress in the thread — the problems in the example code are common and easy to fix. The core issues were: using the wrong types (char vs std::string), calling tolower incorrectly, and writing boolean checks that always evaluate true. Useful pointers already came from , , , and ; the notes below glue those ideas together into a small, safe workflow.

Read the whole user response into a std::string (use std::getline if you want to accept spaces), then trim leading/trailing whitespace and check for emptiness before indexing. Normalize the text to lower case so comparisons are simple. A safe way to lowercase every character is to use std::transform with std::tolower, making sure to cast each char to unsigned char inside the call to avoid undefined behavior on negative char values.

Decide how you want to match input:

  • Shortcut: look at the first character after trimming/lowercasing and accept 'm' or 'f'. Remember to check s.empty() first.
  • Full-word: compare the normalized string against the words you accept (repeat the variable in each comparison; do not rely on a string literal alone in a logical expression — that will always be treated as true). Also avoid single quotes around words: 'male' is not a string literal.

Other small but important tips: avoid system("PAUSE") for portability; validate and test inputs like empty string, extra spaces, single letters and mixed case; give clear error messages when the input is not recognized. Following these steps will make the code robust and fix the specific mistakes seen in 's samples while using the good suggestions already posted by the other members.

Recommended Answers

All 11 Replies


Chris

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

hey can i use a void function for outputtin MR. after taking in the gender???

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.

thx so much guys....i got it...:D

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.