Can anyone see why the function "option3" isn't calculating the values for pmt and totalRetirement? The program runs, but I get 0's for those values. If I remove the function and place the code directly into the case option, the calculations work.

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
void option1(int retirementYears)
{
	cout << "You have " << retirementYears << " years until retirement." << endl;
}
void option2(int retirementYears)
{
	int salary;
	cout << "How much do you make per week in dollars? ";
	cin >> salary;
	cout << "You will earn $" << (salary * 52) * retirementYears << " between now and retirement." << endl;
}
void option3(int retirementYears)
{
		int salary, savingsRate = 0, n = 0;
		double totalRetirement = 0, pmt, wr = 0;
		wr = .06/52;
		pmt = (salary * (savingsRate/100));
		n =  retirementYears * 52;
		totalRetirement = pmt * ((pow((1 + wr), n)) - 1) / wr;
		cout << "How much do you make per week in dollars? ";
		cin >> salary;
		cout << "What percentage will you save? ";
		cin >> savingsRate;
		cout << "There are " << retirementYears << " years till retirement." << endl;
		cout << "pmt is " << pmt << endl;
		cout << "wr is " << wr << endl;
		cout << "n is " << n << endl;
		cout << "You will have $" <<  totalRetirement << " saved when you retire." << endl;
}
int main()
{
	string name;
	int	age, option, retirementYears = 0;
	double pmt = 0;
	
	cout << "What is your name? ";
	getline(cin, name);
	cout << "How old are you? ";
	cin >> age;
	if (age >= 60)
	{
		retirementYears = 65 - age;
	}
	if ((age >= 50 ) && (age < 60 ))
	{
		retirementYears = 67 - age;
	}
	if ((age >= 40) && (age < 50))
	{
		retirementYears = 70 - age;
	}
	if (age < 40)
	{
		retirementYears = 72 - age;
	}
	do 
	{
		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:
					option1(retirementYears);
					break;
			case 2:  
					option2(retirementYears);
					break;
			case 3:
					option3(retirementYears);
					break;
			case 4:
					cout << endl;
					break;
			default:
					cout << "Please choose a valid option." << endl;
					cout << endl;
					break;
		}
			
	} 
	while (option != 4);
	cout << "Thanks for using our program!" << endl;
}

Recommended Answers

All 2 Replies

What is the value of savingsRate when you calculate pmt on line 21?

What is the value of savingsRate when you calculate pmt on line 21?

Aha! Got it - simple code placement.

Thanks for the quick reply.

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.