Thread: Question on scanf();

1. Question on scanf();

Hello, comrades. I have a question on this useless program I wrote. Notice the line /*45*/ and /*46*/. This program didn't function correctly when I runned it with one scanf(), [which it should've worked?], however, when I wrote as below at /*46*/, [scanf() after scanf()], it worked, why is this so?

Code:
```/* This was originally written to calculate my salary */

#include <stdio.h>
#include <stdlib.h>

main ()
{
int wage;
int hours;
int days;
int subtotal;

int fee;
int total;
char choice;

/* Form here goes calculation of gross profit */

printf("How much do you get paid an hour? >\$");
scanf("%d", &wage);
if(!wage) {
printf("Input error... terminating the program.\n\a");
exit(1);
}

printf("How many hours do you work a day? > ");
scanf("%d", &hours);
if(!hours) {
printf("Input error... terminating the program.\n\a");
exit(1);
}

printf("How many days do you work in a month? > ");
scanf("%d", &days);
if(!days) {
printf("Input error... terminating the program.\n\a");
exit(1);
}

subtotal = wage * hours * days;

/* From here goes transportation fee calculation*/

printf("Do you use any public transportation? (y/n) > ");
/*45*/	scanf("%c", &choice);
/*46*/	scanf("%c", &choice);

if(choice == 'y') {
printf("How much does it cost you in a day? >\$");
scanf("%d", &fee);
if(!fee) {
printf("Input error... terminating the program.\n\a");
exit(1);
}
printf("Your salary is \$%d\n", subtotal - fee * days);

} else if(choice == 'n') {
printf("Your salary is \$%d\n", subtotal);

} else {
printf("Input error... terminating the program.\n\a");
}

return 0;
}```
And also, as you probably have noticed, there are repetition of this below with defferent arguments, [!wage, !fee, !days, etc]. Is there any way to make this little more efficient?
Code:
```if(!wage) {
printf("Input error... terminating the program.\n\a");
exit(1);
}```
Btw: Hello! I'm new to this forum!

2. See the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385 (read Option 2)

Originally Posted by http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1043372399&id=1043284385
There are two problems with using scanf() to get a number:

First, validation/error handling is poor. If the user enters 1234ZZ as their number, scanf() will read 1234 into the int variable, and leave the ZZ in the input buffer. Although the user has entered what appears to be an invalid number, the program hasn't actually noticed, and will continue processing as if all is well.
In other words, your probably leaving the linebreak (\n) in the input buffer...

3. This is roughly how I tend to get a single character y/n answer:
Code:
```#include <stdio.h>

int main()
{
char choice='x'; //Initialise no not y/n

while(choice != 'y' && choice != 'n') //Loop until y or n entered
{
printf("Do you use any public transportation? (y/n) > ");
choice=getchar();
getchar(); //Nullify return char
}

printf("Choice: %c", choice);
getchar();
}```

4. Fine except getchar() return's an int , and see the FAQ about clearing the input stream (stdin in this case)... :P

5. I see.. Thank you.

Popular pages Recent additions