Antreas Petsas Antreas Petsas - 1 year ago 101
C Question

fgets in C language multiple prints after get string

I have that code and I want my loop finish when user gives "###".

int main(){

char s[10];

printf("Give string: ");
printf("Give string: ");
}while ( s!="###" );
return 0;

So far it's ok, but when the user gives an input bigger than 11 characters I have multiple prints of "Give String".
I try it with
and I did it right. Can anyone give me a solution to do it with
I mean the output looks like this.

Answer Source

C string doesn't support direct comparison, you would need strcmp for that, so

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

Also you'd need to remove \n from fgets (so as strcmp to ignore \n). So the do..while should look like:

    printf("Give string: ");
    s[strlen(s) - 1] = '\0'; 
} while (strcmp(s,"###"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download