Thread: Change function

  1. #16
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Quote Originally Posted by mjpam View Post
    It's not necessarily overcomplicated if he has to use a function to calculate the change. In that case, it would best to use a function that returns all four numbers at once rather than four functions to return the number of each coin separately.

    So, Cody, do you have to use functions (other than main) in your program?
    You're right of course, if that is the restriction then he is going about it right, but the method I posted above is still useful for avoiding the floating points and finding remainders simply.

  2. #17
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by mjpam View Post
    It's not necessarily overcomplicated if he has to use a function to calculate the change. In that case, it would best to use a function that returns all four numbers at once rather than four functions to return the number of each coin separately.

    So, Cody, do you have to use functions (other than main) in your program?
    You can't return 4 things at once. There is a difference between what you are trying to say and what you are actually saying, and this can be very confusing to a beginner.
    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.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by claudiu View Post
    You can't return 4 things at once. There is a difference between what you are trying to say and what you are actually saying, and this can be very confusing to a beginner.
    Yeah, sorry. I mean "pass", I think.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    13
    Quote Originally Posted by KBriggs View Post
    You're right of course, if that is the restriction then he is going about it right, but the method I posted above is still useful for avoiding the floating points and finding remainders simply.
    You're very right that the rest of your post was very helpful, but I was just trying to point out that using pointers is necessary if he is trying pass (is that the correct terminology, claudiu?) for variables (or at lest their pointers) backing main.

    If I understand what is being done correctly, you pass the pointers to the variables to the function and modify the values of the variables pointed to be the pointers in the function.

  5. #20
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    I think the value of pennies gets messed up through all the type conversions, and it may be a rounding issue. I got some results by adding 0.5 to the pennies and casting it as an integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM