I can't seem to figure out my problem when calling my functions. When I try to call the function the program becomes unresponsive. I need to make it so that when the user picks an option it runs the inputted information through one of the selected formulas then return to the menu when the calculation is completed. Here is my code for reference:
if (annualinterstrate == 0)
{
while (annualinterstrate == 0) {
cout << "You cannot enter 0 for annual interest rate.\n";
cout << "Annual interst rate: ";
cin >> annualinterstrate;
cout << "\n";
}
}
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;
while (x == 1 || x == 2 || x == 3 || x == 4 || x == 5); {
if (x == 1) {
double cf= cashflow(income, expenses);
cout << "Your cash flow is " << cf << endl;
}
else if (x == 2) {
double lr = leverageratio(totaldebt, totalequity);
cout << "Your leverage Ratio is " << lr << endl;
}
else if (x == 3) {
float rr=RealReturn(investmentreturn, inflationrate);
cout << "Your Real return is " << rr << endl;
}
else if (x == 4) {
double pi=PercentageIncrease(purchaseprice, marketprice);
cout << "Your Percentage increase of the value of your asset is " << pi << endl;
}
else if (x == 5) {
float di=YearstoDouble(annualinterstrate);
cout << "Years to double your investment is " << di << endl;
}
else
menu(x);
}
be more specific about which function call breaks it.
be careful with things like pi, which is often quietly defined as a numeric constant.
you repeat the menu function code for no reason in main, just call menu at the top of the while loop and remove that section.
you mix and match doubles and floats oddly. Maybe just use double everywhere to be cleaner (this is standard practice, floats are rarely used, mostly by hardware devices to save bandwidth).
try typing something when it becomes unresponsive (not just enter, type something with enter) to ensure its not an unexpected cin statement.
your menu function needs cleanup, consider a switch or just look at the logic again (make x unsigned and check <= 5 instead of 1,2,3,4,5 for example). its ok for now, just cluttering.
use code tags <> on the formatter for code samples.
When I run the code and after I enter my first menu input it returns a blank line then is unresponsive. I have the numbers the way they are 1 or 2 or 3 or 4 or 5 because the last option is type anything else to exit. so if it had "while (x =< 5) they could put something in like 4.5 and confuse the program. or even -1. I took out the menu function before the while loop and put it in the while loop now it looks like this but I still have a "crash" happen.
while (x == 1 || x == 2 || x == 3 || x == 4 || x == 5); {
menu(x);
if (x == 1) {
double cf= cashflow(income, expenses);
cout << "Your cash flow is " << cf << endl;
}
else if (x == 2) {
double lr = leverageratio(totaldebt, totalequity);
cout << "Your leverage Ratio is " << lr << endl;
}
else if (x == 3) {
float rr=RealReturn(investmentreturn, inflationrate);
cout << "Your Real return is " << rr << endl;
}
else if (x == 4) {
double pi=PercentageIncrease(purchaseprice, marketprice);
cout << "Your Percentage increase of the value of your asset is " << pi << endl;
}
else if (x == 5) {
float di=YearstoDouble(annualinterstrate);
cout << "Years to double your investment is " << di << endl;
}
else
menu(x);
}
Oh, that works to start the loop now I am running into a different problem my code is posted below and I have the menu function hardcoded in once to get the value of x because if I didn't do this my program would keep saying"x is undefined" or "x is an uninitialized variable." So I get X and the loop works but only runs through the first option and no matter the input only does the first option. Any ideas? I will also start another thread since this one is technically solved and this is another issue altogether.
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;
Basically, my problem is the first x the user inputs seems to not equal the second x the user inputs when its taken from the called menu(x) function.
I don't understand this question. Could you please try and rephrase it more clearly?
I will note that as your code currently is, there's no point passing that integer argument into the menu() function, as its value is immediately overwritten by the number input by the user.
I will also note that you've defined menu() to return a value, and yet every time you call it, you ignore the return value.
3) You've marked this thread with a green tick, which usually indicates that the problem has been solved. Is that the case?