In this article, we will discuss the gameplay() function but not all only the usages of one dimension, two-dimension, and third dimension character array. Choosing the correct dimension of the array is very important to store information effectively. We have to display a question, four options and store the correct answer to compare the answer provided by the user. Remember these three information are connected to each other if we mismatch the order then the game will go wrong.
Here, we will declare three character array to store questions, four options, and answer in the same order of the array index.
Question is a string which is an array of character and we will ask 10 questions in total. We create a two-dimension character array, the first index is a total number of questions i.e 10 and the second index is the length of a string.
We can't assign string using "=" to the two-dimension array and correct way to use it is strcpy() function.Here, we will declare three character array to store questions, four options, and answer in the same order of the array index.
Question is a string which is an array of character and we will ask 10 questions in total. We create a two-dimension character array, the first index is a total number of questions i.e 10 and the second index is the length of a string.
char question[10][250];
strcpy(question[0], "This is question no 1");
strcpy(question[1], "This is question no 2");
strcpy(question[2], "This is question no 3");
strcpy(question[3], "This is question no 4");
strcpy(question[4], "This is question no 5");
strcpy(question[5], "This is question no 6");
strcpy(question[6], "This is question no 7");
strcpy(question[7], "This is question no 8");
strcpy(question[8], "This is question no 9");
strcpy(question[9], "This is question no 10");
Next, we create a three-dimension array to store four options for each question. A first index is a number of questions i.e 10, a second index is a number of options i.e 4 and a third index is the length of a string.
char option[10][4][50] = {
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
};
Now, we create a one-dimension array to store the answer corresponding to the questions. char answer[10]={'a','b','c','d','a','b','c','d','a','b'};
Here is the main menu code for the gameplay() function used in this articlevoid gamePlay(){
system("cls");
char question[10][250];
strcpy(question[0], "This is question no 1");
strcpy(question[1], "This is question no 2");
strcpy(question[2], "This is question no 3");
strcpy(question[3], "This is question no 4");
strcpy(question[4], "This is question no 5");
strcpy(question[5], "This is question no 6");
strcpy(question[6], "This is question no 7");
strcpy(question[7], "This is question no 8");
strcpy(question[8], "This is question no 9");
strcpy(question[9], "This is question no 10");
char option[10][4][50] = {
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
};
char answer[10]={'a','b','c','d','a','b','c','d','a','b'};
char letter[4]={'a','b','c','d'};
for(int i=0;i<10;i++){
printf("%s\n",question[i]);
for(int j=0;j<4;j++){
printf("%c) %s\n",letter[j],option[i][j]);
}
printf("Answer is: %c\n",answer[i]);
}
}
In the next part of the article, we will finalize the gameplay() function and complete the quiz game.
No comments:
Post a Comment