First of all this is my first post. I have only been in C++ for 6 months since my school has opened up the class. anyways I have been tried what I could to get this function to work. It gets the gender in from the user in character and then if they enter in the m or f then loop ends. I tried just ending the loop if m is entered and commented out f. The code worked but when I tried them both together again it still didn't work. I would appreciate any help from anyone on how to make this easier or just what is wrong with the code. thanks for the time. Code: void Gender() { char sex=0; while(***!='m'||***!='f') { sex=0; cout<<"What is your character's gender?M or F: "<<endl; cin>>sex; sex=tolower(***); } if(sex=='m') gender="Male"; else gender="Female"; } Gender is global string.
take out sex=0; you don't need it and also the character variable *** should not have initialized as 0 either. i think i see what you are doing, but you don't need to clear the array with 0. when you cin again, it will over write itself
i thought a char variable could only be one character, so when u say gender="male" etc., isnt that illegal? dunno.
The program will only store one character in the variable and will store all that other stuff somewhere in memory. so when the user enters "male" the program will only read it as if they entered 'm'.
Well gender is global string that I initalized at beginning of program(last line of my first post). I could have just passed it but it was just as fast to run it as a global.
oops i thought you meant the char variable well what kenshin did was created a string variable called gender. either by declaring a string like this Code: string gender; or like this Code: char gender[7]; thus allowing it to accept more than one character you'll learn about string variables soon if your taking a class in C++.
Two things: 1. Yes, *** should be initialized at some point before the loop. The line where you declare it is good enough. The reason is that unlike global variables, which are automatically initialized, local variables are not. This means there's some random junk in the variable when you first declare it. Chances are it's not the characters 'm' or 'f', but you should still initialize, just in case. 2. The reason your loop isn't terminating is the clause: Code: *** != 'm' || *** != 'f' If *** is 'm', then you'll get this: false OR true -> true If *** is 'f', then you'll get this: true OR false -> true See? What you actually want is for the clause to evaluate to false when *** is either 'm' or 'f'. Another way of saying that is you want the clause to evaluate to false when *** is not 'm' and not 'f'. See if that helps.