I cant display this output..how i can i declare the gender n relate it with my price..i stuck here..oop c++
i have 2 coding for this..which 1 is better..im new in this progrmmer..

void eSalon::priceCurling()
    {
        cout<<"\nYour Gender is: "<<gender;
        if (gender=female)
      {
       cout<<"Long Hair Rm30-RM49"<<endl;
       cout<<"Short Hair RM50-RM100"<<endl;
                           }
      else if (gender=male)
      {
        cout<<"Long Hair RM46-RM70"<<endl;
        cout<<"Short Hair RM20-RM45"<<endl;
        }

or this 1,,

void eSalon::pricePerm()
    {
        cout<<"\nYour Gender is: "<<gender;
        if (gender=female)
      {
        longHair=50-100;
        shortHair=30-49;
                           }
      else if(gender=male)
      {
           longHair=46-70;
           shortHair=20-45;
           }

How is male and female declared? Why in pricePerm, longHair is defined -50 and shorthair -19, for female and similarly for male. -24 and -25?

Is that some range 30-49 , 50-100 ?
if so 2nd code is a blunder,first might be better.

How is male and female declared?

nothing is clear.

How is male and female declared? is 30-49,50-100 a range? then code2 is apt, code1 in that case is a blunder
nothing's clear

Edited 4 Years Ago by nitinmbhanu: nothing

I would use an enum value to express the geneder. If you haven't used enums in C++ before, just Google it. Enums aren't very complicated; they're basically a way of giving a human-readable name to an integer value (in your case the values would represent genders). I would have something like this:

enum EnumGender {
    eUnknown = -1,  // You don't have to, but I generally have an 'unknown' value
    eMale    =  0,
    eFemale  =  1
};

The eUnknown value is useful for expressing when something hasn't gone as planned, or when you first initialise an enum value and you don't know what it should be yet:

EnumGender gender = eUnknown;
gender = GetGender();
if ( gender == eUnknown )
{
    std::cout << "Uh-oh! Something went wrong when getting the gender!" << std::endl
    return 1;
}

Once you have an enum then you can pass a value into your function that is of type EnumGender and use it to compare in your function:

void eSalon::priceCurling( EnumGender gender )  // Passing the gender in here
{
    cout << "\nYour Gender is: ";  // Remove the gender variable from here
    if ( gender == eFemale )  // Use '==' here, not '='
    {
        cout << "Male" << endl;  // Print out the gender here
        cout << "Long Hair Rm30-RM49" << endl;
        cout << "Short Hair RM50-RM100" << endl;
    }
    else if ( gender == eMale )
    {
        cout << "Female" << endl;  // Print out the gender here
        cout << "Long Hair RM46-RM70" << endl;
        cout << "Short Hair RM20-RM45" << endl;
    }
    else
    {
        cerr << "Unknown gender!" << endl;   // Always check for something going wrong
    }
}

Hope that helps, there are also some other comments in the above code that refer to some other mistakes in your code.

Hi ravenous,

Quick question:

you assigned the enum types numbers but you didn't use them in the sample code. is there a reason for this? and also your code reads:

if ( gender == eFemale ) // Use '==' here, not '='
       {
       cout << "Male" << endl;  // Print out the gender here
       cout << "Long Hair Rm30-RM49" << endl;
       cout << "Short Hair RM50-RM100" << endl;
       }
else if ( gender == eMale )
       {
        cout << "Female" << endl;  // Print out the gender here
        cout << "Long Hair RM46-RM70" << endl;
        cout << "Short Hair RM20-RM45" << endl;
        }

Is that right or a typo? Is this correction right?

    if ( gender == eFemale ) 
           {
           cout << "Female" << endl;  
            //print details for female
           }
    else if ( gender == eMale )
           {
            cout << "Male" << endl;  
            //print details for male
            }

Edited 4 Years Ago by seslie

If you don't give enums values then they just get values starting from 0 and incrementing by one each time. Sometimes this is useful, but most of the time I like to specify the values explicitly. It doesn't make much difference in this case :)

Yes, that was a typo - good spot! :)

This article has been dead for over six months. Start a new discussion instead.