I dont understand why i have to press enter twice when entering an integer?
I assume its due to the (fgetc(stdin) != '\n');
function.This is used so that when letters are input it stops it from infinitely looping.
How can I avoid having to press enter twice to get output?
Im just trying to validate if a number has been entered so if letters are entered then it will prompt the user to re-enter input
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
double val;
char follow;
int count = 0;
int main(void) {
while (count < 1) {
printf("enter an number \n");
int read = scanf_s("%lf%c", & val, & follow);
while (fgetc(stdin) != '\n'); //prevent unlimited loop with character. Potential reason for pressing enter twice.
count = 0;
if (read == 2) {
if (isspace(follow)) {
printf("input is an integer followed by whitespace, accept \n");
printf("%lf", val);
count = count + 1;
} else {
printf("input is an integer followed by non-whitespace, reject");
}
} else if (read == 1) {
printf("input is an integer followed by EOF, accept");
printf("%lf", val);
count = count + 1;
} else {
printf("input is not an integer, reject");
}
}
}