Print out a table showing the total balances at the end of each year for various inte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nabilahsan
    New Member
    • Sep 2014
    • 15

    Print out a table showing the total balances at the end of each year for various inte

    Hey guys. I'm a newbie and this is my assignment for a class day after tomorrow. Since I'm new to this I made the program but the problem is the answer has to be in this form:

    Initial deposit? 10000
    Interest rate? 1.2
    Number of years? 9
    Balances at the end of each year:

    Beginning Ending
    Year Balance Interest Balance
    1 10000.00 110.00 10110.00
    2 10110.00 111.21 10221.21
    3 10221.21 112.43 10333.64
    4 10333.64 113.67 10447.31
    5 10447.31 114.92 10562.23
    6 10562.23 116.18 10678.42
    7 10678.42 117.46 10795.88
    8 10795.88 118.75 10914.64
    9 10914.64 120.06 11034.70
    10 11034.70 121.38 11156.08

    But since we haven't covered the topic on how to print it out in tables it's not necessary. All I have to display is how it is added to each year and is shown at the year end balance.

    Here's my code:
    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    
    
    int main(){
    	double money = 0.0;
    	double interest = 0.0;
    	int year = 0;
    	int total = 0;
    
    	cout << "Initial deposit: " << endl;
    	cin >> money;
    
    	cout << "Annual interest rate: " << endl;
    	cin >> interest;
    
    	cout << "Number Of Years: " << endl;
    	cin >> year;
    	
    	do
    	{
    		money += (money*(interest / 100));
    		total++;
    
    	} 
    	while (total<year);
    
    	cout << money << endl;
    
    
    	system("pause");
    }
    Thanks guys.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    All you have to do is cout your variables inside your "do" loop. Maybe separate the values with a space.

    That will give you the lines in your table.

    Maybe get fancier and cout the column titles just before you enter the "do" loop.

    Comment

    • nabilahsan
      New Member
      • Sep 2014
      • 15

      #3
      I don't know. It's not working. Could you show me? Is it like this?
      Code:
      	do
      	{
      		money += (money*(interest / 100));
      		total++;
      		
      		{
      			cout << "Initial deposit: " << endl;
      		cin >> money;
      
      		cout << "Annual interest rate: " << endl;
      		cin >> interest;
      
      		cout << "Number Of Years: " << endl;
      		cin >> year;
      		}
      
      	}

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        No. It was correct before. cin is for data input. You need the input before the "do" loop.

        Here is your line:

        1 10000.00 110.00 10110.00

        It is a year , a beginning balance, interest, and the ending balance.

        So in the loop you:

        do
        {
        cout the balance. This s your starting balance.

        perform the interest calculaton

        cout the interest

        add the interest to the balance. This is your ending balance.

        cout the balance again.

        }while etc.

        Get this part working. Then post again and we'll work on the year and the column titles.

        BTW: You keep things on the same line by not using endl until after you cout the ending balance

        Comment

        • nabilahsan
          New Member
          • Sep 2014
          • 15

          #5
          Code:
          do
          	{
          		cout << "Initial deposit: ";
          		cin >> money;
          
          
          		cout << "Annual interest rate: ";
          		cin >> interest;
          
          		cout << "Number Of Years: ";
          		cin >> year;
          
          		double actualInterest = (money*(interest / 100));
          
          		double total = money + actualInterest;
          		total++;
          
          		while cout << "Year end balance: " << total;
          	}
          }
          I'm guessing I am messing up the formula. I'm still confused. sorry for being a bother.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Go back and make the code look like your original post. Do not put cin's inside the "do" loop.

            This doesn't work the way you think it does:

            Code:
            double total = money + actualInterest;
            This creates a new variable named total. You don't want that. Instead you want to ADD the interest to the previous total:

            Code:
            total = total + actualInterest;
            Now the total gets bigger by adding the interest and assigning the new value back to total. Can you see that? Now total is ready to add the interest for the next year.

            Next, the actual interest is not calculated on the original money. Rather, you use the total from last time:

            Code:
             actualInterest = (total*(interest / 100));
            That means when you start out what you call money is really the original total.

            The user enters the initial amount. Call it total.

            Next, the interest is the current total multiplied by the interest rate.

            Next, the new total is the current total + the interest from the step above. The new total becomes the current total on the next cycle of the loop. Look at the table display on your first post and you will see what I mean.

            Comment

            • nabilahsan
              New Member
              • Sep 2014
              • 15

              #7
              I was kind of getting it right. But when I'm debugging it, it screws up. But I think I'm getting there. I can see you have been teaching C++ for 20 years! That's amazing. Can you recommend something for a newbie like me? You know, what to read and reference websites.
              Here's my code

              Code:
              #include<iostream>
              #include<cmath>
              using namespace std;
              
              
              int main()
              
              {
              	double money = 0.0;
              	double interest = 0.0;
              	double actualInterest = 0.0;
              	int year = 0;
              	double total = 0.0;
              
              	cout << "Initial deposit: " << endl;
              	cin >> money;
              
              	cout << "Annual interest rate: " << endl;
              	cin >> interest;
              
              	cout << "Number Of Years: " << endl;
              	cin >> year;
              
              	do
              	{
              		total = total + actualInterest;
              		
              		actualInterest = (total*(interest / 100));
              
              		cout << "Balance Ending " << endl;
              		cout << "Year" << year;
              		cout << "Balance" << money;
              		cout << "Interest" << actualInterest;
              		cout << "Balance" << total << endl;
              	}
              	
              	while (total<year);
              	
              	for (year = 1; year <= 10; year++)
              	
              		cout << money << endl;
              
              
              }

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                This code:
                Code:
                	do
                	{
                		total = total + actualInterest;
                		
                		actualInterest = (total*(interest / 100));
                
                etc...
                shows the total being updated before the interest is calculated. Probably these lines need to be reversed.

                You also try to calculate the interest rate. So if you enter 5, you want to end up with .05 for the rate which is why you divide the interest by 100. However, interest is an int and so is 100 giving 5/100. The compiler uses integer division here (no decimals). How many 100's are there in 5? Like 0.

                I would make interest a double and divide by 100.0 (constants like 100.0 are double by default).

                Next, the user enters the amount into money but money never makes it to the total before the loop. Before the loop all you need is:

                Code:
                total = money;
                That means you don't need the "for" loop at the end of the program.

                Next, why does the "do" loop run until total < year??? total is money and year cannot be converted to money. This is an apples and oranges thing. What you want is to start with year 1 and run for the number of years the user entered. Thia means you need a loop counter to count each cycle of the loop:

                Code:
                int currentyear = 1; //start with year 1.
                
                do
                {
                   cout << currentyear;
                
                   ++currentyear;     //for the next cyle
                }while(currentyear <= year)
                If the user entered 10 for year,the loop will start with currentyear of 1 and run until currentyear is 10 and quit.

                Keep posting. You are progressing.

                As to learning C++, it just takes a long time and we have all gone through what you are going through now. You would expect 1 or 2 years to learn Spanish or French which are just other languages with syntax and grammar rules like C++. So your C++ proficiency will take some time. The key is to not give up

                Comment

                • nabilahsan
                  New Member
                  • Sep 2014
                  • 15

                  #9
                  Code:
                  /********************************************************
                  //Name: Md Nabil Ahsan
                  //Course: COSC 1436
                  //Date: September 27, 2014
                  //Purpose: The purpose of this program is to print out a table showing the total balances at the end of each year 
                  // for various interest rates
                  *********************************************************/
                  
                  #include<iostream>
                  #include<cmath>
                  using namespace std;
                  
                  
                  int main()
                  
                  {
                  	double money = 0.0;
                  	double interest = 0.0;
                  	double actualInterest = 0.0;
                  	int year = 0;
                  	int currentYear;
                  	double total = 0.0;
                  
                  	cout << "Initial deposit: " << endl;
                  	cin >> money;
                  
                  	cout << "Annual interest rate: " << endl;
                  	cin >> interest;
                  
                  	cout << "Number Of Years: " << endl;
                  	cin >> year;
                  	{
                  		int currentYear = 1; //start with year 1.
                  
                  		do
                  		{
                  			cout << currentYear;
                  
                  			++currentYear;     //for the next cyle
                  		} while (currentYear <= year);
                  
                  		total = money;
                  	}
                  	do
                  	{
                  
                  	double	actualInterest = (total*(interest / 100));
                  
                  	double	total = total + actualInterest;
                  
                  
                  		cout << "Balance Ending " << endl;
                  		cout << "Year" << year;
                  		cout << "Balance" << money;
                  		cout << "Interest" << actualInterest;
                  		cout << "Balance" << total << endl;
                  	}
                  	
                  }
                  How about now?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Nope. You've added a second "do" loop. Use just the one loop:

                    Code:
                    currentYear = 1; //start with year 1.
                    
                    	total = money;
                    
                    	do
                    	{
                    
                    		actualInterest = (total*(interest / 100));
                    
                    		total = total + actualInterest;
                    
                    
                    		cout << "Balance Ending " << endl;
                    		cout << "Year" << currentYear;
                    		cout << "Balance" << money;
                    		cout << "Interest" << actualInterest;
                    		cout << "Balance" << total << endl;
                    
                    		++currentYear;
                    
                    	} while (currentYear <= year);
                    Notice I removed the double from actualInterest and total inside the loop.

                    C++ has a feature called scope. You will hear about this a lot. For now, scope can be the area between braces, like the braces of your "do" loop. If you use the word double in front of, say total, a new variable is created and used until the code reaches the closing brace }. That's a different total from the one defined at the beginning of main(). In this program you want only one varable for both total and actualInterest.

                    Finally, you want to display currentYear inside the loop. That way the lines of your display will be numbered 1,2,3,4,5.

                    Once you get your right answers you can tidy up your lines of display but I would leave that to the very end.

                    Comment

                    • nabilahsan
                      New Member
                      • Sep 2014
                      • 15

                      #11
                      I cannot thank you enough. It worked!! Now all that is needed is tidying up.

                      Code:
                      #include<iostream>
                      #include<cmath>
                      using namespace std;
                      
                      
                      int main()
                      
                      {
                      	double money = 0.0;
                      	double interest = 0.0;
                      	double actualInterest = 0.0;
                      	int year = 0;
                      	int currentYear;
                      	double total = 0.0;
                      
                      	cout << "Initial deposit: " << endl;
                      	cin >> money;
                      
                      	cout << "Annual interest rate: " << endl;
                      	cin >> interest;
                      
                      	cout << "Number Of Years: " << endl;
                      	cin >> year;
                      	{
                      		currentYear = 1; //start with year 1.
                      
                      		total = money;
                      
                      		do
                      		{
                      
                      			actualInterest = (total*(interest / 100));
                      
                      			total = total + actualInterest;
                      
                      
                      			cout << "Balance Ending " << endl;
                      			cout << "Year" << currentYear << endl;
                      			cout << "Balance" << money << endl;
                      			cout << "Interest" << actualInterest << endl;
                      			cout << "Balance" << total << endl;
                      
                      			++currentYear;
                      
                      		} while (currentYear <= year);
                      	}
                      	system("pause");
                      }

                      Comment

                      • nabilahsan
                        New Member
                        • Sep 2014
                        • 15

                        #12
                        Okay so I edited a little bit. The only problem that remains is making sure all the numbers in a line are orderly spaced in columns.

                        Code:
                        #include<iostream>
                        #include<cmath>
                        using namespace std;
                        
                        
                        int main()
                        
                        {
                        	double money = 0.0;
                        	double interest = 0.0;
                        	double actualInterest = 0.0;
                        	int year = 0;
                        	int currentYear;
                        	double total = 0.0;
                        
                        	cout << "Initial deposit: " << endl;
                        	cin >> money;
                        
                        	cout << "Annual interest rate: " << endl;
                        	cin >> interest;
                        
                        	cout << "Number Of Years: " << endl;
                        	cin >> year;
                        	{
                        		currentYear = 1; //start with year 1.
                        
                        		total = money;
                        		
                        		cout << "Balance Ending " << endl;
                        		
                        		cout << "Year    "  << "Balance    " << "Interest    " << "Balance    "<< endl;
                        
                        		do
                        		{
                        
                        			actualInterest = (total*(interest / 100));
                        
                        			total = total + actualInterest;
                        
                        			cout << currentYear << money << actualInterest << total << endl;
                        
                        			++currentYear;
                        
                        		} while (currentYear <= year);
                        	}
                        	system("pause");
                        }

                        Comment

                        • nabilahsan
                          New Member
                          • Sep 2014
                          • 15

                          #13
                          YESSS. DONE! It was just a simple thing that I was missing in the cout

                          Code:
                          cout << currentYear << " " << money << " "<< actualInterest << " " << total << " " << endl;
                          thank you once again sir.

                          Comment

                          Working...