Results 1 to 2 of 2

Thread: Losing data by converting int to float

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    26

    Question Losing data by converting int to float

    Hi everyone who reads this,

    I'm writing a easy code that handles change(as in money) so that it tells how much your change is and in how many of each bills to give back (like 20's 10's 5's etc...)

    When I compile the code it gets that warning that "convertion from into to float may cause data loss" and I do.
    Sometimes the output will be correct and give me the right amount, but sometimes I lose a penny in the process and end up being short one penny.

    EX: Price = 12.34
    Tendered = 20.00

    Change: 7.66

    Five: 1
    Dollars: 2
    Quarters:2
    Dimes:1
    Nickles:1
    Penny: 0

    The actual output is bit longer than that but you get my point. I's missing that 1 penny and I can't figure out what my problem is x.x I think it might how I set up the
    calculating process.
    If someone can look at my code and maybe point out where my problem is and what I should do to fix it that would be greatly appreciated.

    Code:
    //
    //  ChangeCounter.c
    //  
    //
    //  Created by Batnyagt Batsuren on April 20,2013
    //  Copyright (c) 2013 Batnyagt Batsuren
    //
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define EXIT_SUCCESS 0
    #define EXIT_FAIL -1
    
    int main(void)
    
    {
        //variables
        int change_Twenties = 0;
        int change_Tens = 0;
    	int change_Fives = 0;
        int change_Dollars = 0;
        int change_Quarters = 0;
        int change_Dimes = 0;
        int change_Nickels = 0;
        int change_Cents = 0;
    
        float input_Price = 0.0;
        float tender_Amount = 0.0;
        float change_Amount = 0.0;
    
        //prompts for item price
    	printf("Thank You for using my Change Counter \n");
        printf("Please enter price in dollars and cents:  \n $ ");
        scanf("%f", &input_Price);
    
        //prompts for amount tendered
        printf("Please enter tendered amount:  \n $ ");
        scanf("%f", &tender_Amount);
        
    	
    	//calculating change amount
    	change_Amount = tender_Amount - input_Price;
        //If inputed tender amount is smaller than price,
    	if (change_Amount < 0)
        {
            printf("Please enter a value that's greater or equals to price of item.\n");
    		getchar();
    		getchar();
            return EXIT_FAIL;
        }
        // sets each variable to the correct amount of each and takes away the amount from change cents
        
    
    	change_Cents = (change_Amount * 100.0);
        printf("%d cents \n", change_Cents);
    
        //change 20's
        change_Twenties = (change_Cents / 2000);
        change_Cents %= 2000;
    
        //change Tens
        change_Tens = (change_Cents / 1000);
        change_Cents %= 1000;
    
        //change dollars
        change_Dollars = (change_Cents / 100);
        change_Cents %= 100;
    
        //change quarter
        change_Quarters = (change_Cents / 25);
        change_Cents %=  25;
    
        //change dimes
        change_Dimes = (change_Cents / 10);
        change_Cents %=  10;
    
        //change nickels
        change_Nickels = (change_Cents / 5);
        change_Cents %=  5;
    
        //debug
        printf("$%5.2f \n", input_Price);
        printf("$%5.2f \n", tender_Amount);
        printf("$%5.2f \n", change_Amount);
        printf("%d Twenties \n", change_Twenties);
        printf("%d Tens \n", change_Tens);
        printf("%d Fives \n", change_Fives);
        printf("%d Dollars \n", change_Dollars);
        printf("%d Quarters \n", change_Quarters);
        printf("%d Dimes \n", change_Dimes);
        printf("%d Nickels \n", change_Nickels);
        printf("%d Cents \n", change_Cents);
        getchar();
    	getchar();
    
        return EXIT_SUCCESS;
    }

  2. #2
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: Losing data by converting int to float

    Hi to you
    float support decimal but int doesnt so if u convert it to int you will loose the decimal part

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width