0

The code is suppose to take in 6 words starting with a, b or c and put them in to each respective array. Only problem is that it doesn't store anything at all no matter what I do. The printf in the last part of the code is suppose to spit everything that I typed in out but it's giving me nothing but blanks...

#include <stdio.h>
int main()
{
char startswithA[6][10] = {};
char startswithB[6][10] = {};
char startswithC[6][10] = {};
char holder[6][10] = {};

printf("Enter a word starting with a, b or c (write 2 of each)\n");
for (int i = 0; i < 6; i = i + 1)
{
    printf("Now entering word #%d\n", i+1);
    scanf_s("%s", &holder[i]);
    if(holder[i][0] == 'a')
    {   
        for(int a = 0; a < 10; a = a + 1)
        {
            holder[i][a] = startswithA[i][a];
        }
    }
    else if (holder[i][0] == 'b')
    {
        for(int a = 0; a < 10; a = a + 1)
        {
            holder[i][a] = startswithB[i][a];
        }
    }
    else if (holder[i][0] == 'c')
    {
        for(int a = 0; a < 10; a = a + 1)
        {
            holder[i][a] = startswithC[i][a];
        }
    }
    else
    {
        printf("something out of bound was typed in");
    }
}
for(int b = 0; b < 6; b = b + 1)
{
    printf("%s %s %s\n", startswithA[b], startswithB[b], startswithC[b]);
}

return 0;
}
lmgesus
  • 9
  • 4
  • Besides that issue with swapping the names in your assignments, there are some bugs with reading your input: `scanf_s` requires an extra parameter specifying the length of your input buffer. See [String input using C scanf_s](https://stackoverflow.com/questions/23378636/string-input-using-c-scanf-s) for more details. Not providing that parameter causes undefined behaviour. Also `holder[i]` is an array of `char` that decays to a pointer to `char`. You should remove the `&` from it. – Gerhardh Sep 15 '22 at 09:29
  • @Gerhardh that's odd, my professor never said anything about that part for scanf_s being mandatory. what kind of bugs are we talking about here. – lmgesus Sep 20 '22 at 03:05
  • Whatever your professor tells you, you should always read up the documentation of functions you are using to get familiar. As you can see in the link I provided, that parameter is mandatory for strings. The function also has no mechanism to know you did not provide it. A likely result is that it will just take the value that it finds in the place where the buffer size is expected, i.e. in a certain place on the stack or in a register. As you did not put anything there, it will read garbage resulting in limiting the string to less than buffer size or to allow overflow with too large length. – Gerhardh Sep 20 '22 at 04:55

1 Answers1

1

Looks like it is typo

holder[i][a] = startswithA[i][a];

Did you mean?

startswithA[i][a] = holder[i][a];
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44