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: -
#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.
Nope. You've added a second "do" loop. Use just the one loop: - 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.
12 1769
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.
I don't know. It's not working. Could you show me? Is it like this? - 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;
-
}
-
-
}
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
- 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.
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: - 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: - 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: - 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.
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 -
#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;
-
-
-
}
-
This 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:
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: - 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
- /********************************************************
-
//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?
Nope. You've added a second "do" loop. Use just the one loop: - 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.
I cannot thank you enough. It worked!! Now all that is needed is tidying up. -
-
#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");
-
}
-
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. -
#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");
-
}
-
YESSS. DONE! It was just a simple thing that I was missing in the cout - cout << currentYear << " " << money << " "<< actualInterest << " " << total << " " << endl;
thank you once again sir.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |