Lost again looping

This is a discussion on Lost again looping within the C++ Programming forums, part of the General Programming Boards category; im writing a program that should loop for the number of months that the user inputs this kinda works but ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    29

    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

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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)

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I think you mean to decrement months in the while loop, not increment. Change months++ to months--.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    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....

  5. #5
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    You really are dazed arent you?
    So am I, but not that dazed, yet.
    Keyboard Not Found! Press any key to continue. . .

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    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;

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    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

  9. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I lost my 1 while looping....
    By mkimbrell in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2006, 09:28 AM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21