1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Loop problem

Discussion in 'C' started by creative, Apr 3, 2010.

  1. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    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
     
  3. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    i thought a char variable could only be one character, so when u say gender="male" etc., isnt that illegal? dunno.
     
  4. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    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'.
     
  5. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    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++.
     
  7. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    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.
     

Share This Page