c - scanf is not waiting for input -


i'm trying find bug here, still don't it. i've been debugging , googling , found close topics, there solutions don't need atm, , i'm curious why code not working:

#include <stdlib.h> #include <string.h> #include <stdio.h>  #define buffer 256  int main() {        int missioncode;     char *desc = (char*)malloc(sizeof(char)*buffer);      {         printf("please enter mission code (or -1 exit): ");         scanf("%d", &missioncode);         fflush(null);         if (missioncode==-1)             return 1;     } while (missioncode>10);      {         printf("please enter string:\n");         scanf("%[^\n]s", desc); //it doesn't stop here!         fflush(null);         if (!strcmp("exit",desc))             return 1;     } while (strlen(desc)<20);      printf("your string:\n%s", desc);     return 0; } 

there's wrong scanf\flushall in second loop, don't find out what. btw, c ofcourse.

scanf("%d", &missioncode); 

leaves newline in buffer, so

scanf("%[^\n]s", desc); 

immediately finds 1 , stops. can add space

scanf(" %[^\n]s", desc); 

to format skip initial whitespace.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -