Thread: How can I swich out int with double or float to produce the right results?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    How can I swich out int with double or float to produce the right results?

    My program is as follows
    Code:
    #include <stdio.h>
     
    int countchange(int q, int d, int n, int p) ;
     
     
    /* Don't change the main() function! */
    int main () {
       int cents ; 
       int quarters, dimes, nickels, pennies ;
     
       printf("This program will compute the worth of your change.\n\n") ;
       printf("Enter number of quarters you have: ") ;
       scanf("%d", &quarters) ;
       printf("Enter number of dimes you have: ") ;
       scanf("%d", &dimes) ;
       printf("Enter number of nickels you have: ") ;
       scanf("%d", &nickels) ;
       printf("Enter number of pennies you have: ") ;
       scanf("%d", &pennies) ;
     
       cents = countchange(quarters, dimes, nickels, pennies) ;
     
       printf("Your %d quarters, %d dimes, %d nickels and %d pennies",
          quarters, dimes, nickels, pennies) ;
       printf(" are wort %d cents.\n", cents) ;
     
       return 0 ;
    }
    /* end of main() function */
     
     
     
    /* Function countchange
      
       Computes the worth of the given number of quarters,
       dimes, nickels and pennies.
     
    */
     
     
       /* The function */
     
    int countchange(int q, int d, int n, int p) {
            int qu;
            int di;
            int ni;
            int pe;
            int cents;
     
                    qu = q * .25 ;
                    di = d * .1 ;
                    ni = n * .05 ;
                    pe = p * .01 ;
                                            cents = qu + di + ni + pe ;
     
            return ;
    }
    When I execute my program I know why I am getting the wrong results because the integer value counts everything that is less than 1 as 0 (basically it rounds) therefore skewing my results. I know double/float will accept decimals but when I try and make qu, di, ni, pe, and cents double/float I still get wrong results.

    Any help would be great!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You program claims to output the total number of CENTS, yet you are computing (or trying to compute) DOLLARS.

    The total number of cents is just 25*quarters+10*dimes+5*nickels+cents.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Woah thank you so much I didn't even realize that! It works perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float vs Double
    By sughandh in forum C Programming
    Replies: 3
    Last Post: 07-16-2006, 04:26 AM
  2. double to float
    By jsbeckton in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 11:29 PM
  3. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM
  4. float; double; int
    By Lyanette in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2003, 12:04 PM
  5. Float,double,int
    By Bill 101 in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 02:09 PM