FOR loop stuck help

Solved
Last edited on
Try changing this line:
expenses = avgExpenses * (1+inflation/100);

To:
expenses = avgExpenses * (1+inflation/100.0);

You're doing int division and you'll only end up with a whole number. I believe you want a decimal number to multiply avgExpenses to.

Aside from that, I don't really what your program is supposed to do. Do you have a desired output? What does yours look like?
Last edited on
Thanks for the reply. This is my desired output:

Enter the balance for your minimum sum and savings: 123000 100000
Enter your annual averageexpenses:15000
Enter the inflation percentage:5

YEAR EXPENSES END-OF-YEAR BALANCE
---- ------------ -------------
1 15000.00 208000.00
2 15750.00 192250.00
3 16537.50 175712.50


After entering the values, the table will be the output.
From year 1 to 20.


But this is the output im getting:

YEAR EXPENSES END-OF-YEAR BALANCE
---- ------------ -------------
1 15000.00 208000.00
2 15000.00 208000.00
3 15000.00 208000.00
4 15000.00 208000.00

The average Expenses should be different every year.

This is a sample calculation:

Year 2,

average expenses = 15000 * (1+ 5/100)
= 15750 -------- THIS IS EXPENSES FOR YEAR 2

Therefore, End of Year Balance = 208000 - 15750
= 192250 ---THIS ISEND-OF-YEAR BALANCE

Year 3,

average expenses = 15750 * (1+ 5/100)
= 16537.50 -------- THIS IS EXPENSES FOR YEAR 2

Therefore, End of Year Balance = 192250 - 16537.50
= 175712.50 ---THIS ISEND-OF-YEAR BALANCE

So basically, the code is stuck at calculating for year 1 for 20 years.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
   int avgExpenses, inflation, year, minsum, savings;
   double expenses, endBalance, total;

   cout << "Enter the balance for your minimum sum and savings:";
   cin >> minsum >> savings ;
   cout << "Enter your annual average expenses:";
   cin >> avgExpenses;
   cout << "Enter the inflation percentage:";
   cin >> inflation;

   cout << setw(2) << "YEAR";
   cout << setw(15) << "EXPENSES";
   cout << setw(25) << "END-OF-YEAR BALANCE" << endl;
   cout << setfill('-') << setw(45) << '-' << endl;
   expenses = avgExpenses;
   total = minsum + savings;
   endBalance = total - expenses;
   for (int year = 1; year < 21 ; ++year) {

      cout << setfill(' ') << setprecision(3) << fixed ;
      cout << setw(2) << year ;
      cout << setw(17) << expenses ;
      cout << setw(20) << endBalance << endl;
      expenses *= (1 + inflation / [u]100.0);[/u]
      endBalance -= expenses;
   }
   return 0;
}

Alright, so after doing some minor math, I just moved your variables around. Essentially you were just redefining your variables each year, but to the same values each time. That math can go before the for loop. Now, since you want some other math done each year, that needs to go at the bottom of the for loop to represent the end of year being recalculated.

I also changed the 100 to 100.0 to do double division (return a non whole number value) to make your inflation correct. I underlined all of the changes I made from your original code.

Here is the sample input you gave me and the final output:
Enter the balance for your minimum sum and savings:123000 100000
Enter your annual average expenses:15000
Enter the inflation percentage:5
YEAR       EXPENSES      END-OF-YEAR BALANCE
---------------------------------------------
 1        15000.000          208000.000
 2        15750.000          192250.000
 3        16537.500          175712.500
 4        17364.375          158348.125
 5        18232.594          140115.531
 6        19144.223          120971.308
 7        20101.435          100869.873
 8        21106.506           79763.367
 9        22161.832           57601.535
10        23269.923           34331.612
11        24433.419            9898.193
12        25655.090          -15756.898
13        26937.845          -42694.743
14        28284.737          -70979.480
15        29698.974         -100678.454
16        31183.923         -131862.377
17        32743.119         -164605.495
18        34380.275         -198985.770
19        36099.289         -235085.059
20        37904.253         -272989.312


I hope this is what you wanted.

Edit: I think you might want to setprecision to 2 instead of 3.
Last edited on
You shouldn't delete content from your post like this.
http://www.cplusplus.com/articles/oGLN8vqX/
Topic archived. No new replies allowed.