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!