So my assignment is pretty simple, but it requires me to use a value-returning function instead of a void function. The one problem I'm having is letting the function allow user input. Its simple: The user enters X for amount %26amp; X for interest rate. And to make matters worst, my book doesn't have ANY good examples! (FYI: Prentice Hall is a BAD publisher!). In fact, NONE of the examples allow the user to input data! %26gt;:(
Anyway here's my code:
/*DESCRIPTION:
*This program will allow the user to
*enter the interest rate %26amp; the
*amount invested. As a result, the
*program will display a table showing
*value for 30 yeats.
*
*NOTE: A function will used to calculate
* the future value of each year.
*/
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;cmath%26gt;
using namespace std;
//Future Investment function
double futureInvestmentValue (double investmentAmount,
double monthlyInterestRate, int years)
{
//FORMULA:
//accumulatedValue = investmentAmount * (1 +
//monthlyInterestRate)^numberOfYears*鈥?br>
//declare %26amp; initialize variables
monthlyInterestRate = years / 1200;
double futureValue = investmentAmount* pow (1 + monthlyInterestRate, years * 12);
return futureValue;
}
int main()
{
//prompt user to enter rate %26amp; investment amount
cout %26lt;%26lt; "Enter Amount Invested: ";
double investment;
cin %26gt;%26gt; investment;
cout %26lt;%26lt; "Enter Annual Interest Rate: ";
double annualRate;
cin %26gt;%26gt; annualRate;
//error message for invalid input
if (investment %26lt;= 0 || annualRate %26lt;= 0)
{
cout %26lt;%26lt; "ERROR: Invalid number." %26lt;%26lt; endl;
}
else
{
//declare the function
double investmentDisplay = futureInvestmentValue (1.00, 1.00, 1);
//table labels
cout %26lt;%26lt; left;
cout %26lt;%26lt; setw(12) %26lt;%26lt; "Years" %26lt;%26lt; setw(12) %26lt;%26lt; "Future Value"
%26lt;%26lt; endl;
cout %26lt;%26lt; "---------------------------------------鈥?%26lt;%26lt; endl;
//display results
for (int i = 1; i%26lt;= 30; i++)
{
cout %26lt;%26lt; left;
cout %26lt;%26lt; setw(12) %26lt;%26lt; i %26lt;%26lt; setw(26) %26lt;%26lt; investmentDisplay %26lt;%26lt; endl;
}
}
//pause %26amp; display screen
system("PAUSE");
return 0;
}
OUTPUT:
================================
Enter Amount Invested: 10000
Enter Annual Interest Rate: 9
Years Future Value
--------------------------------------鈥?br>
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
Press any key to continue . . .
==============================
NOTE: I know the invoke is giving "amount" the value 1, but if I don't give futureInvestmentValue 3 values, I will get a syntax error. What am I doing wrong? Damn my retarded textbook!C++ help: Why isn't my value-returning function working?
Let's talk about this line:
double investmentDisplay = futureInvestmentValue (1.00, 1.00, 1);
which is not declaring a function. It's declaring a variable (investmentDisplay) and setting its value to the return value of the futureInvestmentValue function.
But you're passing three 1s as parameters and ignoring the input you got from the user. You need to pass the user's input as parameters to this function. You've already stored the user's input in variables 鈥?just use those as the arguments to this function.C++ help: Why isn't my value-returning function working?
I didn't look at the whole thing, but this jumped out at me:
monthlyInterestRate = years / 1200;
years is an int.
So you have integer division here. monthlyInterestRate will always be 0.
What you want is this:
monthlyInterestRate = years / 1200.0;
Since you're now dividing an int by a double, the result will be a double. So your monthlyInterestRate will now be the value you're expecting.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment