# Amateur question.

Printable View

• 02-07-2005
RP319
Amateur question.
This program is supposed to take the four inputs and use them to calculate the balance for each year of the users N amount of years.

Balance = P * (1 + I / Q ) ^ (N * Q)

I've managed to do pretty much everything except get it to output the answers. I keep getting jumbled output. Any help would be greatly appreciated. Thanks.

Code:

```#include <fstream> #include <iostream> #include <string> #include <iomanip> #include <cmath> using namespace std; void screenHeader();                        // Parameterless Void Function void inputValues(float &p, float &i, float &q, int &n);    // Passed by reference float getBalance(float p, float i, float q, int n);        // Passed by Value int main() { int another = 1; float p, i, q; int n;                // Declaring Variables char answer; float balance; system("clear");            // Clears Screen   screenHeader();            // Calling Void   while(another != 0) {                            // While Loop       system("clear");     screenHeader();     inputValues(p, i, q, n);                 cout << "--------------------------------------------" << endl;     cout << "Principal amount======================> " << p << endl;       cout << "Annual interest rate==================> " << i << endl;       cout << "Times compounded per year=============> " << q << endl;       cout << "Years to keep in account==============> " << n << endl;       for(int count = 1; count <= n; count++)                    // For Loop     {       balance = getBalance(p, i, q, count);       cout << "--------------------------------------------" << endl;       cout << "Balance after " << count << " years: " << balance << endl;     }         cout << "--------------------------------------------" << endl;     cout << "--------------------------------------------" << endl;         cout << "Would you like to do another calculation? Enter Y or N: ";         cin >> answer;         if(answer != 'y' && answer != 'Y')         another = 0; }                                              // end of while loop  return 0; } void screenHeader()  {   cout << "Program SAVE" << endl;   cout << "--------------------------------------------" << endl;   cout << "This program will request that you enter"    << endl;   cout << "values for:                    " << endl;   cout << "                        " << endl;   cout << "    a principal amount (P)                " << endl;   cout << "    an annual interest rate (I)            " << endl;            // Cout's   cout << "    times compounded per year (Q)          " << endl;   cout << "    years in account (N)            " << endl;   cout << "                        " << endl;   cout << "The program will then calculate and print  " << endl;   cout << "the balance for each year up through year N." << endl;   cout << "--------------------------------------------" << endl;   cout << "                                            " << endl;  }  void inputValues(float &p, float &i, float &q, int &n) {      cout << "Please enter your principal amount: "                  << endl;        // Inputting Values and Outputting Statements   cin  >> p;   cout << "Please enter the annual interest rate: "                << endl;   cin  >> i;   cout << "Please enter the times compounded per year: "          << endl;   cin  >> q;   cout << "Please enter the number of years in the account:  "    << endl;   cin  >> n; } float getBalance(float p, float i, float q, int n)                    {   return  (pow (( p*(1+ (i / q))),(n * q))); }```
• 02-07-2005
frrossk
I think your formula for calculate the balance is getting to pretty big numbers (that get beyond the range for floating numbers in C). Declare double getBalance(float p, float i, float q, int n); and see what happens.
Here is the result of your program on my compiler:
Code:

```//my output This program will request that you enter values for:                                                a principal amount (P)                    an annual interest rate (I)                times compounded per year (Q)              years in account (N)                                    The program will then calculate and print  the balance for each year up through year N. --------------------------------------------                                             Please enter your principal amount: Please enter the annual interest rate: Please enter the times compounded per year: Please enter the number of years in the account:  -------------------------------------------- Principal amount======================> 5 Annual interest rate==================> 6 Times compounded per year=============> 7 Years to keep in account==============> 8 -------------------------------------------- Balance after 1 years: 5.95261e+006 -------------------------------------------- Balance after 2 years: 3.54335e+013 -------------------------------------------- Balance after 3 years: 2.10922e+020 -------------------------------------------- Balance after 4 years: 1.25553e+027 -------------------------------------------- Balance after 5 years: 7.4737e+033 -------------------------------------------- Balance after 6 years: 1.#INF -------------------------------------------- Balance after 7 years: 1.#INF -------------------------------------------- Balance after 8 years: 1.#INF -------------------------------------------- -------------------------------------------- Would you like to do another calculation? Enter Y or N:```
(sorry for my bad english)
• 02-07-2005
RP319
Thanks, I tried that and this is my output using doubles instead of floats for getBalance. Seems like the same thing basically. Just not sure, what the issue is. It seems correct to me, but I don't know. :(

