Question on purchase and change program

This is a discussion on Question on purchase and change program within the C Programming forums, part of the General Programming Boards category; I'm trying to write this program that asks for the purchase amount, the payment amount and then outputs the change ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    Question on purchase and change program

    I'm trying to write this program that asks for the purchase amount, the payment amount and then outputs the change in the number of bills and coins. I think I'm missing an 'if' statement for determining the coins. Can someone point me in the right direction with this? Thanks. Here is the code:

    Code:
    #include <stdio.h>
    
      int main()
    
       {
    
         double p,t;
         int coins,bills,change;
         int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;
    
         char answer [2];
    
        do
    
         {
    
            printf("\nEnter the purchase amount:");
            scanf ("%lf",& p);
    
            printf("\nEnter the payment amount:");
            scanf ("%lf",& t);
    
             change=p-t;
             
             bills=(int) change;
    
              twenties=bills/200;
    
              bills=bills%200;
    
              tens=bills/100;
    
              bills=bills%100;
    
              fives=bills/50;
    
              bills=bills%50;
    
              ones=bills/10;
    
              bills=bills%10;
    
             coins=(int)((change-bills)*100);
    
              quarters=coins/25;
    
              coins=coins%25;
    
              dimes=coins/10;
    
              coins=coins%10;
    
              nickels=coins/5;
    
              coins=coins%5;
    
              pennies=coins;
    
       printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);
    
            printf("\nContinue? (Y/N):");
            scanf ("%s",&answer);
    
         }
    
        while ((answer[0]=='y') || (answer [0]=='Y'));
    
      return 0;
    
       }

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    change=p-t;
    bills=(int) change;

    You have declared change as an int, so the first statement already discards any decimal part on p-t, and the cast is redundant (since both are int). Is this what you want? Also, if I buy something that costs $19 and I pay $20 I expect $1 of change, but the change variable here is -$1; should it be t-p?

    char answer [2];
    scanf ("%s",&answer);

    Hello, buffer overflow. See http://www.eskimo.com/~scs/C-faq/q12.20.html
    hello, internet!

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    That was the question I posted. I'm not sure how to get the change.

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    scanf ("%s",&answer);
    if you want to check a character only then why not
    Code:
    char answer='y';
    scanf ("%c",&answer);
    Code:
    change=p-t;
    After this statement.You should check
    Code:
    if(change<0).
    if it is then put a
    Code:
    continue;
    but remember to initialise answer to 'y' at defination its itself
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    try this
    Code:
    #include <stdio.h>
    
    int main() {
       double p,t,change;
       int coins,bills;
       int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;
       char answer;
    
       do {
          printf("\nEnter the purchase amount:");
          scanf ("%lf", & p);
          
          printf("\nEnter the payment amount:");
          scanf ("%lf", & t);
    
          change = t - p;
          printf("\nyour change is : %lf\n", change );         
          bills = (int)change; 
          coins=(int)((change-(double)bills)*100);
    
          printf("that is %d $ in bills and %d cents in coins\n", bills, coins );	  
          
          twenties = bills/20;
          bills -= twenties * 20;
          tens = bills/10;
          bills -= tens * 10;
          fives = bills/5;
          bills -= fives * 5;
          ones=bills;
    
          quarters = coins/25;
          coins -= quarters*25;
          dimes = coins/10;
          coins -= dimes * 10;
          nickels = coins/5;
          coins -= nickels*5;
          pennies = coins;
    
          printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);
          
          printf("\nContinue? (Y/N):");
          scanf ("%c",&answer);
    
       } while ((answer=='y') || (answer =='Y'));
       return 0;
    }
    Kurt

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Thank you. One small issue. Now I get negative numbers in my printf statement at the end. If I put total as say 20.00 and payment at 12.00, I get "-1 fives and -3 ones." They are all set to %d (integers). How can I get rid of the negatives? Here is my latest code:

    Code:
    #include <stdio.h>
    
      int main()
    
       {
    
         double p,t;
         int coins,bills,change;
         int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;
    
         char answer [2];
    
        do
    
         {
    
            printf("\nEnter the purchase amount:");
            scanf ("%lf",& p);
    
            printf("\nEnter the payment amount:");
            scanf ("%lf",& t);
    
             change=t-p;
             
             bills=(int) change;
    
             coins=(int)((change-(double)bills)*100);
    
              twenties=bills/20;
    
              bills=bills%20;
    
              tens=bills/10;
    
              bills=bills%10;
    
              fives=bills/5;
    
              bills=bills%5;
    
              ones=bills/1;
    
              bills=bills%1;
    
     
    
              quarters=coins/25;
    
              coins=coins%25;
    
              dimes=coins/10;
    
              coins=coins%10;
    
              nickels=coins/5;
    
              coins=coins%5;
    
              pennies=coins;
    
       printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);
    
            printf("\nContinue? (Y/N):");
            scanf ("%s",&answer);
    
         }
    
        while ((answer[0]=='y') || (answer [0]=='Y'));
    
      return 0;
    
       }

  7. #7
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    You can make your own absolute function.
    absolute function is
    if (x<0)
    x=(-1)*x
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can do that one better by simply subtracting it from zero.
    Code:
    if( x < 0 )
        x = 0 - x;

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Ok...I got rid of the negatives but now I'm not getting the number of coins. For example, if I put 50.25 for purchase and 20.21 for payment, I get only the bills, 1 twenty and 1 ten but no change.

    Code:
    #include <stdio.h>
    
      int main()
    
       {
    
         double p,t;
         int coins,bills,change;
         int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;
    
         char answer [2];
    
        do
    
         {
    
            printf("\nEnter the purchase amount:");
            scanf ("%lf",& p);
    
            printf("\nEnter the payment amount:");
            scanf ("%lf",& t);
    
             change=t-p;
    
           if (change<0)
    
              change=0-change;       
             
              bills=(int) change;
    
              coins=(int)((change-(double)bills)*100);        
    
              twenties=bills/20;
    
              bills=bills%20;
    
              tens=bills/10;
    
              bills=bills%10;
    
              fives=bills/5;
    
              bills=bills%5;
    
              ones=bills/1;
    
              bills=bills%1;
    
    
              quarters=coins/25;
    
              coins=coins%25;
    
              dimes=coins/10;
    
              coins=coins%10;
    
              nickels=coins/5;
    
              coins=coins%5;
    
              pennies=coins;
    
       printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);
    
            printf("\nContinue? (Y/N):");
            scanf ("%s",&answer);
    
         }
    
        while ((answer[0]=='y') || (answer [0]=='Y'));
    
      return 0;
    
       }

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>if I put 50.25 for purchase and 20.21 for payment, I get only the bills, 1 twenty and 1 ten but no change
    nothing about change is shown or they are zero ?
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    This thread covers the dollars and cents problem in detail

    Dollars and cents

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to calculate change return.
    By OrAnGeWorX in forum C Programming
    Replies: 15
    Last Post: 11-17-2008, 08:49 AM
  2. type casting?
    By greatonesv in forum C Programming
    Replies: 12
    Last Post: 10-22-2008, 08:21 PM
  3. Question..Help..Real Number Object ?
    By Meth in forum C++ Programming
    Replies: 5
    Last Post: 10-01-2005, 10:11 PM
  4. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  5. help with calculating change from a dollar
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2002, 03:58 PM

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