text: c++ - help with user-defined functions, beginner | DaniWeb
  help with user-defined functions, beginner
  sms5660
  I am really confused about how to write functions before main(). My teacher didn't explain it well at all or the stipulations. I have a homework assignment due this weekend and I am so confused. Here is the prompt, below that is my program and what I think I should do (but can't make it work)
  Modify the program from Ch 9 as follows. 1 Move all of the processing of requests 1, 2, and 3 to separate functions called option1, option2, and option3 respectively. 2 Write a function called weeklySalary that returns the weekly salary, and use it for both option2 and option3. Note that the screen output of your program should not change at all.
  int age, yearsUntilRetirement, option = 1;
  double lifeAmountSaved;
  if (age >= 60) // retire at 65
  yearsUntilRetirement = 65 - age;
  if ((age >= 50) && (age < 60)) // retire at 67
  yearsUntilRetirement = 67 - age;
  if ((age >= 40) && (age < 50)) // retire at 70
  yearsUntilRetirement = 70 - age;
  else if (age < 40) // retire at 72
  yearsUntilRetirement = 72 - age;
  while (option!=4)
  cout << "Please select an option:" << endl;
  cout << "You have " << yearsUntilRetirement << " years until retirement." << endl;
  int payPerWeek, yearlyEarnings, lifetimeEarnings;
  cin >> payPerWeek;
  yearlyEarnings = (payPerWeek * 52);
  lifetimeEarnings = (yearsUntilRetirement * (yearlyEarnings));
  cout << "You will earn $" << lifetimeEarnings << " between now and retirement." << endl;
  double n, pmt, wr, totalAtRetirement, percentSaved, lifeAmountSaved;
  wr = .06/52;
  cin >> percentSaved;
  cout << "There are " << yearsUntilRetirement << " years till retirement." << endl;
  pmt = (payPerWeek * percentSaved * .01);
  cout << "pmt is " << pmt << endl;
  cout << "wr is " << wr << endl;
  n = yearsUntilRetirement * 52;
  totalAtRetirement = ((pow((1 + wr), n) - 1) * pmt) / wr;
  cout << "You will have $" << totalAtRetirement << " saved when you retire." << endl;
  cout << "Please choose a valid option." << endl;
  cout << "Thanks for using our program!" << endl;
  I think (obviously) that I need to make option1, option2, and option3 before main. I tried to put the if statements about age in there but visual studio said too many arguments, so I guess you can't do that? Also, I am confused about initializing my variables, I think I am doing it when I tried moving things around but it says I'm not. Also, I think I'm confused about calling the variable and what to put in the parentheses (for option 1 when I called it in case 1 I wanted to put yearsUntilRetirement in the parentheses) is that right? Any help or suggestions are greatly appreciated!!!! Thank you!
  comment Latest Post 7 Years Ago by sms5660
  It's easiest to help you if you attempt to write option1() and if it doesn't work post that code. Then we can see what you tried and explain how to fix it.
  if you put yearsUntilRetirement in the switch statement it would be based off that and the user inputting an option would be useless
  This is what I'm trying to do. Have to do the option thing or else I won't get credit for the assignment, it's the rules
  int option1()
  int yearsUntilRetirement, age;
  return yearsUntilRetirement;
  cout << option1(yearsUntilRetirement);
  Hello sms5660,
  In the line 59 you have passed a parameter to the function option1(). But in the declaration of the function , you didn't make the function to accept integer values.
  I'm such a beginner, how do I make the function accept and integer value? Thank you for your help so far!
  Put it between the ()'s like you did for the call. Check your text.
  I've tried that already, that's why I'm confused about initializing variables......it says age and yearsUntilRetirement aren't being initialized. My teacher never explained how to do multiple functions within a function, I have no idea how to do it and am trying my best. I have tried defining age and yearsUntilRetirement in case 1 and that still doesn't work.
  I've tried that already, that's why I'm confused about initializing variables......it says age and yearsUntilRetirement aren't being initialized.
  Then you did it wrong. But since I can't see what you did, I can't help.
  Here is where I'm at. It is saying I haven't initialized yearsUntilRetirement when I try to do case 1.
  int option1(int yearsUntilRetirement, int age)
  else if ((age >= 50) && (age < 60)) // retire at 67
  else if ((age >= 40) && (age < 50)) // retire at 70
  cout << "PROGRAM WENT WRONG" << endl;
  Well, the compiler is right. Where did you initialize yearsUntilRetirement? All you did is create the variable by that option, and no one knows what value is contains. The compiler is being helpful by pointing this out.
  Ahhhh, dumb mistake! I was using yearsUntilRetirement in the initial parentheses when i defined my function (sorry, bad with the lingo). This is what I have changed, thank you for all of your help!