Calling a Function

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:

#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;
cout << cf;
return cf;
}
double leverageratio(double totaldebt, double totalequity) {
double lr;
cout << setprecision(2) << fixed;
lr = totaldebt / totalequity;
cout << lr;
return lr;
}
float RealReturn(float investmentreturn, float inflationrate) {
float rr;
cout << setprecision(2) << fixed;
rr = ((1 + investmentreturn) / (1 + inflationrate) - 1) * 100;
cout << rr;
return rr;
}
double PercentageIncrease(double purchaseprice, double marketprice) {
double pi;
cout << setprecision(2) << fixed;
pi = (marketprice - purchaseprice) / purchaseprice;
cout << pi;
return pi;
}
float YearstoDouble(float annualinterestrate) {
float di;
cout << setprecision(2) << fixed;
di = 72 / annualinterestrate / 100;
cout << di;
return di;
}
int main()
{
double income, expenses, totaldebt, totalequity, marketprice, purchaseprice;
float investmentreturn, inflationrate, annualinterstrate;

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;
cout << "\n";

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;
cout << "\n";

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;
cout << "\n";

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


return 0;

}
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.
Last edited on
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);
}
I also changed all "pi" to "p"
Last edited on
while (x == 1 || x == 2 || x == 3 || x == 4 || x == 5); {

Your while loop ends at the semi-colon. Once this look is entered, it's an infinite loop.
Remove the semi-colon.
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.
1) Can you please do as jonnin asked, and use code tags to make your code more readable?

http://www.cplusplus.com/articles/z13hAqkS/

2)
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?
Last edited on
1
2
3
4
5
6
unsigned int x;
do
{
    x = menu();
   //other stuffs
} while (x <6 )


you don't need a new thread yet, IMHO.
Last edited on
Topic archived. No new replies allowed.