How do I fix this to make the program work?

This is a discussion on How do I fix this to make the program work? within the C Programming forums, part of the General Programming Boards category; So it's supposed to scan xx.xx euro and if the money is less than the person has to pay, it ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    69

    How do I fix this to make the program work?

    So it's supposed to scan xx.xx euro and if the money is less than the person has to pay, it prints a message asking them to pay <the remaining> more. The code is:

    Code:
    handed_in = 0; 
    remaining = pay;
    
    while (handed_in < pay) {
                scanf("%lf", &handed_in);
                remaining = remaining - handed_in;
                if (remaining > 0) {
                    printf("PAY %5.2lf more \n", remaining);
                }
            }
    After this, I'm covering the case according to which the person has paid more money than owed, in which case the change is provided.

    THE PROBLEM IS: if I pay less the first time, and when asked to pay more, I pay MORE than asked, the program stops scanning and nothing happens. But if I do pay more the first time, things work OK.

    So how do I go about this? Replying would mean the world to me.


    example:
    It says PAY 8.40.
    I enter 3.
    It says PAY 5.40 more.
    I enter 3.
    It says PAY 2.40 more.
    I enter 3.
    And nothing happens next. :/
    Last edited by Xpl0ReRChR; 11-03-2011 at 09:02 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,325
    You need to keep a running total of the amount handed in and compare that with pay, or check if remaining > 0 to control your loop. Right now, handed_in only tracks the amount handed in on that one payment.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Update: The following morning I just woke up and finished the algorithm, before even reading your answer. Looks like I could have used an 8-hour sleep before asking. You were obviously correct and thank you for replying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Correction needed to make the program work?
    By ljgerr93 in forum C Programming
    Replies: 4
    Last Post: 10-17-2011, 12:59 PM
  2. How to make the following program work?
    By lijr07 in forum C Programming
    Replies: 2
    Last Post: 06-24-2011, 09:23 AM
  3. How can I make this program work? Problem with arrays
    By babe20042004 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2009, 06:14 PM
  4. Can't make this work
    By BlahBlahBlah in forum C++ Programming
    Replies: 11
    Last Post: 11-28-2002, 02:45 AM
  5. Replies: 2
    Last Post: 03-25-2002, 04:49 AM

Tags for this Thread


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