How to save questoes in an array of pointers

Asked

Viewed 42 times

1

Good night

I come here to try to dispel a programming problem that occurs to me. In this solution, I have a file with several questions, and each question can be a matter of one of the 3 following themes:

  1. GEO - Geography
  2. MAT - Mathematics
  3. HIS - History

If the user in the execution of the program chooses to answer geography questions, then a method that selects only geography questions will be invoked. This method has among other code the following:

while (fgets(pergunta, 150, f) != NULL) // ler ficheiro linha a linha
    {
        sscanf(pergunta,"%ld", &elapsed_seconds); // ler cada poergunta do ficheiro
        length = sizeof(pergunta) - 4; // pegar no tamanho dos primeiros 4 caracteres

        for (int j = 0; j < length; j++)
        {
            if (pergunta[j] == 'G' && pergunta[j + 1] == 'E' && pergunta[j + 2] == 'O') // se a pergunta é de GEO
            {
                for (int i = 0; i <= sizeof(questoes); i++)
                {

                    //Problema 
                    if(!questoes[i]) // se encontrar posicao vazia
                        questoes[i] = pergunta; // guarda pergunta no apontador

                    if (i == sizeof(questoes))
                    {
                        resultadoRetomado = 1;
                        break;
                    }
                }
            }
            else if (pergunta[j] != 'G' && pergunta[j + 1] != 'E' && pergunta[j + 2] != 'O')
            {
                pergunta[j] = '\0';
            }
        }

        }

Problem:If a geography question is found, the pointer char* questoes[50], passed as parameter to the method, it will be covered and the first question found will be stored. I intend and tried to change this algorithm to check if there is any empty position in the pointer set.

Objective: I intend to keep in char* questions[50] a set of 50 questions. How can I change this code to go through the file and save the 50 questions?

From now on Thank you,

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.