4

Im trying to make a basic C console application calculator. Yet when i execute it the second scanf command is skipped and the third is run instead. This is a problem as here i need to get operation of the user +, -, *, or /. How do i stop this from happening?

float num1;
char sign;
float num2;
float total;
printf("~~~ Calculator ~~~\n");
printf("Please enter the first number: ");
scanf("%f", &num1);//Get value of num1 from user

printf("\nNow please enter the operation , either +, -, *, or / : ");
scanf("%c", &sign);//Get value of sign from user 

printf("\n\nFinaly enter the second number: ");
scanf("%f", &num2);

Edit: Actually after trying various suggestions it seams a space before the %c was the correct way and cleanest way of fixing things. Thanks for the help.

  • The second scanf is not skipped. Think about this: the first scanf scans "%f" which does not include the newline that you typed. The second scanf scans "%c" which of course should read the newline character. For this kind of input I recommend fgets + sscanf instead. – Brandin Dec 22 '13 at 21:03
  • 4
    This is a duplicate of goodness only knows how many questions. The problem is finding a good one to tag as the duplicate. I see 5 probable contenders in the 'related' list, but none of them looks like a good choice. – Jonathan Leffler Dec 22 '13 at 21:12

4 Answers4

2

Put a blank character before %c so it will consume the new line character like this:

scanf(" %c", &sign);//Get value of sign from user
opc0de
  • 11,557
  • 14
  • 94
  • 187
  • When i did this the console just kept on expecting an input and didn't output the next line of text as i hoped –  Dec 22 '13 at 21:17
  • @Mikey This _should_ have worked. Were you expecting this `scanf(" %c", &sign)` to _immediately_ return once a character like '+' was typed? – chux - Reinstate Monica Dec 22 '13 at 22:28
1

After you hit enter, the first scanf consumes the actual number and leaves the \n. The latter is consumed in the next scanf.

scanf("%f\n", &order)

Do that in order to consume the \n you are entering.


As @JonathanLeffler mentioned, it's better approach to use " %c" (Note the space) in the next line in order to consume the newline character.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Not so much 'skips the newline' as 'leaves the newline in the input to be read by the next operation'. And (I should have said earlier), the best fix is probably to use `" %c"` to read the next character that is not white space. Putting `\n` at the end of a format string is really nasty if a user is going to be typing because the input does not end until the user types a character that isn't white space. – Jonathan Leffler Dec 22 '13 at 21:08
1

After the program asking for the first number you enter a number the you hit return. So the the number is stored in the variable num1. But the new line character '\n' still in the buffer you need to clear the buffer before the second scanf is executed Otherwise the signe variable will accept that '\n' as an input because it's a character and it will not prompt for an input.

You should always clear the buffer before asking to input a char

int c;
do {
    c = getchar(); // clearing the buffer
} while (c!='\n' && c!=EOF);
printf("\nNow please enter the operation , either +, -, *, or / : ");
scanf("%c", &sign);//Get value of sign from user

Now it shouold work

rullof
  • 7,124
  • 6
  • 27
  • 36
-2

Actually, this happens because there is still a value in the stdin stream of the program. An easy solution would be using:

fix: DON'T USE -> fflush(stdin);

Consume the newline char by using scanf("%f\n")...

before the second call to scanf...

Best!

Daniel Trugman
  • 8,186
  • 20
  • 41
  • http://stackoverflow.com/questions/2979209/using-fflushstdin – Mat Dec 22 '13 at 21:05
  • 4
    `fflush (...)` is undefined for input streams (for which, ***stdin*** is one). – Andon M. Coleman Dec 22 '13 at 21:06
  • Thanks, never noticed this one before. – Daniel Trugman Dec 22 '13 at 21:13
  • It is hard work cleaning up the mess. Both Windows and Linux define the behaviour of `fflush(stdin)` and (miracle of miracles) it does the same sane sensible operation, but neither the C standard nor POSIX defines the behaviour. Standards are great, but come with caveats. Read the manual page for your system. – Jonathan Leffler Dec 22 '13 at 21:15