Calling a Function in a Loop

Hey, I've received a lot of great help recently and had another issue. My loop will not get past the first if statement and I have come to find that the x value used that runs through the while loop isn't the same x value that my function runs when inside the loop. (If that makes any sense). Here's what I have: (side note if I don't hardcode the previously defined function then I get the "x is undefined error." also the other functions are there I just didn't include what I didn't think was important.

int menu(x){
cout << "Enter which formula to solve \n";
cout << "1 Cash Flow\n";
cout << "2 Leverage Ratio\n";
cout << "3 Real Return\n";
cout << "4 Percentage Return\n";
cout << "5 Years to Double Investment\n";
cout << "Enter anything else to exit\n";
cin >> x;

return x;
}

int main(){

cout << "Enter which formula to solve \n";
cout << "1 Cash Flow\n";
cout << "2 Leverage Ratio\n";
cout << "3 Real Return\n";
cout << "4 Percentage Return\n";
cout << "5 Years to Double Investment\n";
cout << "Enter anything else to exit\n";
cin >> x;

while (x == 1 || x == 2 || x == 3 || x == 4 || x == 5) {


if (x == 1) {
double cf= cashflow(income, expenses);
cout << endl;
cout << "Your cash flow is " << cf << endl;
menu(x);
}
else if (x == 2) {
double lr = leverageratio(totaldebt, totalequity);
cout << endl;
cout << "Your leverage Ratio is " << lr << endl;
menu(x);
}
else if (x == 3) {
double rr=RealReturn(investmentreturn, inflationrate);
cout << endl;
cout << "Your Real return is " << rr << endl;
menu(x);
}
else if (x == 4) {
double p=PercentageIncrease(purchaseprice, marketprice);
cout << endl;
cout << "Your Percentage increase of the value of your asset is " << p << endl;
menu(x);
}
else if (x == 5) {
double di=YearstoDouble(annualinterstrate);
cout << endl;
cout << "Years to double your investment is " << di << endl;
menu(x);
}
else;

return 0;
}
Please edit your post to add the [code][/code] tags, the <> formatting icon.
1
2
3
int menu(x){
   //...
}
doesn't compile, you need to specify the type of `x'
for example int menu(int x){/**/}
but then it doesn't make sense, ¿why would you ask for a parameter if you'll ignore its value and simply overwritte it? menu(foo); menu(bar); menu(42); would all be equivalent

then in your callings you are ignoring the returned value
1
2
3
double rr=RealReturn(investmentreturn, inflationrate); //catching the return value
//...
menu(x); //ignoring the return value 



> then I get the "x is undefined error."
I don't see x's definition anywhere in your code

> the x value used that runs through the while loop isn't the same x value that my function runs
perhaps they are two different variables that just happen to have the same name (read about scope)


> I just didn't include what I didn't think was important.
good, but we need to be able to compile and run your program to reproduce your issue
if your test case does not reproduce the issue then it's useless.
you may comment the calls to the other functions or just delete those lines

(also, your main is missing its closing brace)
replace int menu(x){
with int menu(int &x){

in main() replace
cin >> x;
with
1
2
int x;
cin >> x;
my mistake I had that correctly written in my code but I did not carry it over my code looks like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "pch.h"
#include <iostream>
#include <iomanip>
#include <string>




using namespace std; 
int menu(int x) {
	cout << "Enter which formula to solve \n";
	cout << "1 Cash Flow\n";
	cout << "2 Leverage Ratio\n";
	cout << "3 Real Return\n";
	cout << "4 Percentage Return\n";
	cout << "5 Years to Double Investment\n";
	cout << "Enter anything else to exit\n";
	
	cin >> x;
	return x;
}
double cashflow(double income, double expenses) {
	double cf;
	cout << setprecision(2) << fixed;
	cf = income - expenses;
	
	return cf;
}
double leverageratio(double totaldebt, double totalequity) {
	double lr;
	cout << setprecision(2) << fixed;
	lr = totaldebt / totalequity;
	
	return lr;
}
double RealReturn(double investmentreturn, double inflationrate) {
	double rr;
	cout << setprecision(2) << fixed;
	rr = ((1 + investmentreturn) / (1 + inflationrate) - 1) * 100;
	
	return rr;
}
double PercentageIncrease(double purchaseprice, double marketprice) {
	double p;
	cout << setprecision(2) << fixed;
	p = (marketprice - purchaseprice) / purchaseprice;
	
	return p;
}
double YearstoDouble(double annualinterestrate) {
	double di;
	cout << setprecision(2) << fixed;
	di = 72 / annualinterestrate / 100;
	
	return di;
}
int main()
{
	double income, expenses, totaldebt, totalequity, marketprice, purchaseprice, investmentreturn, inflationrate, annualinterstrate;
	int x;
	
	cout << "Created by: George Tsolometes \n";
	cout << "This program will calculate your Cashflow, Leverage Ratio, Real Return, Percentage Increase, and Years to Double your Income.\n";
	cout << "Please enter the following information \n";

	cout << "Income: "; 
	cin >> income;
	
	cout << "\n";
	cout << "Expenses: ";
	cin >> expenses;
	
	cout << "\n";
	cout << "Total debt: ";
	cin >> totaldebt;
	
	cout << "\n";
	cout << "Total equity: ";
	cin >> totalequity;
	
	
	if (totalequity == 0)
	{
		while (totalequity == 0) {
			cout << "You cannot enter 0 for Total equity.\n";
			cout << "Total equity: ";
			cin >> totalequity;
			
		}
		
	}
	
	cout << "\n";
	cout << "Return on investment: ";
	cin >> investmentreturn;
	
	cout << "\n";
	cout << "Inflation rate: ";
	cin >> inflationrate;
	
	cout << "\n";
	cout << "Market price of asset: ";
	cin >> marketprice;
	
	cout << "\n";
	cout << "Purchase price of asset: ";
	cin >> purchaseprice;
	

	if (purchaseprice == 0)
	{
		while (purchaseprice == 0) {
		cout << "You cannot enter 0 as Purchase Price.\n";
		cout << "Purchase price: ";
		cin >> purchaseprice;
		
		}
		
	}
	cout << "\n";
	cout << "Annual interst rate: ";
	cin >> annualinterstrate;
	

	if (annualinterstrate == 0)
	{
		while (annualinterstrate == 0) {
			cout << "You cannot enter 0 for annual interest rate.\n";
			cout << "Annual interst rate: ";
			cin >> annualinterstrate;
			
		}

	}
	cout << "\n";
	
	menu(x);
	
	
	while (x == 1 || x == 2 || x == 3 || x == 4 || x == 5) {
		
		
		if (x == 1) {
			double cf= cashflow(income, expenses);
			cout << endl;
			cout << "Your cash flow is " << cf << endl;
			cout << endl;
			cout << endl;
			menu(x);
			
		}
		else if (x == 2) {
			double lr = leverageratio(totaldebt, totalequity);
			cout << endl;
			cout << "Your leverage Ratio is " << lr << endl;
			cout << endl;
			cout << endl;
			menu(x);
			
		}
		else if (x == 3) {
			double rr=RealReturn(investmentreturn, inflationrate);
			cout << endl;
			cout << "Your Real return is " << rr << endl;
			cout << endl;
			cout << endl;
			menu(x);
			
		}
		else if (x == 4) {
			double p=PercentageIncrease(purchaseprice, marketprice);
			cout << endl;
			cout << "Your Percentage increase of the value of your asset is " << p << endl;
			cout << endl;
			cout << endl;
			menu(x);
			
		}
		else if (x == 5) {
			double di=YearstoDouble(annualinterstrate);
			cout << endl;
			cout << "Years to double your investment is " << di << endl;
			cout << endl;
			cout << endl;
			menu(x);
			
		}
		else;
			
	}
		 

	return 0;
   
}

and when I made the changes suggested it either told me x was being re-defined or x wasn't defined
as your code is now, change line 10 to int menu(int &x) {
and read http://www.cplusplus.com/doc/tutorial/functions/ (Arguments passed by value and by reference)
thanks a lot, ne555 sorry for the oblivious questions learning this stuff online (basically self-taught) you can miss some easy things.
Topic archived. No new replies allowed.