Thread: While statement

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    12

    While statement

    My program is working but is not displaying the correct results. It is displaying 5% instead of 2, 3, and 4 % for each year. If someone could tell me what I am doing incorrectly that would be fantastic.

    Thanks in advance.


    Here is my code

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    int main()
    {
        int principal = 1000;
        int years = 1;    //counter
        double balance = 0.0;
        double rate = 0.02;
            
        do //begin loop
        {
    while (rate < .05)
    rate = rate +.01;        
                
    
    
    
    
    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;
    
    
            }  //end for
    
    
        //years counter
    years += 1;
    }while (years < 6);
    
    
         
    
    
        //system("pause")
        return 0;
    } //end of main function



    C++ code - 41 lines - codepad

  2. #2
    Guest
    Guest
    Come on man, your code is all over the place. Edit your post and indent it properly, it's common courtesy and will increase your chances of getting help.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it is not a courtesy in this case - it is essential to catch the problem

    for example there is no such thing as for loop as comments suggest in this code
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    int main()
    {
        int principal = 1000;
        int years = 1;    //counter
        double balance = 0.0;
        double rate = 0.02;
    
        do //begin loop
        {
            while (rate < .05)
                rate = rate +.01;
    
            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;
            }  //end for
            //years counter
            years += 1;
        }
        while (years < 6);
    
        //system("pause")
        return 0;
    } //end of main function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    12
    The incorrect code uploaded. I was in the middle of switching the statements. I did tweak parts of the rate. It now displays the years, but it only displays 3, 4 and 5 % and it is only displaying on year 1.

    It should display
    Year 1 through 5 with the rate %'s 2,3, and 4

    This is the code.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    int main()
    {
        int principal = 1000;
        int years = 1;    //counter
        double balance = 0.0;
        double rate = 0.02;
     
        do //begin loop
    
        {cout << "Year" << years << ":" << endl;
    
    
    
            while (rate < .05)
             { rate = rate +.01;
    
    
        //display rate with zero decimal places
                cout << fixed << setprecision(0);
                cout << "  Rate" << rate * 100 << "%: $";
    
            //display balance with two decimal places
                balance = principal * pow(1 + rate, years);
                cout << setprecision(2) << balance << endl;
              
            } // end while 
    //years counter
    years += 1;
        }
    while (years < 6); 
        //system("pause")
        return 0;}
     //end of main function
    
    

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you do not want to help yourself - why do you think other will help you?
    you still put the code without formatting - find some autofarmating tool and use it.

    when you know exactly the range of variable should go in the loop use for

    your task asks to use 2 nested for loops.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. If statement being ignored
    By Nathaneil123 in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2010, 07:48 PM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. What does this statement mean?
    By starcatcher in forum C Programming
    Replies: 3
    Last Post: 02-03-2008, 10:55 AM
  5. Help with if statement
    By viryonia in forum C Programming
    Replies: 1
    Last Post: 02-11-2003, 09:52 PM

Tags for this Thread