input problem

This is a discussion on input problem within the C Programming forums, part of the General Programming Boards category; I just built a change dispenser program for a homework assignment and for some reason when the "amount due" is ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    input problem

    I just built a change dispenser program for a homework assignment and for some reason when the "amount due" is inputted by the user, say 25.80, the program turns it into 25.800000000000001... how can i make that go to only 2 decimal places?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't. 25.80 is not exactly representable in binary.

    BUT! Since it's change, you should be doing things in integral amounts of pennies. So multiply by 100 and round it off.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    i can't do like a "%.2lf" type thing?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That wouldn't change anything -- the input as seen in either case would be "25.80". The problem is that it is a physical impossibility for a computer to store "25.80" internally as a value (except as a string/BCD).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Quote Originally Posted by tabstop View Post
    The problem is that it is a physical impossibility for a computer to store "25.80" internally as a value (except as a string/BCD).
    Now that's just an outright lie.
    It's an impossibility for the poor (IMHO) floating point standard that C uses.
    That's a better way of putting it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    what do you mean physically impossible?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    25.80 in binary is a repeating decimal, just like 1/3 or 1/7 in decimal. So assuming that your computer is storing things in binary (most do) (edit: and not in floating slash, which most aren't), you can't get 25.80 unless you have an infinite number of bits.
    Last edited by tabstop; 10-14-2008 at 02:34 PM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Quote Originally Posted by lukestanton07 View Post
    what do you mean physically impossible?
    It means there's no way you're going to be able to get an exact number in C using floats or doubles.
    You're going to have to use some 3rd-party library if you really want to change this behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    ok i get it i'll just do the *100 and round...

    thanks for the help

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    eh actually...

    i'm actually at a loss as to what to do to fix this... here is my code. as expected the dollars portion works fine... so if you want an idea of how it work just enter like 21.00 and 25.00

    below is my main.c, followed by functions.c and functions.h

    Code:
    /****************************************************************************
    *Luke Stanton																*
    *October 12, 2008															*
    *Programming Assignment 5													*
    *Description:
    
    ****************************************************************************/
    
    #include "functions.h"
    
    int main (void)
    {
    	double price = 0.0;
    	double paid = 0.0;
    	double coin_change = 0.0;
    	double total_change = 0.0;
    	int dollar_change = 0;
    	int hundred_bill = 0;
    	int fifty_bill = 0;
    	int twenty_bill = 0;
    	int ten_bill = 0;
    	int five_bill = 0;
    	int one_bill = 0;
    	int quarter = 0;
    	int dime = 0;
    	int nickel = 0;
    	int penny = 0;
    
    	//welcome screen
    	welcome_screen();
    
    	//prompts the user for the price and amount paid
    	prompt_input(&price, &paid);
    
    	//welcome screen
    	welcome_screen();
    	
    	//determines the total change owed
    	total_change = paid - price;
    
    	//	truncates(removes cents from amount) the amounts so the dollar
    	//	change can be simplified and addressed
    	(int)dollar_change = total_change;
    
    	//	removes the dollars so just the cents can be addressed
    	coin_change = total_change - dollar_change;
    
    	//	finds dollar count in change
    	get_dollar_count(&hundred_bill, &fifty_bill, &twenty_bill, &ten_bill, &five_bill, &one_bill, &dollar_change);
    
    	//	finds coin count in change
    	get_coin_count(&quarter, &dime, &nickel, &penny, &coin_change);
    
    	//	Prints out the total change due
    	printf("Total:	%.2lf\n", price);
    	printf("Paid:	%.2lf\n", paid);
    	printf("Change: %.2lf\n\n", total_change);
    
    	//	Displays what bills are due
    	printf("  Dollar bills:			%d\n  Five dollar bills:		%d\n  Ten dollar bills:		%d\n  Twenty dollar bills:		%d\n  Fifty dollar bills:		%d\n  One hundred dollar bills:	%d\n\n",
    		   one_bill, five_bill, ten_bill, twenty_bill, fifty_bill, hundred_bill);
    
    	//	Displays what coins are due
    	printf("  Pennies:			%d\n  Nickels:			%d\n  Dimes:			%d\n  Quarters:			%d\n\n",
    		   penny, nickel, dime, quarter);
    
    	return 0;
    }
    Code:
    //FUNCTIONS.C
    
    #include "functions.h"
    
    int welcome_screen(void)
    {
    	system("cls");
    	printf("     ________________________________\n");
    	printf("    |$5                            $5|\n");
    	printf("    |            /-------\\           |\n");
    	printf("    |       --- /---------\\ ---      |\n");
    	printf("    |  WELCOME-TO-CHANGE-CALCULATOR  |\n");
    	printf("    |       --- \\---------/ ---      |\n");
    	printf("    |            \\-------/           |\n");
    	printf("    |$5____________________________$5|\n\n");
    
    	return 0;
    }
    
    double prompt_input(double *price, double *paid)
    {
    	printf("\n\n\nPlease enter the price of total purchase:");
    	scanf("%lf", &*price);
    	printf("\nPlease enter the amount paid by the customer:");
    	scanf("%lf", &*paid);
    
    	system("cls");
    
    	return 0;
    }
    
    int get_dollar_count(int *hundred_bill, int *fifty_bill, int *twenty_bill, int *ten_bill, int *five_bill, int *one_bill, int *dollar_change)
    {
    	if (*dollar_change >= HUNDRED)
    	{
    		*hundred_bill = *hundred_bill + 1;
    		*dollar_change = *dollar_change - HUNDRED;
    	}
    	if (*dollar_change >= FIFTY)
    	{
    		*fifty_bill = *fifty_bill + 1;
    		*dollar_change = *dollar_change - FIFTY;
    	}
    	if (*dollar_change >= TWENTY)
    	{
    		*twenty_bill = *twenty_bill + 1;
    		*dollar_change = *dollar_change - TWENTY;
    	}
    	if (*dollar_change >= TEN)
    	{
    		*ten_bill = *ten_bill + 1;
    		*dollar_change = *dollar_change - TEN;
    	}
    	if (*dollar_change >= FIVE)
    	{
    		*five_bill = *five_bill + 1;
    		*dollar_change = *dollar_change - FIVE;
    	}
    	while (*dollar_change != 0)
    	{
    		*one_bill = *one_bill + 1;
    		*dollar_change = *dollar_change - DOLLAR;
    	}
    		
    	return 0;
    }
    
    int get_coin_count(int *quarter, int *dime, int *nickel, int *penny, double *coin_change)
    {
    	while(*coin_change >= QUARTER)
    	{
    		*quarter = *quarter + 1;
    		*coin_change = *coin_change - QUARTER;
    	}
    	while(*coin_change >= DIME)
    	{
    		*dime = *dime + 1;
    		*coin_change = *coin_change - DIME;
    	}
    	if (*coin_change >= NICKEL)
    	{
    		*nickel = *nickel + 1;
    		*coin_change = *coin_change - NICKEL;
    	}
    	while (*coin_change != 0)
    	{
    		*penny = *penny + 1;
    		*coin_change = *coin_change - PENNY;
    	}
    		
    	return 0;
    
    }
    Code:
    //FUNCTIONS.H
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define DOLLAR 1
    #define FIVE 5
    #define TEN 10
    #define TWENTY 20
    #define FIFTY 50
    #define HUNDRED 100
    #define PENNY .01
    #define NICKEL .05
    #define DIME .10
    #define QUARTER .25
    
    int welcome_screen(void);
    
    double prompt_input(double *price, double *paid);
    
    int get_dollar_count(int *hundred_bill, int *fifty_bill, int *twenty_bill, int *ten_bill, int *five_bill, int *one_bill, int *dollar_change);
    
    int get_coin_count(int *quarter, int *dime, int *nickel, int *penny, double *coin_change);

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Checking whether a float value != 0 is a fool's game. Why wouldn't you keep doing exactly as you've been doing: while (*coin_change >= PENNY) etc.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    haha ok but still what to do about the whole repeating decimal problems?

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    I usually solve problems related to finances with integer math because it's accurate.

    dollar_amount *= 100.0;
    as_pennies = dollar_amount; // the whole number part is assigned to long
    quarters = as_pennies / 25;

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You have to recheck your calculations for how you're getting the integer dollar_change and coin_change.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 10:34 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 03:44 PM

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