Home > Software engineering >  How do i put a word into an array
How do i put a word into an array

Time:08-19

so this is part of a kind of menu, the only problemis that the word is not getting into the array "frase" i have already tried with frase [ ] = "the word" but idk why it wont work

if(lvl==1)
    {
        printf("lvl 1\n");
        if (opc==1)
        {
            printf("Animales\n");
            a = rand() %3   1;
            printf("%d", a);
            if (a=1)
                frase  <= "pato";
            if (a=2)
                frase <="ganso";
            if (a=3)
                frase <= "avispa";
        }
        if (opc==2)
        {
            printf("comida\n");
            a = rand() %3   1;
            if (a=1)
                frase <="pasta";
            if (a=2)
                frase <="pizza";
            if (a=3)
                frase <="pastel";
        }
        if (opc==3)
        {
            printf("paises\n");
            a = rand() %3   1;
            if (a=1)
                frase <="peru";
            if (a=2)
                frase <="brasil";
            if (a=3)
                frase <="egipto";
        }
    }

    

`

CodePudding user response:

I suggest you solve this by modeling your data. In this case with a array of structs. Then you index into to obtain the relevant data:

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

int main() {
    struct {
        const char *opc;
        const char **frase;
    } data[] = {
        {"Animales", (const char *[]) { "pato", "ganso", "avispa" }},
        {"comida", (const char *[]) { "pasta", "pizza", "pastel" }},
        {"paises", (const char *[]) { "peru", "brasil", "egipto" }}
    };
    srand(time(0));
    int opc = rand() % 3;
    printf("lvl 1 %s %s\n", data[opc].opc, data[opc].frase[rand() % 3]);
    return 0;
}

If you have a lot of data put the data in a file and write a function to build the struct at start-up. A special case of this approach is to store the data in a lightweight database like SQLite, then you can query for the relevant data at run-time or load it all it upon start-up.

You many no longer need to copy the frase, but if you want to use a strcpy:

char frase[100];
strcpy(frase, data[opc].frase[rand() % 3]);

CodePudding user response:

Multiple things to be improved in the code. The if(a=1) should be changed to ==. Not sure what you mean by frase<="pato", strcpy or strncpy should be used. Please refer the following sample code.

void copytoarray(char *array, char *word, unsigned int len)
{
    if(array == NULL || word == NULL)
    {
        return;
    }
    strncpy(array, word, len);
}

int main(void) {
    char frase[15] = {'\0'};
    int a, lvl =1;
    int opc =1;
    if(lvl==1)
    {
        printf("lvl 1\n");
        if (opc==1)
        {
            printf("Animales\n");
            a = rand() %3   1;
            printf("%d\n", a);
            if (a==1)
                copytoarray(frase, "pato", strlen("pato"));
            if (a==2)
                copytoarray(frase, "ganso", strlen("ganso"));
            if (a==3)
                copytoarray(frase, "avispa", strlen("avispa"));
        }
    }
    printf("Word: %s\n ",frase);
}
  • Related