c - sentinel controlled loop does not work -


why wont sentinel controlled loop work?

i should able enter many names when enter -1 should terminate.

can point me in proper direction?

#include <stdio.h> #include <string.h>  int main() {   char namedata[50];    int n, count = 0, names = 1;    while (names > 0)   {     printf("enter family member name:\n");     scanf("%s", &names);     printf("name:");     puts(namedata);      if (names > 0)     {       namedata[count] = names;       count = count + 1;     }   }    if (strcmp(names, "crystal") == 0)   {     printf("crsytal cool");   }    return 0; } 

your program has numerous problems. i'm lazy explain them , suggest fix them.

i've rewritten code:

#include <stdio.h> #include <string.h>  int main(){     char namedata[100][50]; /* 2d array of 100x50 bytes size                                can hold upto max of 100 strings                                each of max 50 bytes */      char temp[50]; /* temp array scan input */      int count = 0, i;      while (count < 100) /* loop until there no more space store input */     {          printf("enter family member name:\n");         scanf("%49s", temp); /* scan in input (maximum of 49 chars, +1 '\0') */          if (strcmp(temp, "-1") == 0) /* if user typed -1 */         {             break; /* break out of loop */         }          /* otherwise */          printf("name:");         puts(temp);        /* print input */          /* copy input last of location of array */         strcpy(namedata[count], temp);           /* increment count */         count++;       }      for(i = 0; < count; i++) /* loop in each index of array names stored */     {         if (strcmp(namedata[i], "crystal") == 0) /* if match found */         {             printf("crsytal cool");         }     }      return 0;    } 

if not want have fixed size here char namedata[100][50];, need dynamically allocate memory via malloc/calloc/realloc.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -