Hello everyone,I have a simple function in C++ where I ask the user to select if they female or male by typing in the appropriate string ("FEMALE" or "MALE") in upper case format only but I am thinking about it being case-insensitive as it has been causing me trouble. When I run it through the local windows debugger it always goes into the if conditional statement when I type FEMALE or MALE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
std::string createMC::getGender(void) {
std::string genderChoice;
std::cout << "Are you planning to scan female of male skin?\n";
std::cout << "Please type in FEMALE or MALE (upper case only):";
getline(cin, genderChoice);
if (genderChoice != "FEMALE" || genderChoice != "MALE") {
cerr << "Wrong gender selection..please reselect!" << endl;
getGender();
}
else {
return genderChoice;
}
return genderChoice;
}
Called function from int main():
1 2
createMC cr1;
subjectGender = cr1.getGender();
Could someone please tell me the right way to get std::string or const char* or char str[100] input from the user correctly. Thanks.
A very common mistake. You need to use && instead of ||. The choice is invalid if it is not female AND it is not male. Also, you can delete the else part; the final return is good enough.