0

I need to make a program that takes any amount words from users, then prints those words out with an 's' appended to the end.

The loop should run until the user enters in an empty string (hits ENTER). I know I could do this without making a multidimensional array, but doing it will help me understand my next project.

I'm running into the problem that it only allows 3 inputs from the user before terminating the loop, and I don't understand why.

If I change the #define MaxWord to 20 or 50, it only allows 1 input from the user before terminating the loop. Can someone help me out?

#include <stdio.h>
#include <string.h>

#define MAX_WORDS 1
#define MAX_LEN 50

int main()
{

    char input1[MAX_WORDS][MAX_LEN];
    int i = 0;

    do
    {
        printf("Print your word: \n");
        gets(input1[i]);
        if (strlen(input1[i]) != 0)
        {
            printf("Here is your word: %ss\n", input1[i]);
            i++;
        }

    } while (strlen(input1[i]) != 0);

    printf("Complete");

    return 0;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 4
    if `MAX_WORDS` is 1, why would you expect to be able to take more than one input? Everything after that first one invokes *undefined behavior* – UnholySheep Oct 14 '20 at 22:17
  • 1
    `gets` function was deprecated in the C99 standard and removed with C11, don't use it, it's a dangerous function completely vulnerable to destination buffer overflow. `fgets` is regularly used instead. – anastaciu Oct 14 '20 at 22:24
  • As the code is now, it accepts a total of three inputs before stopping, but if i change MAX_WORDS to any other number, such as 20, it only allows one input. – MomoBean Oct 15 '20 at 00:52
  • You test `strlen(input[i])` after incrementing `i`, `input[i]` is now an unintialized array. – HAL9000 Oct 15 '20 at 01:23

0 Answers0