0

I'm passing a 2D array as an argument. Reviewing the array in main shows that I have 4 separate strings of words each being stored in words[1][], words[2][], etc. However, when I pass that array to the function I am using to append plural endings, I only seem to be able to access the first string in the array.

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

#define ROWS 50
#define COLS 50

void readFileFormat(char words[ROWS][COLS]);
void determinePlurals(char words[ROWS][COLS]);

int main()
{
    char words[ROWS][COLS];

    readFileFormat(words);
    determinePlurals(words);

    return 0;
}

void readFileFormat(char words[ROWS][COLS])
{
    int cols = 0;//consistent cols for 2d array

    FILE* nounFile;

    fopen_s(&nounFile, "plural.txt", "r");

    if (nounFile == NULL)
    {
        printf("Error reading file; try again.");
        return 1;
    }

    while (!feof(nounFile))
    {
        for (int rows = 0; rows < ROWS; ++rows)
        {
            fgets(&words[rows][cols], 50, nounFile);
            words[rows][strcspn(words[rows], "\n")] = 0;
        }
    }
    
    fclose(nounFile);
}

void determinePlurals(char words[ROWS][COLS])
{
    char* iesEnd = "ies";
    char* esEnd = "es";
    char* sEnd = "s";

    for (int rows = 0; rows < ROWS; ++rows)
    {
        if (strcmp(words[rows] + strlen(words[rows]) - 1, "y") == 0)
        {
            printf("%s: ", words);
            words[rows][strlen(words[rows]) - 1] = '\0';
            strncat(words[rows], iesEnd, 3);
            printf("%s\n", words);
            rows++;

        }
        else if (strcmp(words[rows] + strlen(words[rows]) - 2, "ch") == 0
            || strcmp(words[rows] + strlen(words[rows]) - 2, "ss") == 0
            || strcmp(words[rows] + strlen(words[rows]) - 2, "x") == 0
            || strcmp(words[rows] + strlen(words[rows]) - 2, "sh") == 0)
        {
            printf("%s: ", words);
            strncat(words[rows], esEnd, 2);
            printf("%s\n", words);
            rows++;
        }
        else
        {
            printf("%s: ", words);
            strncat(words[rows], sEnd, 1);
            printf("%s\n", words);
            rows++;
        }
    }       
}

I've tried iterating through each row, but don't seem to have acccess to the rest of words[][]. I've struggled with multidimensional arrays so I'm looking for a bit of assistance.

James Risner
  • 5,451
  • 11
  • 25
  • 47
SqweepS
  • 3
  • 3
  • Your compiler will be giving you many, many warnings about wrong types here. You need to fix those. Also, please include all your code. You did not show us readFileFormat(). – pmacfarlane Dec 17 '22 at 23:31
  • I'm only getting a warning for using an int return on 'readFileFormat' or that I'm using strncat instead of strncat_s. Also, updated and added my 'readFileFormat' fuction. – SqweepS Dec 17 '22 at 23:54
  • 1
    see https://stackoverflow.com/q/5431941/1216776 – stark Dec 17 '22 at 23:55
  • The many problems in this code demonstrate that you've first typed the program, and now hope to debug it (or have someone re-write it for you.) eg #1 "loading words" The program will keep calling `fgets()` until it has called the function 50 times. Fix that. eg #2, "row++"... The function increments the row counter in each 'case' and increments it again as the last term of the `for()` loop... Start over with a program that simply loads each line and displays it. Test and debug before adding more functionality like arrays and then checking/changing to make words plural. – Fe2O3 Dec 18 '22 at 01:19
  • You're right, I did dive in with limited to no design. Apologies as the goal was not to have someone do the work for me. Appreciate the feedback regardless. – SqweepS Dec 18 '22 at 02:35

0 Answers0