Simple Program For Converting Change to Dollars

This is a discussion on Simple Program For Converting Change to Dollars within the C Programming forums, part of the General Programming Boards category; All right, I was given an assignment to take the number of pennies, nickels, dimes, quarters, and half dollars from ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Simple Program For Converting Change to Dollars

    All right, I was given an assignment to take the number of pennies, nickels, dimes, quarters, and half dollars from the user and then make a program to count the number of coins, how many cents it adds up to (i.e. 450 cents), and then convert it to dollars and cents (i.e. 4 dollars and 50 cents). I have the coins part working, and now I'm trying to get it to add up to the cents and it just isn't working... Here's what I have so far:

    Code:
     #include <stdio.h>
       #include <stdlib.h>
       #define pennies_per_dollar 1 
       #define nickels_per_dollar 5 
       #define dimes_per_dollar 10 
       #define quarters_per_dollar 25 
       #define halfdollars_per_dollar 50
       
       
       main()
       
       {
             int pennies, nickels, dimes, quarters, halfdollars, total_coins, total_cents; 
             int pennies2, nickels2, dimes2, quarters2, halfdollars2;
             
             
             printf("How many pennies do you have? ");
             scanf("%d", &pennies);
             
             printf("How many nickels do you have? ");
             scanf("%d", &nickels);
             
             printf("How many dimes do you have? ");
             scanf("%d", &dimes);
             
             printf("How many quarters do you have? ");
             scanf("%d", &quarters);
             
             printf("How many half dollars do you have? ");
             scanf("%d", &halfdollars);
             
             total_coins = pennies + nickels + dimes + quarters + halfdollars;
             
             pennies2 = pennies * pennies_per_dollar / 100;
             nickels2 = nickels * nickels_per_dollar / 100;
             dimes2 = dimes * dimes_per_dollar / 100;
             quarters2 = quarters * quarters_per_dollar / 100;
             halfdollars2 = halfdollars * halfdollars_per_dollar / 100;
             
             total_cents = pennies2 + nickels2 + dimes2 + quarters2 + halfdollars2;
             
             printf("\n");
             printf("Your total number of coins is: %d\n", total_coins);
             
             printf("You have %d cents.", total_cents);
             
             
             system("PAUSE");
             
       }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The logic is askew -

    pennies per dollar is *one*?? try 100
    half dollar per dollar is 50?? try 2.

    etc.

    It's easiest if you just convert everything to pennies, and add things up from there.

    There's no need at all for pennies2, nickels2, etc. Best to just lose them. We're not building piano's here.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Haha, I will take it out, I have just been getting desperate and adding and adding more things that really shouldn't be there I believe. I appreciate it, I will let you know what it spits out.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Hrm, the program is still only spitting out 1-3 cents everytime it runs, and this is what I have changed, I'm not sure what's wrong still..

    Code:
     #include <stdio.h>
       #include <stdlib.h>
       #define pennies_per_dollar 100
       #define nickels_per_dollar 20
       #define dimes_per_dollar 10
       #define quarters_per_dollar 4 
       #define halfdollars_per_dollar 2
       
       
       main()
       
       {
             int pennies, nickels, dimes, quarters, halfdollars, total_coins, total_cents; 
             
             printf("How many pennies do you have? ");
             scanf("%d", &pennies);
             
             printf("How many nickels do you have? ");
             scanf("%d", &nickels);
             
             printf("How many dimes do you have? ");
             scanf("%d", &dimes);
             
             printf("How many quarters do you have? ");
             scanf("%d", &quarters);
             
             printf("How many half dollars do you have? ");
             scanf("%d", &halfdollars);
             
             total_coins = pennies + nickels + dimes + quarters + halfdollars;
             
             total_cents = (pennies / pennies_per_dollar) + (nickels / nickels_per_dollar) + (dimes / dimes_per_dollar) + (quarters / quarters_per_dollar) + (halfdollars / halfdollars_per_dollar);
             
             printf("\n");
             printf("Your total number of coins is: %d\n", total_coins);
             
             printf("You have %d cents.", total_cents);
             
             
             system("PAUSE");
             
       }

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    total_cents = (pennies / pennies_per_dollar) + (nickels / nickels_per_dollar) + (dimes / dimes_per_dollar) + (quarters / quarters_per_dollar) + (halfdollars / halfdollars_per_dollar);
    Wrong way around. To find the total cents you want to multiply not divide.

    For example given a dollar is 100 cents and I have 2 dollars, that's 100 x 2 cents not 2 / 100 cents is it?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    I had just noticed that after I posted it, I actually am getting somewhere now, but whatever I put in I only get out 1236 cents? So I've switched it to multiplication but it is still just a tad awry...so frustrating.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Code:
    total_cents = ((pennies * pennies_per_dollar) / 100) + ((nickels * nickels_per_dollar) / 100) + ((dimes * dimes_per_dollar) / 100) + ((quarters * quarters_per_dollar) / 100) + ((halfdollars * halfdollars_per_dollar) / 100);
    This is my new line for total_cents and it's still crap not working..

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why should it? It's not as though it makes any sense. Say I have five nickels. nickels_per_dollar is defined as 20. So (5*20)/100 = 1 -- meaning five nickels is worth one cent.

    Think about what you're doing for seven seconds, and then write down code.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Haha, thanks for the e-slap in the face tabstop, I thought about it for seven seconds. I redefined my defines to what each thing literally equals and now this is my new line and it works...

    Code:
    total_cents = (pennies) + (nickels * nickel) + (dimes * dime) + (quarters * quarter) + (halfdollars * halfdollar);

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Will.I.Am View Post
    I had just noticed that after I posted it, I actually am getting somewhere now, but whatever I put in I only get out 1236 cents? So I've switched it to multiplication but it is still just a tad awry...so frustrating.
    Will, you have been at it so much, you can't see the forest for the tree's.

    "It's not that you're wrong, it's not that I'd knock it, it's just that I, am not in the market..."

    Sorry, a song from 20 years ago, comes to mind.

    Your formula should be:

    total cost = pennies + (nickels * 5) + (dimes * 10) + (quarters * 25) + (half dollars * 50)

    Remember, you're changing everything to pennies, *first*.

    Now
    dollars = total cost / 100; (fractional parts are truncated)
    total change = total cost - (dollars * 100) [you can also do this with mod operator]

    Now a while loop will polish off the coins:

    Code:
    while(total change > 4)  {
       if(total change >= 50)  {
          half dollars++;
          total change -= 50;
       } 
    
       if(total change >= 25)  {
          quarters++;
          total change -= 25;
       }
    
    //etc. down to nickels. Pennies equals the remaining total change.
    
    }
    Make sense to you?

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Adak, that makes perfect sense now.. I really do not know what I was thinking I started this, but this is my source code and it works great, enjoy it.

    Code:
    #include <stdio.h>
       #include <stdlib.h>
       
       #define nickel 5
       #define dime 10
       #define quarter 25
       #define halfdollar 50
       
       
       main()
       
       {
             int pennies, nickels, dimes, quarters, halfdollars, total_coins, total_cents, total_dollars, total_remainder; 
             
             printf("How many pennies do you have? ");
             scanf("%d", &pennies);
             
             printf("How many nickels do you have? ");
             scanf("%d", &nickels);
             
             printf("How many dimes do you have? ");
             scanf("%d", &dimes);
             
             printf("How many quarters do you have? ");
             scanf("%d", &quarters);
             
             printf("How many half dollars do you have? ");
             scanf("%d", &halfdollars);
             
             total_coins = pennies + nickels + dimes + quarters + halfdollars;
             
             total_cents = (pennies) + (nickels * nickel) + (dimes * dime) + (quarters * quarter) + (halfdollars * halfdollar);
             
             total_dollars = (total_cents / 100);
             
             total_remainder = (total_cents % 100);
             
             printf("\n");
             printf("Your total number of coins is: %d\n", total_coins);
             printf("You have %d cents.\n", total_cents);
             printf("You have %d dollars and", total_dollars);
             printf(" %d cents.\n\n", total_remainder);
             
             
             system("PAUSE");
             
       }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a "total remainder" in pennies, but you didn't convert it back to normal "change":

    half dollars, if more than 49 cents remain in total remainder. Subtract 50 cents for every 1 half dollar.
    and
    quarters if more than 24 cents remains after half dollars are subtracted from total remainder.
    and
    dimes if more than 9 cents remains after quarters are subtracted from total remainder, etc.

    Better look at my while loop, again.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Ah, I understand now, but we haven't started while statements in class and such or even half the commands you used in your statement. But when I run mine it gives me back what I need and as much as the book is asking for. I definitely thank you for all of your help, and now I know who to look for whenever I get baffled over the next program. Looking back I actually do feel somewhat like wtf after reading what I originally posted, I am just glad everything works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. A simple currency conversion program gone south!!
    By lildroopie in forum C Programming
    Replies: 1
    Last Post: 02-01-2009, 11:45 AM
  3. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM
  4. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  5. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM

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