Recursion age

extemployeetype.cpp

[code]
void extEmployeeType::yearsFromRetirement(int age)
{
if (age == 65)
cout << "The employee is " << diff << " years from retirement." << endl;
else if (age < 65)
{
diff++;
yearsFromRetirement(age + 1);
}
else
{
diff--;
yearsFromRetirement(age - 1);
}
}
[/code]



main.cpp
[code]
int main(int argc, char *argv[])
{
int age;
employeetype age;

cout << "Enter the age for the employee -> ";
cin >> age;

age.getAge(int);
age.yearsFromRetirement(int);

system("PAUSE");
return EXIT_SUCCESS;
}

[/code]

int age;
employeetype age;
^have no clue what do to with those two lines as far as declaring.


cout << "Enter the age for the employee -> "; -- this line is correct.
cin >> age; -- not sure if that's correct.

age.getAge(int);
age.yearsFromRetirement(int);
^need alot of help with these lines.



this program lets you input an age then if the age is however many years less then 65, it output a retirement message.. years before... and 65+.. years after retirement. I have no clue what to do in the main function to call it.
Someone please help me. I'm about feedup with it. My teacher is telling me it's like my other program that's similar to this but that deals with a clock and this doesn't. I don't know how to get it to accept a "person's" age. Help please!

Comments

  • i m nt sure i hv understood wht exactly ur problem is! however, two vars called age in the same scope ... a typo i guess? why shudnt cin >> age work (assuming an operator>> is defined for whtevr type age is of)

  • [code] int main(int argc, char *argv[])
    {
    int age;
    employeetype age;

    cout << "Enter the age for the employee -> ";
    cin >> age;

    age.getAge(int);
    age.yearsFromRetirement(int);

    system("PAUSE");
    return EXIT_SUCCESS;
    }

    [/code]



    I thought I made my problem clear? I guess not.

    int age; -- that I'm pretty sure is correct.
    employeetype age; -- I'm not sure what to declare here as far as a variable and whatnot.

    age.getAge(int); -- I'm not sure if "age" is right or like I said what to declare here to make the main function call the member function.
    age.yearsFromRetirement(int); -- same here, i don't think age is right but I'm clueless as to what goes in here.


    When the main function calls the member function for yearsFromRetirement.. whatever age I put in with output "It is (age) before OR after retirement. I just don't know how exactly to call it.
  • 1> two vars by the name age in the same scope -- not allowed.
    2> employeetype -- why nt employee?
    3> getAge() unnecessary. also, consider a fn declared as,
    int f(int);
    the call to f() will b f(5) or f(anIntVar), nt f(int)
    4> yearsFromRetirement shud nt print, it shud return the diff. what if i want a diff print format?
    5> calculating diff uses tail recursion and updates global var diff ... risky, costly and unnecessary.

    i m writing a somewht equivalent code. hope ths helps to clarify the concepts.
    ths isnt a good design. i m jst trying to mk as less change to the original code structure as possible

    int main(int argc, char *argv[])
    {
    int age;

    // prompt for age
    cout << "age? ";
    cin >> age;

    // static member function of employee
    employee::printRetirementStatus(age);
    }

    void employee::printRetirementStatus(int age)
    {
    int diff = yearsFromRetirement(age);

    if (diff == 0)
    cout << "retirement due in this year" << endl;
    else if (diff > 0)
    cout << "retired " << diff << "years ago" << endl;
    else
    cout << "will retire after " << diff << "years" << endl;
    }

    inline int yearsFromRetirement(int age)
    {
    const int RETIREMENT_AGE = 65;
    return (age - RETIREMENT_AGE);
    }

    and if u must use recursion, variable diff shud b initialized to 0.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion