473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. int main(){
  8.     double money = 0.0;
  9.     double interest = 0.0;
  10.     int year = 0;
  11.     int total = 0;
  12.  
  13.     cout << "Initial deposit: " << endl;
  14.     cin >> money;
  15.  
  16.     cout << "Annual interest rate: " << endl;
  17.     cin >> interest;
  18.  
  19.     cout << "Number Of Years: " << endl;
  20.     cin >> year;
  21.  
  22.     do
  23.     {
  24.         money += (money*(interest / 100));
  25.         total++;
  26.  
  27.     } 
  28.     while (total<year);
  29.  
  30.     cout << money << endl;
  31.  
  32.  
  33.     system("pause");
  34. }
  35.  
Thanks guys.
Sep 25 '14 #1

✓ answered by weaknessforcats

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

Expand|Select|Wrap|Line Numbers
  1. currentYear = 1; //start with year 1.
  2.  
  3.     total = money;
  4.  
  5.     do
  6.     {
  7.  
  8.         actualInterest = (total*(interest / 100));
  9.  
  10.         total = total + actualInterest;
  11.  
  12.  
  13.         cout << "Balance Ending " << endl;
  14.         cout << "Year" << currentYear;
  15.         cout << "Balance" << money;
  16.         cout << "Interest" << actualInterest;
  17.         cout << "Balance" << total << endl;
  18.  
  19.         ++currentYear;
  20.  
  21.     } 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.

12 1769
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 26 '14 #2
I don't know. It's not working. Could you show me? Is it like this?
Expand|Select|Wrap|Line Numbers
  1.     do
  2.     {
  3.         money += (money*(interest / 100));
  4.         total++;
  5.  
  6.         {
  7.             cout << "Initial deposit: " << endl;
  8.         cin >> money;
  9.  
  10.         cout << "Annual interest rate: " << endl;
  11.         cin >> interest;
  12.  
  13.         cout << "Number Of Years: " << endl;
  14.         cin >> year;
  15.         }
  16.  
  17.     }
Sep 26 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
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
Sep 26 '14 #4
Expand|Select|Wrap|Line Numbers
  1. do
  2.     {
  3.         cout << "Initial deposit: ";
  4.         cin >> money;
  5.  
  6.  
  7.         cout << "Annual interest rate: ";
  8.         cin >> interest;
  9.  
  10.         cout << "Number Of Years: ";
  11.         cin >> year;
  12.  
  13.         double actualInterest = (money*(interest / 100));
  14.  
  15.         double total = money + actualInterest;
  16.         total++;
  17.  
  18.         while cout << "Year end balance: " << total;
  19.     }
  20. }
  21.  
I'm guessing I am messing up the formula. I'm still confused. sorry for being a bother.
Sep 26 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1.  actualInterest = (total*(interest / 100));
  2.  
  3.  
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.
Sep 26 '14 #6
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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7.  
  8. {
  9.     double money = 0.0;
  10.     double interest = 0.0;
  11.     double actualInterest = 0.0;
  12.     int year = 0;
  13.     double total = 0.0;
  14.  
  15.     cout << "Initial deposit: " << endl;
  16.     cin >> money;
  17.  
  18.     cout << "Annual interest rate: " << endl;
  19.     cin >> interest;
  20.  
  21.     cout << "Number Of Years: " << endl;
  22.     cin >> year;
  23.  
  24.     do
  25.     {
  26.         total = total + actualInterest;
  27.  
  28.         actualInterest = (total*(interest / 100));
  29.  
  30.         cout << "Balance Ending " << endl;
  31.         cout << "Year" << year;
  32.         cout << "Balance" << money;
  33.         cout << "Interest" << actualInterest;
  34.         cout << "Balance" << total << endl;
  35.     }
  36.  
  37.     while (total<year);
  38.  
  39.     for (year = 1; year <= 10; year++)
  40.  
  41.         cout << money << endl;
  42.  
  43.  
  44. }
  45.  
