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!

Recommended Answers

All 11 Replies

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

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;


int option1()
{
	int yearsUntilRetirement, 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;
	}

	cout << "You have " << yearsUntilRetirement << " years until retirement." << endl;
	cout << endl;
	return yearsUntilRetirement;
}

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;
	

	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 << option1(yearsUntilRetirement);
				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;
}

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.

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.

cout << option1(yearsUntilRetirement);

I'm such a beginner, how do I make the function accept and integer value? Thank you for your help so far!

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.

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.

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;


int option1(int yearsUntilRetirement, int age)
{
	if (age >= 60) // retire at 65
	{
		yearsUntilRetirement = 65 - age;
	}
	else if ((age >= 50) && (age < 60)) // retire at 67
	{
		yearsUntilRetirement = 67 - age;
	}
	else if ((age >= 40) && (age < 50)) // retire at 70
	{
		yearsUntilRetirement = 70 - age;
	}
	else if (age < 40) // retire at 72
	{
		yearsUntilRetirement = 72 - age;
	}
	else
	{
		cout << "PROGRAM WENT WRONG" << endl;
	}
	return yearsUntilRetirement;
}

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;
	

	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;
}

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!

8. int option1(int age)
59. cout << "You have " << option1(age) << " years until retirement." << endl;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.