'Scanf doesn't work as expected in VS Code [duplicate]
I practice a simple example to input operator in C: The code as here:
#include <stdio.h>
int main() {
int a,b;
char opera;
printf("input 2 integer number ");
scanf("%d %d",&a,&b);
printf("\n input the operator: ");
scanf("%c", &opera);
switch (opera)
{
case '+':
printf("result is %d \n", a+b);
break;
default:
break;
}
}
Problem: Terminal will pass the input operator
input 2 integer number
4 5
input the operator:
PS D:\Quang\3. Study\C\Bai 2\.vscode>
But if I input operate first, it work:
#include <stdio.h>
int main() {
int a,b;
char opera;
printf("\n input the operator: ");
scanf("%c", &opera);
printf("input 2 integer number");
scanf("%d %d",&a,&b);
switch (opera)
{
case '+':
printf("result is %d \n",a+b);
break;
default:
break;
}
}
Result:
input the operator: +
input 2 integer number 4 5
result is 9
Anyone has the same issue with VS Code?
Solution 1:[1]
Well after scanning the two integers, the stdin buffer is not empty a '\n' new line char is left there, so after reading one char to be the operator, you actually read that new line char, so you can fix that by making a custom flush function, that just reads the chars left in the stdin like this:
#include <stdio.h>
// make stdin buffer empty
void flush() {
int c;
while(1) {
c = fgetc(stdin);
if(c == EOF || c == '\n') break;
}
}
int main() {
int a, b;
char opera;
printf("input 2 integer number ");
scanf("%d %d",&a,&b);
flush();
printf("input the operator: ");
scanf("%c", &opera);
// I have added other operators
switch (opera) {
case '+': printf("result is %d", a + b); break;
case '-': printf("result is %d", a - b); break;
case '/': printf("result is %d", a / b); break;
case '*': printf("result is %d", a * b); break;
case '%': printf("result is %d", a % b); break;
default: printf("unknown operation");
}
}
or simply just read the new line char with the scanf before reading the actual operator like this:
#include <stdio.h>
int main() {
int a, b;
char opera;
printf("input 2 integer number ");
scanf("%d %d",&a,&b);
printf("input the operator: ");
// read the new line char before reading the operator
scanf(" %c", &opera);
// I have added other operators
switch(opera) {
case '+': printf("result is %d", a + b); break;
case '-': printf("result is %d", a - b); break;
case '/': printf("result is %d", a / b); break;
case '*': printf("result is %d", a * b); break;
case '%': printf("result is %d", a % b); break;
default: printf("unknown operation");
}
}
Result:
input 2 integer number 4 5
input the operator: *
result is 20
Solution 2:[2]
Well that is because you enter 2 numbers at first...
input 2 integer number 4 5
at the end, you press enter key. So this '\n' character is stored in input buffer... When your next statement executes :
scanf("%c", &opera);
this input is fulfilled by '\n' already present in buffer. This causes skipping of input.
SOLUTION :-
Use the below statement.
scanf(" %c",&opera); // Any extra spaces or newline will be discarded...
You wanna read this :-
Solution 3:[3]
There are two methods to solve the issue:
- Use
fflush(stdin)just before thescanf(..., &opera)statement. If you don't want to follow the aforementioned step, just leave a whitespace before
%ccharacter ofscanf(..., &opera), something like:scanf(" %c", &opera);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Pirthvi Hasan G |
| Solution 3 | Rohan Bari |
