0% found this document useful (0 votes)
2 views

code

The document is a C program for a quiz game inspired by 'Who Wants to Be a Millionaire'. It includes structures for questions and contestants, functions to load data from files, play the game, and display a menu. The game allows contestants to answer questions, use lifelines, and tracks their performance.

Uploaded by

kty.ylmz.1423
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code

The document is a C program for a quiz game inspired by 'Who Wants to Be a Millionaire'. It includes structures for questions and contestants, functions to load data from files, play the game, and display a menu. The game allows contestants to answer questions, use lifelines, and tracks their performance.

Uploaded by

kty.ylmz.1423
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_QUESTIONS 100


#define MAX_CONTESTANTS 50
#define MAX_CATEGORIES 10

// Structure to store questions


typedef struct {
char category[50];
char text[256];
char choices[4][100];
int correctAnswer;
int difficulty;
} Question;

// Structure to store contestant information


typedef struct {
char name[50];
int birthYear;
char phone[15];
char city[50];
int correctAnswers;
} Contestant;

Question questions[MAX_QUESTIONS];
Contestant contestants[MAX_CONTESTANTS];
int questionCount = 0, contestantCount = 0;

void loadQuestions(const char *filename);


void loadContestants(const char *filename);
void playGame();
void showMenu();
void showStatistics();
void useLifeline(int questionIndex);

int main() {
int choice;
do {
showMenu();
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
playGame();
break;
case 2:
loadQuestions("questions.txt");
break;
case 3:
loadContestants("contestants.txt");
break;
case 4:
showStatistics();
break;
case 5:
printf("Exiting the game.\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 5);

return 0;
}

void loadQuestions(const char *filename) {


FILE *file = fopen(filename, "r");
if (!file) {
printf("Error opening questions file!\n");
return;
}
questionCount = 0;
while (fscanf(file, "%49[^#]#%255[^#]#%99[^#]#%99[^#]#%99[^#]#%99[^#]#%d#%d\n",
questions[questionCount].category, questions[questionCount].text,
questions[questionCount].choices[0],
questions[questionCount].choices[1],
questions[questionCount].choices[2],
questions[questionCount].choices[3],
&questions[questionCount].correctAnswer,
&questions[questionCount].difficulty) == 8) {
questionCount++;
}
fclose(file);
printf("Questions loaded successfully!\n");
}

void loadContestants(const char *filename) {


FILE *file = fopen(filename, "r");
if (!file) {
printf("Error opening contestants file!\n");
return;
}
contestantCount = 0;
while (fscanf(file, "%49[^#]#%d#%14[^#]#%49[^#]\n",
contestants[contestantCount].name,
&contestants[contestantCount].birthYear,
contestants[contestantCount].phone,
contestants[contestantCount].city) == 4) {
contestantCount++;
}
fclose(file);
printf("Contestants loaded successfully!\n");
}

void playGame() {
srand(time(NULL));
int contestantIndex = rand() % contestantCount;
Contestant *currentContestant = &contestants[contestantIndex];
currentContestant->correctAnswers = 0;

printf("\nContestant: %s from %s\n", currentContestant->name,


currentContestant->city);
int lifelinesUsed = 0;
for (int i = 0; i < 5; i++) {
int questionIndex = rand() % questionCount;
printf("\nQuestion %d: %s\n", i + 1, questions[questionIndex].text);
for (int j = 0; j < 4; j++) {
printf("%d) %s\n", j + 1, questions[questionIndex].choices[j]);
}

if (lifelinesUsed < 2) {
printf("Use lifeline? (1 for 50/50, 2 for Double Dip, 0 to skip): ");
int lifeline;
scanf("%d", &lifeline);
if (lifeline == 1 || lifeline == 2) {
useLifeline(questionIndex);
lifelinesUsed++;
}
}

int answer;
printf("Your answer: ");
scanf("%d", &answer);
if (answer == questions[questionIndex].correctAnswer) {
printf("Correct!\n");
currentContestant->correctAnswers++;
} else {
printf("Wrong! The correct answer was %d\n",
questions[questionIndex].correctAnswer);
break;
}
}
printf("Game Over. %s answered %d questions correctly.\n", currentContestant-
>name, currentContestant->correctAnswers);
}

void showStatistics() {
printf("\nStatistics Function Placeholder\n");
}

void useLifeline(int questionIndex) {


printf("Lifeline used! Eliminating two wrong answers.\n");
// Placeholder for lifeline logic
}

void showMenu() {
printf("\nWho Wants to Be a Millionaire\n");
printf("1. Play Game\n");
printf("2. Load Questions\n");
printf("3. Load Contestants\n");
printf("4. Show Statistics\n");
printf("5. Exit\n");
}

You might also like