Strings and Arrays, problems getting the right result

Asked

Viewed 679 times

0

The goal of the program is to show information on one option and a 10 question survey on another option. Each discipline has 10 questions, and each discipline is done in a subprogram. the problem is the use of string.

typedef struct
{
    char pergunta[150];
    char resposta; // a, b, c
} Perguntas;

for example

void discasc(void){
    int valasc,i;
    char resposta;
    char respostas[10];
    int certas = 0;
    Perguntas pITEL [10];
    strcpy(pITEL[0].pergunta,"...?\na. ...\nb. ...\nc. ...");
    pITEL[0].resposta = 'a';

plus ten different questions, and to know the right or wrong one uses the following

printf("Menu de ASC\n\n");
    printf("1 - Informação sobre a Disciplina;\n");
    printf("2 - Teste com perguntas aleatórias;\n");
    printf("3 - Voltar ao menu anterior.\n");
    scanf("%d",&valasc);
    if (valasc<1 || valasc>3)
        printf("O valor inserido não corresponde a nenhuma das opções anteriores.");
    switch(valasc) {
    case 1:
    printf("...");
    case 2:
        for (i=0; i<10; i++){
            resposta=0;
            printf("%s\n",pASC[i].pergunta);
            printf("A sua resposta: ");
            scanf("%c", &resposta);
            respostas[i]=resposta;
        }
        for (i=0;i<10;i++){
            if(pASC[i].resposta=respostas[i]){
                certas++;
            } else {
            printf("A resposta à pergunta %d está errada. A resposta certa é %c", pASC[i].resposta);
            }
            }
            printf("Resultado: %d \10", certas);
        }
        return;
        }
  • care that has a = within the if. This will always turn out to be true, and it will change the value of what’s on the left.

  • Thank you, but the problem is already solved. I thank you anyway.

2 answers

1


José, working with strings and char in c is very boring, but with some tricks you’ll get used.

Although you haven’t explained it very well, what I imagine must be happening is that you are "skipping" a few scanf(), this happens because whenever you use a scanf() to read an integer your buffer gets an ' n', so your program is waiting where to insert that ' n', then when it arrives in your scanf() to read what will be inserted in this variable string/char the ' n' is inserted in it.

What I do to avoid this is whenever I will read an entire variable instead of putting scanf("%d", &varInt), I put scanf("%d%*c", &varInt), this kind of empties your buffer, not occurring this error.

There are some other ways too, if you search here at Stackoverflow you will find several solutions. Here also has a great explanation and several solutions.

Hope I helped, hugs.

0

You didn’t specify your problem!

I was going to comment but as I do not have permission required, there are some errors in your code:

// Aqui são dois sinais de iguais para comparar as respostas.
if(pASC[i].resposta == respostas[i])
{
    certas++;
} 
else 
{
    // Aqui você indicou que passará um inteiro "%d" para o printf mas não passou.
    printf("A resposta à pergunta %d está errada. A resposta certa é %c", i, pASC[i].resposta);
}
  • ok thanks, it is difficult to say what the problem is, but essentially it is when I run the program, it asks the questions but repeats, ie, asks the question (a) I answer and then copies the question (a), asks the question (b) but does not let me answer and still asks the question (c) and let me answer. only with source code could you help me but easily.

Browser other questions tagged

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