Date loop

In the while loop at the bottom I want to get the date to increase. The month is the only part that changes but I need for it to start over at 1 once it gets to 12 without resetting the other variable values. Any help is appreciated.

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
35
36
37
38
39
40
  #include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main()
{
    float prin, air = .1, np = 36, date, pay, mpr, mi, bal;
    int n = 0;

    cout<<"Enter principal amount: \n";
    cout<<"$";
    cin>>bal;

    cout<<"Enter annual interest rate in decimal form: "<<endl;
    cin>>air;

    cout<<"Enter number of payments in months: \n";
    cin>>np;

    mpr = air / np;

    pay = mpr * (bal / (1 - (1 / pow((1 + mpr), np))));

    cout<<"Payment is: "<<pay<<endl;
    cout<<"Payment: Due Date  Number Payment  Principal  Interest  Balance "<<endl;

    while (n < np)
    {
       n++;
       mi = bal * mpr;
       prin = pay - mi;
       bal = bal - prin;
       cout.setf(ios::fixed);
       cout<<setw(10)<<setprecision(2)<<n<<"/5/13"<<setw(7)<<n<<setw(11)<<pay<<"   "<<prin<<"     "<<mi<<"     "<<bal<<endl;//in this line
    }
    return 0;
}
Last edited on
Well if month goes past 12 than you will need to increase year as well, Or you will be going back in time.
1
2
3
4
5
if(month > 12)
{
     month = 1;
     year++;
}
Last edited on
Now I have this but it is an infinite loop since the conditions are never met if np is over twelve.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
while (n < np)
    {
       n++;
       if (n>12)
       {
           n = 1;
           year++;
       }
       mi = bal * mpr;
       prin = pay - mi;
       bal = bal - prin;
       cout.setf(ios::fixed);
       cout<<setw(10)<<setprecision(2)<<n<<"/5/"<<year<<setw(7)<<n<<setw(11)<<pay<<"   "<<prin<<"     "<<mi<<"     "<<bal<<endl;
    }
    return 0;
}
Something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

     int month = 0;
    while (n < np)
    {
       n++;
       month++;
       if (month>12)
       {
           month = 1;
           year++;
       }
       mi = bal * mpr;
       prin = pay - mi;
       bal = bal - prin;
       cout.setf(ios::fixed);
       cout<<setw(10)<<setprecision(2)<<month<<"/5/"<<year<<setw(7)<<n<<setw(11)<<pay<<"   "<<prin<<"     "<<mi<<"     "<<bal<<endl;
    }
    return 0;
}
Thanks, that worked perfectly!
Topic archived. No new replies allowed.