# Lost again looping

• 09-22-2004
dAzed
Lost again looping
im writing a program that should loop for the number of months that the user inputs

this kinda works but was trying to keep looping for the number of months entered if a user inputs 4 months that the account has been open it should loop 4 times asking the following:

a. the amount deposited ie i enter 10 and want to be able to enter another depost during the month ie 1350.00, 323.49, etc
hmmm?

b. how do I not accept negative numbers for this account? hmm...

c. ditto for the amount withdrawn

d. and I aslo would like to be able to break out of the break out of the loop and close the account if balance = 0 anytime during it execution.

here is what I have so far.....

Code:

``` #include <iostream> using namespace std; int main() /*function main*/ { float bal = 0; float rate = 0; float intearned = 0; int months = 0; int deposit = 0; int withdrawal= 0; cout <<"Please enter the your balance  \$: (-1 to stop) ";   cin >> bal; cout <<"What is the interest rate? (-1 to stop) ";   cin >> rate;   cout <<"How many months has the account been established (-1 to stop) ";   cin >> months;         while (months >= 1) {        cout <<"Please enter any deposits this month  \$: (-1 to stop) ";         cin >> deposit;         bal += deposit;                        bal += (rate) * months/12;         intearned = (rate * months/12);         months++;                 cout <<"Please enter any withdrawals this month  \$: (-1 to stop) ";         cin >> withdrawal;         bal -= withdrawal;        //        if (withdrawal = -1) //                if (deposit = -1)                 break; }                 cout <<'\n';     cout << "The balance is " << bal <<  endl; //print output     cout << "The total deposits are: " << deposit << endl;     cout << "The total withdrawals are: " << withdrawal << endl;     cout << "The total interest earned is : " << intearned << endl;                 return 0; }//end main```
any help would be greatly appreciated
• 09-22-2004
axon
b and c, you can't really not accept some input...in the a real world situation you can't just not accept the letter 'Z' for example... you have to do some error checking instead...if an unwanted input occurs, ask again with specific directions.

d) at the start of the loop you could always check if ballance == 0, if( balance == 0 ) break;

I don't understand what you're asking in a)
• 09-22-2004
PJYelton
I think you mean to decrement months in the while loop, not increment. Change months++ to months--.
• 09-22-2004
dAzed
i guess im

im trying to keep looping the amount deposited more than one deposit during the month

i.e

323.54
1241.98
123.45

hmm.... :confused:
• 09-22-2004
DeepFyre
You really are dazed arent you?
So am I, but not that dazed, yet.
• 09-22-2004
use nested loops. Have the outer loop control which month you're in and have the inner loop control all transactions for any month. If any transaction results in a balance of 0 or less you can set flags for both loops to break out of the loops.
• 09-22-2004
dAzed
im so lost i could use an example
beer me.... this is what i have still wont work bummer

Code:

```        while (months >= 1) {                months--;                 while (months > 0) {                cout <<"Please enter any deposits this month  \$: (-1 to stop) ";                 cin >> deposit;                 bal += deposit;                                bal += (rate) * months/12;                 intearned = (rate * months/12);                         if (bal = 0)                                 break;                 cout <<"Please enter any withdrawals this month  \$: (-1 to stop) ";         cin >> withdrawal;         bal -= withdrawal;                if (months = -1)                 break;```
• 09-22-2004
dAzed
its homework its my third program so I am kinda dumfounded

should i make functions in the program etc and call them etc???

I have a week yipee!!

im suppose to write a program that calculates the balance of a savings
account at the end of a period of time. It should ask the user for the annual interest rate ( was thinking obout a pow math.h thing for this)
the starting balance, and the number of months that have passed since the account was etablished.

A loop should then run once for every month that the user entered above and:

a. ask the user for mutiple deposits during that month

b. ask the user for mutiple withdrawals during the month

c. calulate the interest

print out the the results

any suggestions are extremly helpful thank u for looking at this thread
• 09-22-2004
Vicious
Seriously, if you dont have a good idea of what needs to be done it will be hard to do.

So, get a piece of paper and litterally write out how you would do it on paper, then start thinking how you could put it into code.