Sep 26 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1.     do
  2.     {
  3.         total = total + actualInterest;
  4.  
  5.         actualInterest = (total*(interest / 100));
  6.  
  7. 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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. int currentyear = 1; //start with year 1.
  2.  
  3. do
  4. {
  5.    cout << currentyear;
  6.  
  7.    ++currentyear;     //for the next cyle
  8. }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
Sep 26 '14 #8
Expand|Select|Wrap|Line Numbers
  1. /********************************************************
  2. //Name: Md Nabil Ahsan
  3. //Course: COSC 1436
  4. //Date: September 27, 2014
  5. //Purpose: The purpose of this program is to print out a table showing the total balances at the end of each year 
  6. // for various interest rates
  7. *********************************************************/
  8.  
  9. #include<iostream>
  10. #include<cmath>
  11. using namespace std;
  12.  
  13.  
  14. int main()
  15.  
  16. {
  17.     double money = 0.0;
  18.     double interest = 0.0;
  19.     double actualInterest = 0.0;
  20.     int year = 0;
  21.     int currentYear;
  22.     double total = 0.0;
  23.  
  24.     cout << "Initial deposit: " << endl;
  25.     cin >> money;
  26.  
  27.     cout << "Annual interest rate: " << endl;
  28.     cin >> interest;
  29.  
  30.     cout << "Number Of Years: " << endl;
  31.     cin >> year;
  32.     {
  33.         int currentYear = 1; //start with year 1.
  34.  
  35.         do
  36.         {
  37.             cout << currentYear;
  38.  
  39.             ++currentYear;     //for the next cyle
  40.         } while (currentYear <= year);
  41.  
  42.         total = money;
  43.     }
  44.     do
  45.     {
  46.  
  47.     double    actualInterest = (total*(interest / 100));
  48.  
  49.     double    total = total + actualInterest;
  50.  
  51.  
  52.         cout << "Balance Ending " << endl;
  53.         cout << "Year" << year;
  54.         cout << "Balance" << money;
  55.         cout << "Interest" << actualInterest;
  56.         cout << "Balance" << total << endl;
  57.     }
  58.  
  59. }
How about now?
Sep 26 '14 #9
weaknessforcats
9,208 Expert Mod 8TB
Nope. You've added a second "do" loop. Use just the one loop:

Expand|Select|Wrap|Line Numbers
  1. currentYear = 1; //start with year 1.
  2.  
  3.     total = money;
  4.  
  5.     do
  6.     {
  7.  
  8.         actualInterest = (total*(interest / 100));
  9.  
  10.         total = total + actualInterest;
  11.  
  12.  
  13.         cout << "Balance Ending " << endl;
  14.         cout << "Year" << currentYear;
  15.         cout << "Balance" << money;
  16.         cout << "Interest" << actualInterest;
  17.         cout << "Balance" << total << endl;
  18.  
  19.         ++currentYear;
  20.  
  21.     } 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.
Sep 26 '14 #10
I cannot thank you enough. It worked!! Now all that is needed is tidying up.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8.  
  9. {
  10.     double money = 0.0;
  11.     double interest = 0.0;
  12.     double actualInterest = 0.0;
  13.     int year = 0;
  14.     int currentYear;
  15.     double total = 0.0;
  16.  
  17.     cout << "Initial deposit: " << endl;
  18.     cin >> money;
  19.  
  20.     cout << "Annual interest rate: " << endl;
  21.     cin >> interest;
  22.  
  23.     cout << "Number Of Years: " << endl;
  24.     cin >> year;
  25.     {
  26.         currentYear = 1; //start with year 1.
  27.  
  28.         total = money;
  29.  
  30.         do
  31.         {
  32.  
  33.             actualInterest = (total*(interest / 100));
  34.  
  35.             total = total + actualInterest;
  36.  
  37.  
  38.             cout << "Balance Ending " << endl;
  39.             cout << "Year" << currentYear << endl;
  40.             cout << "Balance" << money << endl;
  41.             cout << "Interest" << actualInterest << endl;
  42.             cout << "Balance" << total << endl;
  43.  
  44.             ++currentYear;
  45.  
  46.         } while (currentYear <= year);
  47.     }
  48.     system("pause");
  49. }
  50.  
Sep 26 '14 #11
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.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7.  
  8. {
  9.     double money = 0.0;
  10.     double interest = 0.0;
  11.     double actualInterest = 0.0;
  12.     int year = 0;
  13.     int currentYear;
  14.     double total = 0.0;
  15.  
  16.     cout << "Initial deposit: " << endl;
  17.     cin >> money;
  18.  
  19.     cout << "Annual interest rate: " << endl;
  20.     cin >> interest;
  21.  
  22.     cout << "Number Of Years: " << endl;
  23.     cin >> year;
  24.     {
  25.         currentYear = 1; //start with year 1.
  26.  
  27.         total = money;
  28.  
  29.         cout << "Balance Ending " << endl;
  30.  
  31.         cout << "Year    "  << "Balance    " << "Interest    " << "Balance    "<< endl;
  32.  
  33.         do
  34.         {
  35.  
  36.             actualInterest = (total*(interest / 100));
  37.  
  38.             total = total + actualInterest;
  39.  
  40.             cout << currentYear << money << actualInterest << total << endl;
  41.  
  42.             ++currentYear;
  43.  
  44.         } while (currentYear <= year);
  45.     }
  46.     system("pause");
  47. }
  48.  
Sep 27 '14 #12
YESSS. DONE! It was just a simple thing that I was missing in the cout

Expand|Select|Wrap|Line Numbers
  1. cout << currentYear << " " << money << " "<< actualInterest << " " << total << " " << endl;
thank you once again sir.
Sep 27 '14 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: | last post by:
Hello, I have generated an HTML version of a previously text report in our application. Looks great everything fine for viewing. Currently the heading of the report is at the top of the HTML...
18
by: Bruno Baguette | last post by:
Hello, I have to design a table wich will store some action reports. Each report have an ID like this 1/2004, 2/2004, ... and each years, they restart to 1 (1/2004, 1/2005, 1/2006,...). So, I...
11
by: cansnowboard | last post by:
I built a file management database which generates a file number based on the year. i.e. 2006-PRRS-13 The criteria was that each file should be unique. So I took the primary key, added one to...
0
by: engloon | last post by:
Hi guys. I found this code that read a file then print out. The problem that I'm facing is, I have a lot of files to be printed, so I would like to print without showing the print property menu....
1
by: medic3212 | last post by:
Greatings, I am a vbs newbie, about two months of class. I am working on a dispatching program which uses an incrementing call number. The call number is made up of two separate parts, the...
6
by: anguyen | last post by:
Hello everyone, I want to generate a unique ID number for a test number field in the format NTN-YY-XXX where YY is the two digit of the year, and XXX is the sequential number. This number reset to...
3
by: Aaron J | last post by:
I have a database with records assigned to a year (2011, 2010, 2009, etc...) Each year may have up to three records assigned to it. How can I display a table to put each year on its own line...
2
by: YasanthaBandula | last post by:
Hi. i am Yasantha.. I have a Access 2007 database.. This is fields.. Child ID,Child Name, DOB, Admission date, Departure date .. So i want a create query for get count values in .How many Children...
3
by: SDaxx | last post by:
I am new to access and vba and am trying to have a text field (txtFiscalYear) increment by one on October 1 of each year. For example on 9/30/2013 the field is 2013 and then on October 1, 2013 the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.