Thread: input problem

1. 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. 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. i can't do like a "&#37;.2lf" type thing?

4. 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. Originally Posted by tabstop
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.

6. what do you mean physically impossible?

7. 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.

8. Originally Posted by lukestanton07
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.

9. ok i get it i'll just do the *100 and round...

thanks for the help

10. 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. 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. haha ok but still what to do about the whole repeating decimal problems?

13. 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. You have to recheck your calculations for how you're getting the integer dollar_change and coin_change.

Popular pages Recent additions