while ((eat = getchar()) != '\n' && eat != EOF); // where 'eat' is long long eat
This is the line I'm using.
The issue, you ask ? Aha ! I have to press [ENTER]
twice, to actually 'send' the input to my program. (I want this to be in a function that works after both scanf
and fgets
)
This seems reminiscent of using a getchar()
alone, but I'm a newbie and I would just solve those by removing the getchar()
, which of course cannot be done here.
I'm quite confused what to do and if it is possible to implement a simple solution that doesn't force the user to press the [ENTER]
key twice.
A bigger, better code snippet :
int num,q=0; static int pins[8]; char pswd[9]; char bad =1,Y_N;
while(bad == 1) {
printf("\nEnter (8 char) Password : ");
getchar();
fgets(pswd,sizeof(pswd),stdin);
int len = strlen(pswd);
if(pswd[len-1] == '\n'){
pswd[len-1] = '\0';
len--;
}
if(len < 8)
printf("\nPassword less than 8 characters.\nPlease Retry.\n");
else
bad = 0;
}
while ((eat = getchar()) != '\n' && eat != EOF);
EDIT :
I made a stupid mistake, and this error is not being caused by the loop itself but because of the code where the function (that this code is part of) gets called. An extra getchar()
in that place has caused this error. It is solved by removing that extra getchar()
. Thanks to all those who helped me troubleshoot this, I apologise to you all.