-2

I have problem with this code:

printf("Select your math: \n'+'addition \n'-'subtraction \n'*'multiplication \n'/' division \n");
char do_math;
scanf("%c", &do_math);
printf("Type 1 st number: ");

Problem is, that program doesn't wait until I type "do_math" but it displays "Type 1 st number: " right after first printf. Any ideas?

Xenix
  • 9
  • 1

1 Answers1

3

The only way your compiler will miscompile that is if you deliberately defined scanf() as a do-nothing macro. Don't blame your compiler!

There is probably a previous scanf() call in the program that left a carriage return in the input buffer. You can confirm that hypothesis by printing the value of do_math.

Try scanf(" %c", &do_math); (with a space before %c) to discard such whitespace.

(It's also a good idea to pay attention to the return value from scanf().)

200_success
  • 7,286
  • 1
  • 43
  • 74