Code:

```Program SAVE -------------------------------------------- This program will request that you enter values for:     a principal amount (P)     an annual interest rate (I)     times compounded per year (Q)     years in account (N) The program will then calculate and print the balance for each year up through year N. -------------------------------------------- Please enter your principal amount: 1000 Please enter the annual interest rate: 4.0 Please enter the times compounded per year: 2 Please enter the number of years in the account: 5 -------------------------------------------- Principal amount======================> 1000 Annual interest rate==================> 4 Times compounded per year=============> 2 Years to keep in account==============> 5 -------------------------------------------- Balance after 1 years: 9e+06 -------------------------------------------- Balance after 2 years: 8.1e+13 -------------------------------------------- Balance after 3 years: 7.29e+20 -------------------------------------------- Balance after 4 years: 6.561e+27 -------------------------------------------- Balance after 5 years: 5.9049e+34 -------------------------------------------- -------------------------------------------- Would you like to do another calculation? Enter Y or N:```
• 02-07-2005
Salem
> Annual interest rate==================> 4
This is like 4000% or something, which will grow the values unbelievably fast.

IIRC, a 4% interest rate is written as either 1.04 or 0.04
• 02-07-2005
RP319
Yeah you're right. However the same problems remain when I use what four percent actually would be.

Code:

```Program SAVE -------------------------------------------- This program will request that you enter values for:     a principal amount (P)     an annual interest rate (I)     times compounded per year (Q)     years in account (N) The program will then calculate and print the balance for each year up through year N. -------------------------------------------- Please enter your principal amount: 1000 Please enter the annual interest rate: 0.04 Please enter the times compounded per year: 2 Please enter the number of years in the account: 5 -------------------------------------------- Principal amount======================> 1000 Annual interest rate==================> 0.04 Times compounded per year=============> 2 Years to keep in account==============> 5 -------------------------------------------- Balance after 1 years: 1.0404e+06 -------------------------------------------- Balance after 2 years: 1.08243e+12 -------------------------------------------- Balance after 3 years: 1.12616e+18 -------------------------------------------- Balance after 4 years: 1.17166e+24 -------------------------------------------- Balance after 5 years: 1.21899e+30 -------------------------------------------- -------------------------------------------- Would you like to do another calculation? Enter Y or N:```
• 02-07-2005
Salem
> Balance = P * (1 + I / Q ) ^ (N * Q)

> return (pow (( p*(1+ (i / q))),(n * q)));
How about
return p * pow ( (1+ (i / q))), (n * q)) );
• 02-07-2005
RP319
Won't Compile while using :
return p * pow ( (1+ (i / q))), (n * q)) );

:(
• 02-07-2005
Salem
Just balance the () with the p* outside of the pow.
• 02-07-2005
MadCow257
try taking away the last ), I think that's one too many.
• 02-07-2005
RP319
Still isn't compiling. Not sure at all what else to do.
• 02-07-2005
frrossk
Try this:
Code:

```double getBalance(float p, float i, float q, int n)                    {   return (p* pow ( (1+ (i / q)),(n * q))); }```
It's working OK for me...
• 02-07-2005
RP319
Awesome, Thanks a ton! I knew it was something minor I just couldn't handle sitting there any longer. Needed some outside input. Thanks for all who helped. :cool: