void signup(struct user *u)
{ char c_pswd[80];
int i,flag=1;
clrscr();
printf("\n>ENTER FULL NAME: ");
gets(u->name);//scanf("%s",u->name);
printf("\n>ENTER USERNAME: ");
scanf(" %s",u->username);
printf("\n>ENTER DATE OF BIRTH: ");
scanf(" %s",u->dob);
printf("\n>ENTER EMAIL: ");
scanf(" %s",u->email);
printf("\n>ENTER GENDER(M/F): ");
scanf(" %c",&(u->gender));
printf("\n>ENTER MOBILE NUMBER: ");
scanf("%d",&u->mobile_no);
while(flag==1) {
printf("\n>ENTER PASSWORD(ATLEAST 8 CHAR): ");
scanf("%s",u->password);
printf("\n>CONFIRM PASSWORD: ");
scanf("%s",c_pswd);
if(strcmp(u->password,c_pswd)!=0)
{
clrscr();
printf("\t\tPASSWORDS DON'T MATCH ENTER AGAIN...");
}
else {
clrscr();
printf("\n\n\n\n\n\n\n\t\t\tSIGNUP SUCCESFUL!!!");
printf("\n\n\nREDIRECTING TO LOGIN...");
delay(5000);//time delay of 5 seconds
flag=0;//AGAIN GOES FOR PASSWORD INPUT AND VERIFICATION
}
}
}

- 76,821
- 6
- 102
- 177

- 443
- 6
- 19
-
1The _compiler_ does not skip anything. Te compiler is responsible for _generating_ code not for _executing_ it - if anything "skips" anything, then it is _your code_ doing that, – Clifford Oct 21 '17 at 08:41
-
As a rule use `fgets()` on the `stdin` stream rather then the horribly unsafe `gets()`. For the `scanf()` string-with-spaces problem see https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf – Clifford Oct 21 '17 at 08:45
-
What does the code _calling_ `signup()` look like? What is the state of the input buffer prior to calling it? – Clifford Oct 21 '17 at 09:04
-
Consider shortening the title to something more specific and snappy and place the question in the title in the body text. Suggest "Program does not wait on gets() input call". The fact that it is a "student diary program" is largely irrelevant. – Clifford Oct 21 '17 at 09:07
-
Possible duplicate of [How do you allow spaces to be entered using scanf?](https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf) – Bo Persson Oct 21 '17 at 11:33
-
soda, your edit disfigured your post completely. You could do better. – Jens Gustedt Oct 26 '17 at 19:17
-
Perhaps reading the manual page for `scanf` will help. – Ed Heal Oct 26 '17 at 19:17
-
Just use `scanf(" %X[^\n]", Y)` where X is the maximum length of the array to place the string less one, and Y is that array – Ed Heal Oct 26 '17 at 19:19
1 Answers
It is not clear perhaps from the information given why gets()
fails while the commented-out scanf()
call works, however console input is generally line buffered, and if a say some preceding input processing has not consumed the buffered data and the buffer contains a , that buffered line will be accepted as the input without waiting for further input.
For for example if you had:
menu_select = getchar() ;
if( menu_select == 's' )
{
signup( &user ) ;
}
The user may enter s<newline>
but the scanf()
consumes only the s
leaving (\n
) in the buffer, so that in signup()
, the first input call is immediately satisfied as an empty line.
One pattern for dealing with this is to ensure that all input extracts the whole line. In the above case for example:
menu_select = getchar() ;
while( menu_select != `\n` || getchar() != `\n ) ; // empty flush loop
if( menu_select == 's' )
{
signup( &user ) ;
}
Consider encapsulating it:
char inchar()
{
char ch ;
scanf( "%c", &ch) ;
while( ch != `\n` || getchar() != `\n ) ; // empty flush loop
return ch ;
}
The problem occurs with any input that does not process the entire line, not just getchar()
, but also scanf()
and gets()
which is it is not clear if or how your scanf()
version works.

- 88,407
- 13
- 85
- 165