Home > Back-end >  How can values saved in arrays be tested in an if condition?
How can values saved in arrays be tested in an if condition?

Time:04-02

I was creating a quiz program in c which asks for questions from an instructor, as well as answers and stored them in to separate arrays say “questions” and “answers”. The screen is cleared and the questions are displayed to the student and the student’s answers are saved in another array, say “std_answers”. Then, values stored in “answers” are tested with values stored in “std_answers” and whenever there’s a match, the students score on 3 is incremented. (Questions to be stored are in the form "4*8/6" or "65 8/7"...or something like that)

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

int main(){
    int i,question[i][20],answers[i][20],std_answers[i][20],score;

    /*demands and saves questions and answers into
      the "question" and "Answers" arrays.*/

    for(i=0; i<3; i  ){
        printf("Enter question %d: \n",i);
        scanf("%s",&question[i]);
        printf("Enter the answer for question %d: ",i);
        scanf("%s",&answers[i]);
    }

    //hides all previous entries.
    system("cls");
    i=0;

    //Asks questions stored in
    // the"question" array.

    for(i=0; i<3; i  ){
        printf("-------------------------");
        printf("\n What is the answer to: \n");
        printf(" %s \n", question[i]);
        scanf("%d",&std_answers[i]);

        //tests the answers.
        if(answers[i]==std_answers[i]){
            score=score 1;
            printf("CORRECT Answer! \n\n");
            printf("\n-------------------------");
        }
        else{
            printf("WRONG Answer! \n");
            printf("-------------------------");
        }
    }
    printf("You scored %d on 3",score);
    return 0;
}

At the point where I was supposed to test values stored in “std_answers” with “answers” to find matches, the was to syntax error, but when I executed the code, it kept on saying “WRONG Answer” even when it matches.

CodePudding user response:

This line is very wrong.

 int i,question[i][20],answers[i][20],std_answers[i][20],score;

lets reorg it first

 int i; 
 int question[i][20];
 int answers[i][20];
 int std_answers[i][20];
 int score;

the 3 arrays (question, answers, std_answers) are set to be 'i' by 20. Well you didnt specify a value for i' , it could be 0, it could be 4765895. I assume the 20 is to allow questions that are 20 chars long (seems a bit short)

second you want to store questions in 'question' array, but it is declared as an int.

third why is answers i * 20, the answeres are numbers not strings, so you just need 'i' of them. same for std_answers

later you loop asking for 3 questions, so why do you try to make variable size array here. Cleaned up

after the #inlcudes lets have

 #define NUMBER_OF_QUESTIONS 3

then

 char question[NUMBER_OF_QUESTIONS][100];
 int answers[NUMBER_OF_QUESTIONS;
 int std_answers[NUMBER_OF_QUESTIONS];
 int score = 0;

then

for(int i=0; i<NUMBER_OF_QUESTIONS; i  ){
    printf("Enter question %d: \n",i);
    scanf("           
  • Related