your help is needed (loops)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

your help is needed (loops)

 
0
  #1
Nov 22nd, 2008
Im having a hard time where to start with this assignment. Obviously I did the easy part now its time for the loop which i just dont get.

heres how the program will run
Enter the initial balance ===> 1000

Enter the number of months to cover: ===> 3
Enter the annual interest rate (decimal format; e.g., 5 not .05) ===> 10
Enter the amount deposited for month 1 ===> 500
Enter the amount withdrawn for month 1 ===> 400
Enter the amount deposited for month 2 ===> 600
Enter the amount withdrawn for month 2 ===> 700
Enter the amount deposited for month 3 ===> 800
Enter the amount withdrawn for month 3 ===> 900

Starting balance: $ 1000.00
Total amount deposited: $ 1900.00
Total amount withdrawn: $ 2000.00
Total interest earned: $ 25.64
Final balance: $ 925.64



so far my code lol
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. int startbalance, months;
  10. float rate;
  11.  
  12. cout <<"Enter the initial balance:";
  13. cin >> startbalance;
  14.  
  15. cout <<"Enter the number of months to cover:";
  16. cin >> months;
  17.  
  18. cout <<"Enter the annual interest rate (decimal format; e.g., 5 not .05:";
  19. cin >> rate;
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. system("pause");
  32. return 0;
  33. }

yeah i know its not much but i can sure use a hand. am i going to use "while" "do" "for" "count++" for this assignment?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: your help is needed (loops)

 
0
  #2
Nov 22nd, 2008
You could probably use any flavor of loop you wish. Generally while loops are used if you aren't certain of the number of times you want to loop whereas for loops are used if you do know the number of times to loop. Do/while forces something to be done at least once.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: your help is needed (loops)

 
0
  #3
Nov 23rd, 2008
alright so i been working on it and have been doing some good progress but Im not that great with getting calculations so im a bit stuck. I need to know how to get the total interest earned

Total interest earned: $ 25.64


that way I can get the final balance.

  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. int numMonths;
  10. double numBalance;
  11. float rate;
  12. double totaldep = 0.0;
  13. double totalwith = 0.0;
  14.  
  15.  
  16.  
  17.  
  18. //starting balance
  19. cout <<"Enter the initial balance:";
  20. cin >> numBalance;
  21. //Number of months
  22. cout <<"Enter the number of months to cover:";
  23. cin >> numMonths;
  24.  
  25. cout <<"Enter the annual interest rate (decimal format; e.g., 5 not .05:";
  26. cin >> rate;
  27.  
  28. for (int month = 1; month <= numMonths; month++)
  29. { double deposited, withdrawn;
  30.  
  31. cout <<"Enter the amount deposited for month" << month << ": ";
  32. cin >> deposited;
  33. cout <<"Enter the amount withdrawn for month" << month << ": ";
  34. cin >> withdrawn;
  35. totaldep += deposited;
  36. totalwith += withdrawn;
  37. }
  38. cout << fixed << showpoint << setprecision(2);
  39. cout <<"Starting balance:" << setw(26) << '$' << numBalance << endl;
  40. cout <<"Total amount deposited:" << setw(20) << '$' << totaldep << endl;
  41. cout <<"Total amount withdrawn:" << setw(20) << '$' << totalwith << endl;
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. system("pause");
  53. return 0;
  54. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: your help is needed (loops)

 
0
  #4
Nov 23rd, 2008
First, you need to learn how to format your code. This will help with following the code -- for you and us. If we can't follow your code, we can't help much.

What is the calculation for calculating the interest for one month? How did you arrive at 25.64? If it was given to you by your instructor, how did he arrive at the value?

And while we're at it, check this out, too. Something you might want to ask your instructor about.
Last edited by WaltP; Nov 23rd, 2008 at 1:41 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: your help is needed (loops)

 
0
  #5
Nov 23rd, 2008
heres a sample run like i posted b4
Enter the initial balance ===> 1000

