Like Tree2Likes
  • 2 Post By std10093

Using functions to convert change into dollars

This is a discussion on Using functions to convert change into dollars within the C Programming forums, part of the General Programming Boards category; I know this is pretty basic script the problem I have is that I have to use functions for each ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    3

    Using functions to convert change into dollars

    I know this is pretty basic script the problem I have is that I have to use functions for each calculation. The problem I face is that when the user types in the amount of quarters, dimes, nickels, and pennies when a function tries to add them together it displays a huge number instead of the right number.

    Here is my code:

    Code:
    #include <stdio.h>
    
    void insert_money(int p, int n, int d, int q, int total)
    {
        printf("How much of each coin do you want to insert: \n");
        printf("Quarters: ");
        scanf("%d", &q);
    
        printf("Dimes: ");
        scanf("%d", &d);
    
        printf("Nickels: ");
        scanf("%d", &n);
    
        printf("Pennies: ");
        scanf("%d", &p);
    }
    
    void return_dollars(int p, int n, int d, int q, int dollars, int total)
    {
        total = 25*q + 10*d + 5*n + p;
        dollars = total/100;
        total = p % 100;
    
        printf("\nYou have this many Dollars: %d\n", dollars);
    }
    
    void remove_money(int p, int n, int d, int q, int total)
    {
        q = total/25;
        total = p % 25;
    
        d = total/10;
        total = p % 10;
    
        n = total/5;
        total = n % 5;
    }
    
    void display(int p, int n, int d, int q)
    {
        printf("You have this many Quarters: %d\n", q);
        printf("You have this many Dimes: %d\n", d);
        printf("You have this many Nickels: %d\n", n);
        printf("You have this many Pennies: %d\n", p);
    }
    
    void main()
    {
        int p, n, d, q, dollars, total;
    
        insert_money(p, n, d, q, total);
    
        return_dollars(p, n, d, q, dollars, total);
    
        remove_money(p, n, d, q, total);
    
        display(p, n, d, q);
    
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    First things first. //Welcome to the forum!!

    We use to write
    Code:
    int main(void)
    {
        ...
        return 0;
    }
    instead of the void main() you have there.

    Now, an piece of advice, when you get large numbers and you expect to get some regular numbers, then probably, a logical error has occur, of type uninitialized variable!

    Let me see your code now...Hmm...

    What's up with the passing of the arguments in your functions? Are they passed by value or by reference? Read your notes and try again
    stahta01 and Filster7 like this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    That is a good question, but when I change the variables instead of them being the same as the arguments it will tell me that I have 'undeclared variables'.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    That's not the case. Haven't you have been shown a swap example?

    You need to pass the arguments by reference. Recall, that you can always return a variable, or return something, but this is just one "thing". For example, this code returns a value, to inform main.

    However, you have more than one variables, so you should use "passing arguments by reference", because this is what you are supposed to be practicing right now.
    Check this example and then try again
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    3
    My latest assignment was to use pointers I am just trying to redo an older assignment because I am terrible with functions and I know they are important and basic.

    What is the difference then of pointers and passing arguments by reference?

    Edit: How can pass the information the user typed in the first function into the second function and so on?
    Last edited by letsrelax; 07-20-2013 at 07:39 PM.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    you can consider the relative merits by looking these things up
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,700
    What is the difference then of pointers and passing arguments by reference?
    Passing by reference is accomplished by using pointers in C. This code changes 42:
    Code:
    #include <stdio.h>
    
    void foo(int *x) {
    
      *x += 1;
    
    }
    int main(void) {
      int y = 41;
    
      foo(&y);
    
      printf("%d\n", y);
    
      return 0;
    }
    You can use the parameter list to change multiple variables. Whenever you want to change a variable of some type, even another pointer,you need to use a pointer -- add a *.

    If you're not allowed to do that -- you did mention this is an earlier assignment -- you should make a struct type and use that. Make sure your function has a return value if you do it that way.
    Code:
    struct money foo(struct money myMoney);
    Last edited by whiteflags; 07-20-2013 at 07:48 PM. Reason: wrong type, ding dong

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by std10093 View Post
    Check this example
    Quote Originally Posted by letsrelax View Post
    ..I am terrible with functions and I know they are important and basic.
    Edit: How can pass the information the user typed in the first function into the second function and so on?
    Did you really read the example I provided?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dollars to yen.
    By rosemary in forum C Programming
    Replies: 7
    Last Post: 10-07-2012, 10:29 PM
  2. C Program: Convert to Binary and Change Endian-ness
    By lolgasm in forum C Programming
    Replies: 4
    Last Post: 03-29-2012, 04:41 PM
  3. Simple Program For Converting Change to Dollars
    By Will.I.Am in forum C Programming
    Replies: 12
    Last Post: 02-01-2009, 10:20 PM
  4. Trying to convert a total amount into change
    By American Raptor in forum C Programming
    Replies: 9
    Last Post: 12-07-2008, 09:33 PM
  5. Convert code to use Pointers, Functions, etc.
    By JYoung in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-13-2001, 12:33 PM

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