0

I have finished an entire program but there is one small problem with a bit of error trapping.

while((((payment/.01)-(floor(payment/.01)))>0.0)  ||  (payment<0.0)) 
        {
        	printf("You can't pay with fractions of one cent! You can't pay with negative amounts! Please type in your payment ");
        	scanf("%f", &payment);
        }

In this little bit of code it is supposed to not accept negative values and values past 2 decimal places -- which is what it almost does EXCEPT the loop picks up 4.23 and 4.26 as unacceptable values also and I can't understand why! (might be more but I have only found those)

My entire program looks loke this. The problem arises whenever I have a while loop that looks like this: while((((payment/.01)-(floor(payment/.01)))>0.0) || (payment<0.0))
Another bit of information, if it helps- I am using the miracle c compiler.

#include<stdio.h>
#include<math.h>

float adds(float amountofitems);
float change(float amountpaid, float total); 
					

main()				
{
				
    float numberofitems;
    float total;
    float payment;
    float difference;
    
    
    printf("Please type in the number of items you would like to purchase ");
    scanf("%f",&numberofitems);
    
    while((numberofitems<0.0)  ||  (numberofitems!=floor(numberofitems)))
    {
    	printf("You can't buy less than nothing! You can't buy fractional amounts! Please type in the number of items you would like to purchase. ");
    	scanf("%f",&numberofitems);
    }
    total=adds(numberofitems);
    
    printf("Please pay %.2f (enter the amount of money you are giving as payment) ", total);
    scanf("%f",&payment);
    while((((payment/.01)-(floor(payment/.01)))>0.0)  ||  (payment<0.0))
        {
        	printf("You can't pay with fractions of one cent! You can't pay with negative amounts! Please type in your payment ");
        	scanf("%f", &payment);//gets a float from the user and stores it into payment
        }
    difference=change(payment,total);
    
    
    while(difference<0.0)
    {
     	printf("Please pay %.2f (enter the amount of money you are giving as payment) ", difference*-1);
     	scanf("%f",&payment);
     	while((((payment/.01)-(floor(payment/.01)))>0.0)  ||  (payment<0.0))
        {
        	printf("You can't pay with fractions of one cent! You can't pay with negative amounts! Please type in your payment ");
        	scanf("%f", &payment);
        }
     	total=difference*-1;
     	difference=change(payment,total);
    }


    printf("\nThank you for shopping at Fred and Ethel's Convenience Mart! ");
    getchar();
}

float adds(float amountofitems)
{ 
    float x;
    float price;
    float total;
    
    for(x=0.0;x<amountofitems;x++)
    {
        printf("Please type in the price of the item ");
        scanf("%f", &price);
        while((((price/.01)-(floor(price/.01)))>0.0)  ||  (price<0.0))
        {
        	printf("The price can't be fractions of one cent! The price can't be negative! Please type in the price of the item ");
        	scanf("%f", &price);
        }
        total=total+price;
    }
    
    return total;
}        

float change(float amountpaid,float total)
{
	float difference;
	difference=amountpaid-total;
	
	if(difference>0.0)
	{
		printf("You get $%.2f back!",difference);
	}
	if(difference==0.0)
	{
		printf("You paid with exact change! Good for you!");
	}
	if(difference<0.0)
	{
		
		printf("You owe us $%.2f ",difference*-1);
	}
	
	return difference;
}
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Duoas
0

I haven't played with it really, but offer only this observation: using floats exactly is playing with fire. And you're stepping on the gas with payment/.01 . If you must use floats, just say payment*100 .

There's a nifty routine called modf() that splits a float into its integer and fractional parts. You could say: while ((modf( payment *100, NULL ) > 0.001) || (payment < 0.0)) That 0.001 is a simple epsilon that lets floating point numbers be their inexact selves...

Please use [[I][/I]code[I][/I]] blocks...

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.