1

I am trying to get names stored in a string array as user input. When I do this, the program returns an empty element at the beginning of the array, and the last element the user inputs is not stored.

The code works if I change the condition in the for loops to <= size, but I still want to understand where the empty element comes from. This is what I get when I run the code

#define MAXNAME 81

int main() {
    int size, i;

    printf("Input the number of names in the array: ");
    scanf("%d", &size);
    // we scan the elements to be stored in the array
    printf("Enter %d names to the array:\n", size);
    char names[size][MAXNAME];
    for (i = 0; i < size; i++) {
        gets(names[i]);
    }
    printf("\n");
    // we print the elements stored in the array
    int p;
    printf("The elements in the array are:\n");
    for (p = 0; p < size; p++) {
        printf("names[%d] = %s\n", p, names[p]);
    }
    printf("\n");
    // we search for the max length of the elements in the array
    int maxLength = 0;
    int j, k;
    for (j = 0; j < size; j++) {
        int testLength = strlen(names[j]);
        printf("The length of %s is %d\n", names[j], testLength);
        if (testLength > maxLength) {
            maxLength = testLength;
        }
    }
    printf("\n");
    printf("The maximum length is %d\n", maxLength);
    // we print the elements with size == max length
    printf("The element(s) with this length is(are):\n");
    for (k = 0; k < size; k++) {
        int compareLength = strlen(names[k]);
        if (compareLength == maxLength) {
            printf("%s\n", names[k]);
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Nina
  • 33
  • 5
  • 1
    You're reading the newline (following the input number) as if it was the first, empty name. (Also, get away from using `gets()` - it cannot be used safely and should be considered unusable). – Toby Speight Jun 24 '19 at 14:30
  • [Input in C. Scanf before gets. Problem](//stackoverflow.com/q/2366509) – 001 Jun 24 '19 at 14:37
  • gets() is the main cause of many problems such as this one. it's safer to use scanf() instead of gets() to avoid this little annoying dis-informations. – Benny Jun 24 '19 at 15:22
  • Your output is plain text. Please do not add screenshots of text. Instead just copy&paste the text into the question. Also links to external resources should not be required to get all necessary details of your question. – Gerhardh Jun 24 '19 at 16:41

1 Answers1

1

In this code, scanf() reads an integer and leaves a newline character in buffer. So gets() only reads a newline. We can solve this by adding a getchar() after scanf() to read an extra newline.

For more information, see GeeksforGeeks article.

Updated code:

#define MAXNAME 81

 int main(){
    int size, i;
    char names[size][MAXNAME];
    printf("Input the number of names in the array: ");
    scanf("%d", &size);
    getchar();
    // we scan the elements to be stored in the array
    printf("Enter %d names to the array:\n", size);

    for(i = 0; i < size; i++){
        gets(names[i]);
    }
    printf("\n");
    // we print the elements stored in the array
    int p;
    printf("The elements in the array are:\n");
    for(p = 0; p < size; p++){
        printf("names[%d] = %s\n",p, names[p]);
    }
    printf("\n");
    // we search for the max length of the elements in the array
    int maxLength = 0;
    int j, k;
    for(j = 0; j < size; j++){
        int testLength = strlen(names[j]);
        printf("The length of %s is %d\n", names[j], testLength);
       if(testLength > maxLength){
            maxLength = testLength;
        }
    }
    printf("\n");
    printf("The maximum length is %d\n", maxLength);
    // we print the elements with size == max length
    printf("The element(s) with this length is(are):\n");
    for(k = 0; k < size; k++){
        int compareLength = strlen(names[k]);
        if(compareLength == maxLength){
            printf("%s\n", names[k]);
        }
    }
    return 0;
}
Christina Jacob
  • 665
  • 5
  • 17
  • The first fix doesn't do what we want: a whitespace in the format string means *consume all available consecutive whitespace*, so the `scanf()` will block waiting for more input, until a non-whitespace character is entered. – Toby Speight Jun 24 '19 at 15:39
  • Verified. Working. "so the scanf() will block waiting for more input, until a non-whitespace character is entered." Isn't this what we wanted? Scanf to consume all the white spaces before a letter is entered? – Christina Jacob Jun 24 '19 at 15:51
  • @ChristinaJacob: adding a trailing space in the `scanf()` statement will cause non empty input to be required before the `printf("Enter %d names to the array:\n", size);` executes, that is before the prompt is emitted, which is definitely not what the OP wants. – chqrlie Jun 26 '19 at 12:47
  • Got the point. Updating the answer. – Christina Jacob Jun 26 '19 at 15:23