Strings and Arrays, problems getting the right result

0

The purpose of the program is to display information in one option and a 10-question inquiry in another option. Each discipline has 10 questions, and each discipline is done in a subprogram. the problem is the use of the 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 ", certas);
        }
        return;
        }
    
asked by anonymous 21.01.2018 / 19:06

2 answers

1

Jose, working with strings and char in c is very annoying, but with some tricks you'll get used to.

Although you have not explained very well, what I imagine should be happening is that you are "skipping" some scanf() , this is because whenever you use scanf() to read an integer your buffer receives a '\ n', then your program waits for where to insert this '\ n', then when it arrives in your scanf() to read what will be inserted into that string / char variable '\ n'

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

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

I hope to have helped, hugs.

    
22.01.2018 / 12:47
0

You did not specify what your problem was!

I would comment but as I do not have the necessary permission, 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);
}
    
21.01.2018 / 20:11