I’m trying to write a piece of code that will read user input, store it inside a double or a char depending on the input’s type. It works fine, unless I input “i”, “j”, “I” or “J”. These characters are read correctly if the first scanf looking for doubles does not run. What’s happening?
Here is my main loop (for testing purposes):
int main(void) {
char c;
double d;
while (1) {
if (scanf("%lf", &d) && 0) {
printf("Double: ");
printf("%lfn", d);
} else {
scanf("%c", &c);
printf("Char: ");
printf("%cn", c);
}
}
return 0;
}
New contributor
nudebarber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.