Hi everyone, I have been a long time forum viewer but first time poster. I am really struggling with this project I am working on. I created the three functions but they are not being called when the input matched the else ifs. I really don't understand this problem. I have read so much about functions that I think I know less. Please any help would be appreciated. My errors are probably apparent to most of you right away.
I need these functions to be called upon when the user inputs the corresponding number.

Thank you all!

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

void option1() // would this execute if 1 was entered? (see below "cin >> option")
	{
				years = retire - age;
				cout << "You have " <<  years << " years until retirement." <<endl;
	}
//----------------------------------------------------------------------------------

void option2() 
	{
		cout << "How much do you make per week in dollars? ";
		cin >> salary; 
		earnings =  52 * (salary * (retire - age)); 
		cout << "You will earn $" << earnings << " between now and retirement. " << endl;
	}

//-----------------------------------------------------------------------------------

void option3() 

	{
				cout << "How much do you make per week in dollars? ";
				cin >> salary;
				cout << "What percentage will you save? "; 
				cin >> rate; 
				earnings =  52 * salary;
				total = (rate / 100)  * earnings; 
				years = retire - age;
				cout << "There are " << years << " years till retirement." << endl;
						 
						savings = savingsNow;
						pmt = (savings + total) / 52; // weekly payment made to retirement account
						cout << "PMT is " << pmt << endl; 
							
							
						wr = .06 / 52; // wr is weekly intrest rate
						cout << "wr is " << wr << endl; 
							
						n = 52 * years; //week until retirement 
						cout << "n is " << n << endl;

						totalRetirement = (pmt) * (pow(1 + wr, n)-1) / (wr);
						cout << "You will have $" << totalRetirement << " saved when you retire. " << endl;

	}

//-------------------------------------------------------------------------------------------------------------



int main ()

{
string  name; 
double total, rate, salary, savings, intrest, savingsNow = 0, x, pmt, wr, n, totalRetirement;
int age, retire, option, years, earnings;

 

cout << "What is your name? "; 
getline (cin, name); 

cout << "How old are you? "; 
cin >> age;

if ((age >= 60) && (age < 65))
	retire = 65; 

if ((age >= 50) && (age < 60))
	retire = 67;

if ((age >= 40) && (age < 50))
	retire = 70;

if (age < 40)
	retire = 72;
	
		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; 

			if (option == 1) 
			{
				option1();
 // Shouldn't this call the declared function above and send it back down here?!?!?
			}
			else if (option == 2) 
			{
				option2(); 
			} 
			else if (option == 3) 
			{ 
				option3(); 
			}

			else
			{
			cout << "Please choose a valid option." << endl; 
			} 

			}while (option !=4);
		
		cout << "Thanks for using our program!" << endl;
		cout << endl; 
}

Again, thank for looking guys. ANY help would be appreciated.

There are two opening curly braces after the do keyword on line 82, this may be interfering.

Thanks ichigo, I actually put that in by accident. The code is still not working. I just keep getting "undeclared identifiers" for every friggin thing I put in. The program was working great until I had to switch everything to declared functions.

Your problem is that you are attempting to use identifiers declared as local to main() inside of functions that they do not exist in.

void someFunc();

int main() {
  int anIntInMain = 15;  //declare an integer local to main()
  someFunc();
  return (0);
}

void someFunc() {
  int someTotal = 0, someValue = 5;
  someTotal = someValue + anIntInMain;  //ERROR: anIntInMain is not in scope
}

You will have to add them to the functions as arguments/parameters:

void someFunc(int);

int main() {
  int anIntInMain = 15;  //declare an integer local to main()
  someFunc(anIntInMain);
  return (0);
}

void someFunc(int incommingValue) {
  int someTotal = 0, someValue = 5;
  someTotal = someValue + incommingValue;
}
commented: Great help without giving me the answer. I didnt get function untill he explained it. +0

Thanks bro, that was very helpful. You explanation made it a lot easier to understand. I have a another question. How can one create a function that is used inside of another function. I now understand the concept but nesting them seems crazy! Say I wanted to calculate "years" in a function then use then return "years" to option1() and option2(). This is just an example not my actual problem. I really want to understand C++ not just get by in it.

Just call the function. There's no magic about it at all.

Yeah, nothing much to it. I'll give you a small demonstration that will hopefully make it clear for you.

int calculateYears(int retire, int age)   {
  return (retire-age);
}

void option1(int retire, int age)  {
  cout << "You have " << calculateYears(retire, age) << " years until retirement. 
n";
}

>>How can one create a function that is used inside of another function
Nothing special about it. Just declare and implement it like any other function. As long as you format your call correctly and the function identifier (the function's name) is in-scope, it'll work just fine.

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.