Thread: Is this possible

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Is this possible

    I have my program running just about the way I want it to but can't work out some final details.

    Basically I want to run this loop while the acc does not = 9999

    Code:
    //loop to gather account information
    while(acc != 9999){//run loop while account number does not = 9999
    acc = accno();
    //break loop if acc = 9999
    if(acc == 9999)
    {
        break;
    }
    //count account entries
    bal = balance(); // balance function
    pur = purchase(); // purchase function
    pay = payment(); //payment function
    cr = credit(); //credit function 
    
    
    interim = iendbal( bal, pur, pay, cr);
    
    interest = accint(interim); 
    
    final = fendbal(interest, interim); 
    
    printfun(acc,bal,pur,pay,cr,interest,final); 
    
    totfeb = totfeb + final;
    }
    But acc can only be between 1000 - 4000. How can I write another if statement to loop back to prompting for acc if the account number doesn't meet these conditions.

    Edit: added a little more code for clarification.
    Last edited by idfjvafnv; 10-09-2011 at 02:27 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok so, we can't see the closing brace, where does the loop end? And what is accno() doing...

    I can foresee all kinds of problems...
    For example: getting into the loop and acc never ending up at 9999 creating an infinite loop...

    If your accno can only be in the range 1000 to 4000 ... your loop will never end. You need to find a better way of doing this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    I added a bit more hopefully to clarify a bit.

    acc will eventually = 9999 because that it how the user will let the program know it is done entering information.

    accno() is just asking for a number then returning the entered value, but I want it the value to only be able to be between 1000 and 4000 and if not I want it to ask for the number again until it is between 1000 and 4000

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Oh my, you've programmed yourself into a very interesting corner here...

    The right place to limit the range is in the accno() function where it's easy to loop until you've got what you want.
    The loop however needs out of range values to exit.

    You could add logic in your accno() function to the effect that
    Code:
     if((acc != 9999) && (acc <1000 || acc > 4000))
       ask_again;
    That would allow the 9999 to "leak" out for the loop closure.

    But still... I would strongly recommend you find a better way of doing this... Way too much to go wrong here...

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    when you say ask_again; what could I use to make that loop. I just wanna try that then I'll try and go back and redo it in a better way

Popular pages Recent additions subscribe to a feed