Why does scanf not read white spaces?
Also in my code if I use scanf first then fgets or scanf the second time after a few lines, as you can see in the code, then if I give an input that has a space like, "Hey How are u" then my code loops, why so?
I fixed it by just using fgets
while(1)
{
entry=&entry_var;
*entry=0;
printf("\n++++++++DFS CLIENT MENU++++++++");
printf("\n1- ENTER THE COMMAND");
printf("\n2- EXIT\n");
/*instance 1:if I use scanf here then whether i use scanf or fgets the
second time it loops in *entry==1 */
fgets (command, sizeof(command), stdin);
*entry=atoi(command);
printf("Entry: %d", *entry);
if(*entry==1)
{
printf("\n--------COMMANDING ZONE--------");
printf("\nInput the Function: ");
//This is the second instance
fgets (command, sizeof(command), stdin);
//scanf("%s",command);
printf("\n%s",command);
command_parse(command);
}
else if(*entry==2)
{
break;
}
}
scanfis for reading and parsing tokens like numbers and words. Spaces are the separators between the tokens, it generally skips over them. - Barmarwhile (getchar() != '\n');should do it. - PC Ludditefgets()to read lines of data; usesscanf()when you want to parse the line of data. It has many advantages, including that it avoids the problems you're running into. - Jonathan Leffler