Thread: C Programming Question

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    7

    C Programming Question

    I've been assigned to run a program to calculate change. I have to display how many dollars, quarters, dimes, etc are to be returned to the customer. I am leaving out half of the problem, but I'm not having any issues with that part. Just having issues figuring out how to show that the customers gets back 4 dollars, 2 quarters, & 1 dime, etc. Any help is much appreciated. Thanks

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    By leaving out what you have done, we have no way to help you. Around here, it always pays to show what you have so far. This tells us many things, like in general your level of expertise (so we don't shoot a solution to you far more advanced than the rest of your app), compiler, platform, etc.

    What exactly are you having problems with? And if its just 'having issues' then there is no code to fix...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    If they're all in separate variables, ie
    Code:
    int dollars, quarters, dimes, nickels, pennies;
    then
    Code:
    printf("%d dollars, %d quarters, %dimes, %d nickels, %d pennies\n", dollars, quarters, dimes, nickels, pennies);
    is what you're looking for.

    But like jcobb said, if this isn't your answer then you're going to have to explain yourself a bit more.
    Consider this post signed

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    7
    This is what I have:

    Code:
    #include <stdio.h>
    int main()
    {
    float owe, paid, change;
    int dollars, quarters, dimes, nickels, pennies;
        printf("How much does the customer owe? ");
        scanf("%f",&owe);
        printf("How much did the customer pay? ");
        scanf("%f",&paid);
        change=paid-owe;
        printf("The customers change is $%5.2f\n",change);
        
    return 0;
    }
    So, I'm confused on how to display if the change is $4.25, to show 4 dollars, 1 quarter

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well the first thing I would do is convert it to pennies and stick it into an integer by taking change, multiplying it by 100 and assign it to an int. In your case it would convert the float from 4.25 to 425. Then I push it through a decision tree where you would do something like intChange = 425; (note that is calculated). Then since a dollar unit is 100 pennies, do a totalDollars = intChange / 100; This will yield 4 in this case. Get the resulting pennies by doing remainingPennies = intChange - (totalDollars * 100). This will leave you with 25 in the remaining pennies var and 4 in the total dollars var. Then repeat the same basic process with quarters (25 pennies):
    Code:
    totalQuarters = intChange / 25;
    int remainingPennies = intChange - (totalQuarters * 25);
    
    if( remainingPennies == 0)
    {
        cout << "we are done:" << endl;
        cout << "Total change: Dollars: " << totalDollars << ", Quarters: " << totalQuarters << endl;
    }
    I will leave the dimes etc as an exercise for the OP. It should be blindingly obvious by now. I know you could use the modulo operator to do the change thing too but given that the OP was confused by something like this my intent was to keep any formulas as simple at possible.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would just plain not use floats. Use an integer value cents. Then this one is all just simple modulus and subtraction.

    pay - owe = cents
    cents -= (cents%25)*25 <- get quarters
    cents -= (cents%10)*10 <- get dimes

    Obviously the "getting" is left out of this big hint.

    If you want to print your cents as $:
    Code:
    printf("$%d.%02d",cents/100,cents%100);
    Last edited by MK27; 05-16-2010 at 02:32 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by MK27 View Post
    I would just plain not use floats. Use an integer value cents. Then this one is all just simple modulus and subtraction.

    pay - owe = cents
    cents -= (cents%25)*25 <- get quarters
    cents -= (cents%10)*10 <- get dimes

    Obviously the "getting" is left out of this big hint.

    If you want to print your cents as $:
    Code:
    printf("$%d.%02d",cents/100,cents%100);
    100, 000.09999 percent agreed MK

    floating point is good for some things but in far too many other cases it can lead to imprecise results.....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Is this the same assignment from this thread? Change function

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    7
    its not the same, but close.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    jeffcobb, did you notice this is the C board?
    Posting C++ code in the C board and vice versa is usually not encouraged.
    Another possible solution would be the way you do it in real life: Count the number of dollars you give back, then subtract that amount from what you have yet to give out, then from that, select the highest number of money type you give out, calculate the amount, etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You guys do realize that a thread on this particular assignment has been posted over 5 times in the past 2 months or so and that the code the OPs provide us with is a just a basic framework GIVEN BY THE INSTRUCTOR?!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by claudiu View Post
    You guys do realize that a thread on this particular assignment has been posted over 5 times in the past 2 months or so and that the code the OPs provide us with is a just a basic framework GIVEN BY THE INSTRUCTOR?!
    Now you expect them to actually put in some effort on their own, and maybe even search the forums!? WHAT IS NONSENSE!?


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  2. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM