Friday, July 31, 2009

I'm trying to do a simple C program and I need to figure out how to check if a user input is a floating point?

I can't figure out why this code doesnt' work.


#include%26lt;stdio.h%26gt;


#include%26lt;stdlib.h%26gt;


#include%26lt;conio.h%26gt;





float salesTax(float input, float rate);





int main(){


float amount;


float i = .0725;


float j = .075;


float k = .0775;


printf( "Enter in the purchase amount please! (Amount Must Be Positive) " );


scanf( "%f", %26amp;amount );


while ( amount %26lt;= 0 ) {


printf ("ERROR: You have to enter in a positive digit. Try Again\n\n\n" );


printf("Press any key to continue. . . \n");


getch();


clrscr();


printf( "Enter in the purchase amount please! (Amount Must Be Positive) " );


scanf( "%f", %26amp;amount );}


clrscr();


printf("Your purchase price: $%d", amount);


printf("Del Mar Tax = " );


printf("%.2f\n\n", salesTax(amount,i));


printf("Encinitas Tax = ");


printf("%.2f\n\n", salesTax(amount,j));


printf("La Jolla Tax = ");


printf("%.2f\n\n\n\n", k);


printf("Press any key to continue. . . \n");


getch();


return 0;}





float salesTax (float input, float rate){


return input*rate;}

I'm trying to do a simple C program and I need to figure out how to check if a user input is a floating point?
You didn't say what happens, but there's two suspicious statements:





printf("Your purchase price: $%d", amount);





amount is a float so the format specifier should be %f not $d.





printf("%.2f\n\n\n\n", k);





This prints only the rate, not the tax amount. Should be salesTax(amount,k);





Other than that it probably works.

calling cards

No comments:

Post a Comment