0

Trouble Coding Compound Interest

I'm trying to calculate compound interest using the formula A = P(1 + r/12)^(12t) My error code is saying the compoundInterest must return a value but Visual Studio isn't telling me which line the error is. #include <iostream> #include <math.h> using namespace std; double compoundInterest(double P, double r, int t); int main (void) { double P, r; int t; cout << "This app is used to calculate the compound interest in an account. " << endl; cout << "Enter the principal in the account: " << endl; cin >> P; cout << "Enter the annual rate for the account as a percent: " << endl; cin >> r; cout << "Enter the time in number of years: " << endl; cin >> t; cout << "Balance of account after interest: " << compoundInterest( P, r ,t) << endl; return 0; } double compoundInterest(double P, double r, int t) { double A = P * pow(1 + r / 12, 12); system("pause"); }

2nd Sep 2018, 4:35 AM
Krista Clark
Krista Clark - avatar
2 Respostas
+ 14
Try changing your function to this: double compoundInterest(double P, double r, int t) { double A = P * pow(1 + r / 12, 12*t); return A; } The reason why it wasn't working is because your function must return a double but it wasn't returning anything.
2nd Sep 2018, 4:47 AM
Eduardo Petry
Eduardo Petry - avatar
+ 2
Thank you. It fixed the problem.
2nd Sep 2018, 5:08 AM
Krista Clark
Krista Clark - avatar