[C] Scanf and error trapping question
I'm just beginning at C, so I have a few questions on something. I'm trying to get user input and multiply it by tax rates. The scanf function is not working for me. It doesn't read anything into my variable and then it prematurely exits.
Code:
#include <stdio.h> //include library stdio
//main method
void main()
{
/***********************
* Del Mar - 7.25% *
* Encinitas - 7.5% *
* La Jolla - 7.75% *
***********************/
//print out the 'menu' of tax rates
printf("*** Tax Rates ***\n");
printf("-----------------\n");
printf("Del Mar - 7.25%\n");
printf("Encinitas - 7.5%\n");
printf("Del Mar - 7.75%\n\n\n");
double new_amount = 0;
printf("Current amount = $125\n\n");
printf("Enter an amount: ");
scanf ("%d", &new_amount);
new_amount += 125;
double tax_rate = .0725;
double tax_total = tax_rate*new_amount;
double total_amount = tax_total+new_amount;
printf("$%d at Encinitas tax rate:\n",new_amount);
printf("-- Tax: %f\n", tax_total);
printf("-- Total: %f\n", total_amount);
getchar();
}
Why is it always giving me 0 as the new amount no matter what I input?
Also, is there some way to add some simple error checking, like making sure the user enters a number? If so, could you point me to a tutorial or something?
Re: [C] Scanf and error trapping question
%d will not read a floating point number. You should use %f, which stands for float -- specifically, %lf, which means a long float or double.
scanf returns the number of variables sucessfully read, so if one variable is expected and it is not read correctly, scanf will return 0.
You can find the specifications for the % format specifiers on the printf() man page, or by searching for printf or scanf on google.
Re: [C] Scanf and error trapping question
Thanks sunburt. I just went ahead and used fgets and fputs to be on the safe side.
Re: [C] Scanf and error trapping question
How is that the "safe side"? You have to do all the conversions yourself, have to watch buffer space ...
By the way, it's int main in C, too.
Re: [C] Scanf and error trapping question
I have the following method doing the input:
Code:
double get_input()
{
//first get the input as a string
char text[20];
fputs("Enter an amount: ", stdout);
fflush(stdout);
//make sure the input is valid
if ( fgets(text, sizeof text, stdin) )
{
//try to convert the input to a double
int num;
if (sscanf(text,"%d",&num) == 1 )
{
return num;
}
//else, there was a parse error
else
{
//print out an error message and exist
printf("Error -- Press Enter To Exit\n");
char c;
scanf("&c",c);
exit(1);
}
}
}
But miracle C has a problem with one of the lines and gives the following error:
Quote:
C:\Program Files\Miracle C\TaxRatesPartOne.c: line 15: Parse Error, expecting `'(''
'if ( fgets(text, sizeof text, (&_files[0])) ) { int num'
aborting compile
What exactly is wrong? Everything looks fine to me?
Re: [C] Scanf and error trapping question
No reason for the fgets here, just use fscanf (or even scanf, since you're hard-coding stdout anyway) directly as you do with sscanf.
As for the error, I see two possibilities:
1) The compiler cannot swallow using sizeof without parentheses. Try sizeof(text).
2) The compiler cannot swallow the variable definition inside the if. Try placing it at the top of the function. Same for the char in the else-block; however, that is a real error in C89. In C prior to the C99 standard, variable declarations must be at the top of their block.