0

First block of code has printf and scanf statements; these work relatively fine (our prof told us to use %*c%c for some scanf inputs since asking for character and integer inputs gets weird but he didn't explain why). When I run my program the first block of code seems to work fine but the second's format is off and it seems to skip lines of instructions (namely, it skips receiving scanf inputs from the user and just displays the next line). Any ideas? For context, I'm only 3 weeks into an intro to C course so please be gentle.

printf("COFFEE-1...\n");
printf("Type ([L]ight,[B]lend): ");
scanf("%*c%c", &type1);
printf("Grind size ([C]ourse,[F]ine): ");
scanf("%*c%c", &grind1);
printf("Bag weight (g): ");
scanf("%d", &weight1);
printf("Best served with cream ([Y]es,[N]o): ");
scanf("%*c%c", &cream1);
printf("Ideal serving temperature (Celsius): ");
scanf("%d", &temp1);

printf("\nCOFFEE-2...\n");
printf("Type ([L]ight,[B]lend): ");
scanf("%*c%c", &type2);
printf("Grind size ([C]ourse,[F]ine): ");
scanf("%*c%c", &grind2);
printf("Bag weight (g): ");
scanf("%d", &weight2);
printf("Best served with cream ([Y]es,[N]o): ");
scanf("%*c%c", &cream2);
printf("Ideal serving temperature (Celsius): ");
scanf("%d", &temp2);
  • 1
    I'm guessing he used `"%*c%c"` to have the first `%*c` skip over the newline that ended the previous line. The correct way to do that is with `" %c"`. The space means to skip over all whitespace. – Barmar May 27 '22 at 20:57
  • “but he didn't explain why”. *Please* ask your professor to explain this until you understand why, in practice. This is a *key* to using `scanf` (if it must be used at all, as most robust methods are available). – S3DEV May 27 '22 at 20:57
  • There are ways of using `scanf` successfully for introductory programs like this, but `%*c%c` certainly isn't one of them! If your instructor can't even get this right, I shudder to think what's going to happen when you get to a *really* tough topic, like arrays and pointers. In the meantime you might check this [list of unwritten rules for avoiding `scanf`'s worst problems](https://stackoverflow.com/questions/72178518#72178652). – Steve Summit May 27 '22 at 21:07

0 Answers0