loops - fgets in C language multiple prints after get string -


i have code , want loop finish when user gives "###".

int main(){     char s[10];      printf("give string: ");     fgets(s,11,stdin);     do{         printf("give string: ");         fgets(s,11,stdin);     }while ( s!="###" );     return 0; } 

so far it's ok, when user gives input bigger 11 characters have multiple prints of "give string". try scanf , did right. can give me solution fgets? i mean output looks this.

c string doesn't support direct comparison, need strcmp that, so

while ( s!="###" ) should while(strcmp(s,"###"))

also you'd need remove \n fgets (so strcmp ignore \n). do..while should like:

do{     printf("give string: ");     fgets(s,11,stdin);     s[strlen(s) - 1] = '\0';  } while (strcmp(s,"###")); 

Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -