Reputation: 5
Very new to C++. Doing my best to get a good handle on this.
The extra couts after the read statements were for troubleshooting purposes to make sure it was actually reading user input.
I feel like this is a dumb question but iv been scouring the forums to find something similar to my issue.
I cannot for the life of me figure out how to get this program to start calculating the correct interest. When comparing to an online interest calculator it comes up with a very different number than what the program is calculating.
I have spent tons of hours troubleshooting this and texting different algorithms but I have yet to come out on-top. I dont normally like asking for help because I learn by struggling through something but this one is throwing me for a loop...
So I think I might either be having an issue with the libraries I am using or a calculation error. I know the code is a bit messy and I am learning how to clean it up but as for now I just want to get the code calculating compound interest based off of user input. I have added comments to explain what each section of code does to help with my messiness.
I dont believe its a syntax error but with the way I am either using a few of the commas in the algorithm or my parentheses are somehow wrong.
the formula I am using is,
A = P (1 + r/n)^(nt)
Amount = principle (1 + interest rate/times compounded)^(rate, times compounded)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double rate;
double time;
double principle;
double amount;
double amount2;
//Asking for the amount of money that the user would like to invest
cout << "What is the amount of money you would like to invest? ";
cin >> principle;
cout << principle << endl;
//Asking for the interest rate that will be compounded annually
cout << "What is the interest rate you would like to calculate? ";
cin >> rate;
//interest rate divided by 100 so it can be multiplied by the principle and the amount of time the money will be invested later in code.
rate /= 100;
cout << rate << endl;
//Asking for the amount of years that the user will invest.
cout << "How many times would you like to compound your money? ";
cin >> time;
cout << time << endl;
//calculation for amount of money that will be made after all user input is input
amount2 = pow(rate, time);
cout << amount2 << endl;
amount = (principle * (1 + rate/time), amount2);
//Output data after all user data is input and calculated.
cout << "Your will have "; cout << amount; cout << " dollars in
interest! "<< endl;
return 0;
}
Upvotes: 0
Views: 796
Reputation: 3671
This line:
amount = (principle * (1 + rate/time), amount2);
Does absolutely nothing with principle * (1 + rate/time)
. That expression is evaluated, and then the results are discarded, leaving amount
to be assigned the value in amount2
. I assume you were trying to call a function with those two expressions as parameters.
http://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator
In a comma expression
E1, E2
, the expressionE1
is evaluated, its result is discarded, and its side effects are completed before evaluation of the expressionE2
begins.
Now that we have your formula:
A = P (1 + r/n)^(nt)
Amount = principle (1 + interest rate/times compounded)^(rate * times compounded)
I am not certain you are computing the formula you have listed above, and note the correction to the second line, where I replace the comma with multiplication.
amount2 = pow(rate, time);
Computes rate^time
, where you should have:
amount2 = 1 * time; // Where one is the times compounded per period (year)
So that your other line can read:
amount = principle * pow((1 + rate, amount2);
Because you are compounding once per year, you are dividing rate by 1, not by the number of years. By the same token, amount2 is now strictly equal to time;
Upvotes: 2