Thread: float to integer

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Question float to integer

    Having trouble getting the right output out of this assignment.

    The first assignment is:
    Write a C function named change() that accepts a floating point number of total coins and the addresses of the integer variables named quarters, dimes, nickels, and pennies. The function should determine the number of quarters, dimes, nickels and pennies in the total coins number passed to it and write these values directly into the respective variables declared in its calling function using pointers.

    Call the function change() from main() 3 times and print out the contents of the variables quarters, dimes, nickels, and pennies after each function return.

    First call - pass in the total value $1.88 and on return print the contents of the variables.

    second call - pass in the total value .32 and on return print the contents of the variable

    Thrid call - ask for a total value input from the keyboard and on return print the contents of the variable.

    Output should look like:
    TOTAL VALUE ENTERED: 1.88
    7 quarters
    1 dime
    0 nickels
    3 pennies

    This is what i have so far:
    Code:
    #include <stdio.h>
    
    void change(float, int *, int*, int*, int*);
    
    int main()
    {
    	float total;
    	int quarters, dimes, nickels, pennies;
    
        {
            total = 1.88;
            
            change(total, &quarters, &dimes, &nickels, &pennies);
            
            printf("TOTAL VALUE ENTERED: %6.2f", total);
            printf("%d quarters", quarters);
            printf("%3d dimes", dimes);
            printf("%3d nickels", nickels);
            printf("%3d pennies", pennies);
        }
    
        {
            total = 0.32;
            
            change(total, &quarters, &dimes, &nickels, &pennies);
            
            printf("TOTAL VALUE ENTERED: %6.2f", total);
            printf("%d quarters", quarters);
            printf("%3d dimes", dimes);
            printf("%3d nickels", nickels);
            printf("%3d pennies", pennies);
        }
    
        {
            printf("Enter the amount of money in your pocket:  \n");
            	scanf("%f", &total);
            
            change(total, &quarters, &dimes, &nickels, &pennies);
            
            printf("TOTAL VALUE ENTERED: %6.2f", total);
            printf("%d quarters", quarters);
            printf("%3d dimes", dimes);
            printf("%3d nickels", nickels);
            printf("%3d pennies", pennies);
        }
        return 0;
    }
    void change(float total, int *quarters, int *dimes, int *nickels, int *pennies)
    {		 
            if( total >= 0.25 )
            	*quarters = (total/0.25);
            
            if( total >= 0.10 )
            	*dimes = (total - (*quarters * 0.25))/0.10;
            
            if( total >= 0.05 )
            	*nickels = (total - (*quarters * 0.25) - (*dimes * 0.10))/0.05;
            
            if( total < 0.05 )
            	*pennies = (total - (*quarters * 0.25) - (*dimes * 0.10) - (*nickels * 0.05))/0.01);
    		
    	return;
    }
    c:\program files\miracle c\change.c: line 51: cannot convert from float/double '*quarters = (total/0.25)' aborting compile

    This is what I keep getting so I think I just have to fiqure out how to change quarters, dimes, nickels and pennies to an integer but I'm not quite sure how to do it. Any help would be greatly appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cannot convert from float/double '*quarters = (total/0.25)' aborting compile
    Odd. The only actual error that I see is a syntax error from mismatched parentheses. Converting a double to int is legal, but it should produce a warning unless you treat warnings as errors.
    My best code is written with the delete key.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Part of the problem?
    c:\program files\miracle c\change.c: line 51: cannot convert from float/double '*quarters = (total/0.25)' aborting compile
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yeah, do a board search - miracle C is a PoS compiler, get dev-c++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 10:47 PM