Swapping a nested For statement with a nested While statement

I have a homework problem from my Intro to C++ class that I cannot get my head around. I am given a specific IPO chart and code that works with a nested For statement. I am to swap the For statement with a nested While statement. The output should read:

Year 1:
Rate 3%: $1030.00
Rate 4%: $1040.00
Rate 5%: $1050.00
Year 2:
Rate 3%: $1060.90
Rate 4%: $1081.60
Rate 5%: $1102.50
Year 3: on through year 5

The original code looks like this:

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    int principal = 1000;
    int years = 1;         //counter
    double balance = 0.0;
        
    do     //begin loop
    {
           cout << "Year " << years << ":" << endl;
           
           for (double rate = .03; rate < .06; rate += .01)
           {
               balance = principal * pow(1 + rate, years);
               //display rate with zero decimal places
               cout << fixed << setprecision (0);
               cout << "     Rate " << rate * 100 << "%: $";
               //display balance with two decimal places
               cout << setprecision(2) << balance << endl;
           }   //end while
           
           years += 1;    //update years counter
     }     while (years < 6);
         
    system("pause");
    return 0;
}   //end of main function 


I have tried various ways to swap in a while statement. My first thought was to simply move the variable declaration statement out of the for statement and up with the other declaration statements. I then overwrote the For statement with
while (rate < .06). The code looks like this:

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    int principal = 1000;
    int years = 1;         //counter
    double balance = 0.0;
    double rate = .03;
        
    do     //begin loop
    {
           cout << "Year " << years << ":" << endl;
           
           while (rate < .06)
           {
               balance = principal * pow(1 + rate, years);
               //display rate with zero decimal places
               cout << fixed << setprecision (0);
               cout << "     Rate " << rate * 100 << "%: $";
               //display balance with two decimal places
               cout << setprecision(2) << balance << endl;
               rate += .01;
           }   //end while
           years += 1;    //update years counter
     }     while (years < 6);
         
    system("pause");
    return 0;
}   //end of main function 


This gives the proper Year 1, Year 2, Year 3, Year 4 and Year 5 but only returns interest rates for Year 1, years 2 - 5 are blank. I completely changed everything for double while statements, that didn't work at all. I tried changing to pretest loops for both while statements as well. The best I can get is the code above. Can someone point me in the right direction here? I'm tired of bashing my head into my desk and cannot afford to buy a new desk right now. Thanks
Last edited on
Instead of using a while statement in the nested while statement,you should juss use a single nested while,it'd everything,but i'm gonna start from the double rate=0.3
1
2
3
4
5
6
7
8
9
double rate = 0.3;
do
{
cout<<"Years\t";
//or you use the setw(int_val) here instead of the tab i used
/* write the calculations here and cout the calculations to the console screen,then make the increment thus*/
rate+=0.1;
}
while (rate < 0.6);
Last edited on
So I should try:

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    int principal = 1000;
    int years = 1;         //counter
    double balance = 0.0;
    double rate = .03;
        
    do     //begin loop
    {
           cout << "Year " << years << ":" << endl;
          {
               balance = principal * pow(1 + rate, years);
               //display rate with zero decimal places
               cout << fixed << setprecision (0);
               cout << "     Rate " << rate * 100 << "%: $";
               //display balance with two decimal places
               cout << setprecision(2) << balance << endl;
               rate += .01;
           }   //end while
           years += 1;    //update years counter
     }     while (rate < .06);
         
    system("pause");
    return 0;
}   //end of main function   


Can you explain the cout << "Years\t"; statement? I haven't seen that before. Is it just a shorthand version of what I already have? If it was that simple I might just have to slap myself......thx!
Topic archived. No new replies allowed.