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.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string name;
int age, yearsUntilRetirement, option = 1;
double lifeAmountSaved;
cout << "What is your name? ";
getline(cin, name);
cout << "How old are you? ";
cin >> age;
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 << "\t(1) Number of years to retirement" << endl;
cout << "\t(2) Amount earned between now and retirement" << endl;
cout << "\t(3) Amount saved at retirement" << endl;
cout << "\t(4) Exit (do nothing)" << endl;
cin >> option;
switch (option)
{
case 1:
cout << "You have " << yearsUntilRetirement << " years until retirement." << endl;
cout << endl;
break;
case 2:
int payPerWeek, yearlyEarnings, lifetimeEarnings;
cout << "How much do you make per week in dollars? ";
cin >> payPerWeek;
yearlyEarnings = (payPerWeek * 52);
lifetimeEarnings = (yearsUntilRetirement * (yearlyEarnings));
cout << "You will earn $" << lifetimeEarnings << " between now and retirement." << endl;
cout << endl;
break;
case 3:
double n, pmt, wr, totalAtRetirement, percentSaved, lifeAmountSaved;
wr = .06/52;
cout << "How much do you make per week in dollars? ";
cin >> payPerWeek;
cout << "What percentage will you save? ";
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;
cout << "n is " << n << endl;
totalAtRetirement = ((pow((1 + wr), n) - 1) * pmt) / wr;
cout << "You will have $" << totalAtRetirement << " saved when you retire." << endl;
break;
case 4:
cout << endl;
break;
default:
cout << "Please choose a valid option." << endl;
}
cout << 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!