Reference parameters and calculating change

This is a discussion on Reference parameters and calculating change within the C Programming forums, part of the General Programming Boards category; I need to query the user for the amount owed and the amount received. I then must calculate the change ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Reference parameters and calculating change

    I need to query the user for the amount owed and the amount received. I then must calculate the change due and display it in the form of dollars, quarters, dimes, nickels and pennies. I need to use at least 4 reference parameters within the code as well. The code looks good to me, but for some reason the calculations just spit out garbage. Any suggestions?



    Code:
    /*Program to determine amount of change due
      Nov. 4, 2005
      Language: C (gcc target)
    */
    
      #include <stdio.h>
      #define dollar 100
      #define quarter 25
      #define dime 10
      #define nickel 5
    
    
    /*Function prototypes*/
      void fourth (int *amount2);
      void tenth (int *amount3);
      void twentieth (int *amount4);
      void hundredth (int *amount5);
    
    int main(void)
    { double total, payment;
      int amt, amt2, dollars;
    
      printf("Enter amount of money due:  ");
      scanf("%d", &total);
      printf("Enter amount of money given:  ");
      scanf("%d", &payment);
      amt=(payment-total)*100;
      dollars=amt/dollar;
      printf("%d dollars in change due ",dollars);
      amt2=amt-(dollars*100);
      fourth(&amt2);
      return 0;
    }
    
    
    void fourth (int *amount2)
    { int quarters, amt3;
    
      quarters=*amount2/quarter;
      printf("\n%d quarters in change due ",quarters);
      amt3=*amount2-(quarters*quarter);
      tenth(&amt3);
      return;
    }
    
    
    void tenth (int *amount3)
    { int dimes, amt4;
    
      dimes=*amount3/dime;
      printf("\n%d dimes in change due ",dimes);
      amt4=*amount3-(dimes*dime);
      twentieth(&amt4);
      return;
    }
    
    
    void twentieth (int *amount4)
    { int nickels, amt5;
    
      nickels=*amount4/nickel;
      printf("\n%d nickels in change due ",nickels);
      amt5=*amount4-(nickels*nickel);
      hundredth(&amt5);
      return;
    }
    
    
    void hundredth (int *amount5)
    { int pennies;
    
      pennies=*amount5;
      printf("\n%d pennies in change due ",pennies);
      return;
    }

  2. #2
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39
    with a quick glance
    why return on voids?
    why ?
    Code:
    fourth(&amt2);
    why the &?

    there are simpler ways
    Last edited by white; 11-04-2005 at 11:49 AM.
    ----------------

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Quote Originally Posted by white
    with a quick glance
    why return on voids?
    why ?
    Code:
    fourth(&amt2);
    why the &?

    there are simpler ways

    It was my understanding that while the void returns no value, I still need the return in order for the entire program to complete. Is this incorrect? As far as the simpler ways goes, I don't know them and would appreciate some enlightenment.

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>It was my understanding that while the void returns no value, I still need the return in order for the entire program to complete.
    After the completion of the whole program only the return value of main is returned not all the return values of the functions you have defined in your program.

    Code:
    double total, payment;
    printf("Enter amount of money due:  ");
      scanf("%d", &total);
      printf("Enter amount of money given:  ");
      scanf("%d", &payment);
    you are storing a int value in double thats what you did'nt intend to.
    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
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39
    well the rule is that pass a value into a function and the function returns a value
    no need to pass a pointers and such...

    so
    Code:
    fourth(&amt2);
    becomes
    Code:
    fourth(amt2);
    Code:
    void fourth (int *amount2)
    becomes
    Code:
    void fourth (int *amount2)
    you do the calculations.... then you pass the next number into the next function
    and so on. (along with the prementioned changes)
    ----------------

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by white
    well the rule is that pass a value into a function and the function returns a value
    no need to pass a pointers and such...

    so
    Code:
    fourth(&amt2);
    becomes
    Code:
    fourth(amt2);
    Incorrect.amt2 is not a pointer but the function declaration wants a pointer so &amt2 is correct to pass.
    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"

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    the only thing you need to do to get the code in your original post to work is to use %lf instead of %d in your scanf calls, since the datatype for payment and total is double.

    you do not need return statments in void functions (you should remove them, though they won't hurt anything)

    @white. There is nothing wrong with using pointers over passing by value. In many situations it's preferred.
    If you read the original post he "needs" to... probably a requirement of the assignment.


    ^this is nothing not already stated in the thread, but it was getting a little confusing

Popular pages Recent additions subscribe to a feed

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