Enter the number of months to cover: ===> 3
Enter the annual interest rate (decimal format; e.g., 5 not .05) ===> 10
Enter the amount deposited for month 1 ===> 500
Enter the amount withdrawn for month 1 ===> 400
Enter the amount deposited for month 2 ===> 600
Enter the amount withdrawn for month 2 ===> 700
Enter the amount deposited for month 3 ===> 800
Enter the amount withdrawn for month 3 ===> 900

Starting balance: $ 1000.00
Total amount deposited: $ 1900.00
Total amount withdrawn: $ 2000.00
Total interest earned: $ 25.64
Final balance: $ 925.64

Im guessing I have to do some math operationt to calculate the interest rate. but idk what. like rate(interest rate) = something something

i dont even know if rate(interest rate) should be a float.

hopefully you guys get what im saying
Last edited by anbuninja; Nov 23rd, 2008 at 1:51 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: your help is needed (loops)

 
0
  #6
Nov 23rd, 2008
Suppose the annual interest rate is 10%. It means you'll get 10% of whatever amount is stored on an annual basis.
Be it the case you have 100$ deposited for a whole year at 10% interest rate.
  1. int deposit;
  2. float interestRate, interest;
  3. cin >> deposit; //In this case 100
  4. cin >> interestRate; //In this case 10
  5. interestRate /= 100.; //Use 100. not 100, so it would be regarded as a float type
  6. interest = interestRate * depost; //Calculate the interest
  7. deposit += interest; //Add the interest to the total deposit
  8. cout << "Your total interest is " << interest << "." << endl;
  9. cout << "Now the total deposit is " << deposit << "." << endl;
So the calculation you're looking for is just interest rate(as a float value) multiplied by the total deposited amount.

If the amount of deposited money changes every month, you would have to calculate them individually and finally add it up for each month.
Unless ofcourse you only want to give interest on the deposited money at exactly 1 year.
As an example, suppose there's been 1000$ deposited for 11 months, during the 12th month everything is withdrawn, does the person still get interest for having his/her money there for 11 months, or does he/she get nothing?
Last edited by BeyondTheEye; Nov 23rd, 2008 at 8:53 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: your help is needed (loops)

 
0
  #7
Nov 23rd, 2008
im trying but still dont know how he got 25.64 for interest rate

my updated code

  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. int numMonths;
  10. double numBalance;
  11. float interest, interestRate = 100.;
  12. double totaldep = 0.0;
  13. double totalwith = 0.0;
  14.  
  15.  
  16.  
  17.  
  18.  
  19. //starting balance
  20. cout <<"Enter the initial balance:";
  21. cin >> numBalance;
  22. //Number of months
  23. cout <<"Enter the number of months to cover:";
  24. cin >> numMonths;
  25.  
  26. cout <<"Enter the annual interest rate (decimal format; e.g., 5 not .05):";
  27. cin >> interest;
  28.  
  29. for (int month = 1; month <= numMonths; month++)
  30. { int deposited, withdrawn;
  31.  
  32.  
  33. cout <<"Enter the amount deposited for month" << month << ": ";
  34. cin >> deposited;
  35. cout <<"Enter the amount withdrawn for month" << month << ": ";
  36. cin >> withdrawn;
  37. totaldep += deposited;
  38. totalwith += withdrawn;
  39. interest = interestRate * deposited;
  40. deposited += interest;
  41. }
  42.  
  43.  
  44.  
  45.  
  46. cout << fixed << showpoint << setprecision(2);
  47. cout <<"Starting balance:" << setw(26) << '$' << numBalance << endl;
  48. cout <<"Total amount deposited:" << setw(20) << '$' << totaldep << endl;
  49. cout <<"Total amount withdrawn:" << setw(20) << '$' << totalwith << endl;
  50. cout <<"Total interest earned: " << setw(20) << '$' << interest << endl;
  51.  
  52.  
  53.  
  54. system("pause");
  55. return 0;
  56. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 55
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: your help is needed (loops)

 
0
  #8
Nov 23rd, 2008
  1. cin >> interest;
  2. totaldep += deposited;
  3. totalwith += withdrawn;
  4. interest = interestRate * deposited;
  5. deposited += interest;
You're asking the user to input a variable named interest(shouldn't it be interestRate anyway?), but where are you using it?
Using the same values you did, it would be something more like this:
  1. cin >> interest;
  2. totaldep += deposited;
  3. interest /= 100.; //Go from percent value to float
  4. interest *= deposited; //Calculate the actual interest
  5. //interest /= 12; //Supposing the interest is annual, as said before, you would only want to give interest for one month, not an entire year.
  6. deposited += interest;

Ofcourse, if you were to make a loop out of it, you'd have to have seperate variables for interest and interestRate. That is, if you don't want to request the user to input the interest rate every time.
Last edited by BeyondTheEye; Nov 23rd, 2008 at 6:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: your help is needed (loops)

 
0
  #9
Dec 3rd, 2008
till this day i still dont know how to do the calculation for interest to get 25.64

my code

  1. #include <cmath>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. int numMonths;
  9. double numBalance;
  10. float interest;
  11. double totaldep = 0.0;
  12. double totalwith = 0.0;
  13.  
  14.  
  15.  
  16.  
  17.  
  18. //starting balance
  19. cout <<"Enter the initial balance:";
  20. cin >> numBalance;
  21. //Number of months
  22. cout <<"Enter the number of months to cover:";
  23. cin >> numMonths;
  24.  
  25. cout <<"Enter the annual interest rate (decimal format; e.g., 5 not .05):";
  26. cin >> interest;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. for (int month = 1; month <= numMonths; month++)
  33. { float deposited, withdrawn;
  34.  
  35.  
  36. cout <<"Enter the amount deposited for month" << month << ": ";
  37. cin >> deposited;
  38. cout <<"Enter the amount withdrawn for month" << month << ": ";
  39. cin >> withdrawn;
  40. totaldep += deposited;
  41. totalwith += withdrawn;
  42. interest /= 100.; //Go from percent value to float
  43. interest *= deposited;
  44. interest /= 12;
  45. deposited += interest;
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. cout <<"Starting balance:" << fixed << showpoint << setprecision(2)<< setw(26) << '$' << numBalance << endl;
  53. cout <<"Total amount deposited:" << fixed << showpoint << setprecision(2) << setw(20) << '$' << totaldep << endl;
  54. cout <<"Total amount withdrawn:" << fixed << showpoint << setprecision(2) << setw(20) << '$' << totalwith << endl;
  55. cout <<"Total interest earned: " << setw(20) << '$' << interest << endl;
  56.  
  57.  
  58.  
  59. system("pause");
  60. return 0;
  61. }

heres how it should run

Sample Run #1:



Enter the initial balance ===> 1000

Enter the number of months to cover: ===> 3
Enter the annual interest rate (decimal format; e.g., 5 not .05) ===> 10
Enter the amount deposited for month 1 ===> 500
Enter the amount withdrawn for month 1 ===> 400
Enter the amount deposited for month 2 ===> 600
Enter the amount withdrawn for month 2 ===> 700
Enter the amount deposited for month 3 ===> 800
Enter the amount withdrawn for month 3 ===> 900

Starting balance: $ 1000.00
Total amount deposited: $ 1900.00
Total amount withdrawn: $ 2000.00
Total interest earned: $ 25.64
Final balance: $ 925.64

i need help there please!!!! i wan to get that part done so I can almsot be done.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: your help is needed (loops)

 
0
  #10
Dec 4th, 2008
monthlyInterestRate = (interestInput/100)/12;
totaldep += deposited;
totalwith += withdrawn;
balance = balance + deposited - withdrawn;
monthlyInterest = balance * monthlyInterestRate;
totalInterest += monthlyInterest;
balance += monthlyInterest;
Last edited by Lerner; Dec 4th, 2008 at 7:15 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC