https://github.com/calcuis/quiz-game-c
quiz with save (as text file) in c
https://github.com/calcuis/quiz-game-c
Last synced: 3 months ago
JSON representation
quiz with save (as text file) in c
- Host: GitHub
- URL: https://github.com/calcuis/quiz-game-c
- Owner: calcuis
- Created: 2023-07-01T05:16:49.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-07-01T18:29:43.000Z (almost 2 years ago)
- Last Synced: 2025-01-21T13:11:54.303Z (5 months ago)
- Language: C
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Quiz Game
The given C code is a simple quiz game that asks multiple-choice questions and allows the user to select an answer. It then checks the user's answer against the correct answer and keeps track of the score. Finally, it displays the final score and stores it in a file named "results.txt".Let's go through the code step by step:
Include necessary header files:
```
#include
#include
```
This code includes the necessary header files for input/output operations (stdio.h) and character handling functions (ctype.h).Define arrays to store questions, options, and answers:
```
char questions[][100] = {"1. What year did the C language debut?: ",
"2. Who is credited with creating C?: ",
"3. Python is written by which language?: "};char options[][100] = {"A. 1969", "B. 1972", "C. 1975", "D. 1999",
"A. Dennis Ritchie", "B. Bill Gate", "C. Tim Cook", "D. Elon Musk",
"A. C", "B. C+", "C. C++", "D. C#"};char answers[3] = {'B', 'A', 'A'};
```
These arrays store the questions, options, and correct answers for each question. The questions array is a 2D character array, where each row represents a question. The options array is also a 2D character array, where each row represents the options for a particular question. The answers array stores the correct answers for each question.Calculate the number of questions:
```
int numberOfQuestions = sizeof(questions)/sizeof(questions[0]);
```
This line calculates the number of questions by dividing the total size of the questions array by the size of a single element (questions[0]). This calculation gives the total number of rows in the questions array.Declare variables for user input and score:
```
char guess;
int score;
```
The guess variable stores the user's answer for each question, and the score variable keeps track of the number of correct answers.Print the game title and open a file for writing:
```
printf("*** QUIZ GAME ***\n");
FILE *pF = fopen("results.txt", "w");
```
This line prints the title of the game. The fopen function opens the file "results.txt" in write mode and returns a pointer to the file (pF).Start a loop to ask questions:
```
for(int i = 0; i < numberOfQuestions; i++){
// ...
}
```
This loop iterates through each question.Print the current question and options:
```
printf("****************************************\n");
printf("%s\n", questions[i]);
printf("****************************************\n");for(int j = (i * 4); j < (i * 4) + 4; j++){
printf("%s\n", options[j]);
}
```
These lines print the current question and its options. The question is printed using the format string %s, and the options are printed using a nested loop. The j loop iterates over the options for the current question, and j is calculated based on the current question index i.Get user's answer and check for correctness:
```
printf("Guess: ");
scanf("%c", &guess);
scanf("%*c"); //clear \n from input bufferguess = toupper(guess);
fprintf(pF, "%c\n", guess);if(guess == answers[i]){
printf("CORRECT!\n");
score++;
}
else{
printf("WRONG!\n");
printf("Answer: %c\n", answers[i]);
}
```
These lines prompt the user to enter their answer, read it using scanf, and store it in the guess variable. The %c format specifier is used to read a single character. The scanf("%*c") is used to clear the newline character (\n) from the input buffer after reading the answer.The toupper function is then used to convert the user's answer to uppercase. This allows the program to handle both uppercase and lowercase answers. The user's answer is stored in the file using fprintf.
The code then compares the user's answer (guess) with the correct answer (answers[i]) for the current question. If they match, the user's answer is correct, and the score is incremented. Otherwise, the answer is wrong, and the correct answer is displayed.
Print the final score and close the file:
```
printf("******************\n");
printf("FINAL SCORE: %d/%d\n", score, numberOfQuestions);
printf("******************\n");fprintf(pF, "FINAL SCORE: %d/%d", score, numberOfQuestions);
fclose(pF);
```
These lines print the final score by displaying the number of correct answers (score) and the total number of questions (numberOfQuestions). The score is also written to the file using fprintf. Finally, the file is closed using fclose.Return from the main function:
```
return 0;
```
The program ends by returning 0 from the `main` function.Overall, this code implements a simple quiz game using C language, where the user answers multiple-choice questions, and their score is calculated and displayed at the end. Additionally, the program stores the user's answers and final score in a file named "results.txt".