Here's a complete C implementation of a Who Wants to Be a Millionaire-style quiz game with lifelines, external question loading, and progressive scoring.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
typedef struct {
char question[256];
char answer[256];
char option1[256];
char option2[256];
char option3[256];
char option4[256];
} Question;
typedef struct {
int currentQuestion;
int moneyWon;
int lifelineUsed[3]; // 0=unused, 1=used
int gameOver;
} GameState;
Question* loadQuestions(const char* filename, int* numQuestions) {
FILE* fp = fopen(filename, "r");
if (!fp) {
printf("Error opening question file!\n");
return NULL;
}
fscanf(fp, "%d\n", numQuestions);
Question* questions = malloc(*numQuestions * sizeof(Question));
if (!questions) {
printf("Memory allocation failed!\n");
fclose(fp);
return NULL;
}
for (int i = 0; i < *numQuestions; i++) {
fscanf(fp, "%[^\n]\n", questions[i].question);
fscanf(fp, "%[^\n]\n", questions[i].answer);
fscanf(fp, "%[^\n]\n", questions[i].option1);
fscanf(fp, "%[^\n]\n", questions[i].option2);
fscanf(fp, "%[^\n]\n", questions[i].option3);
fscanf(fp, "%[^\n]\n", questions[i].option4);
}
fclose(fp);
return questions;
}
void displayQuestion(Question q) {
printf("\n%s\n\n", q.question);
printf("1. %s\n", q.option1);
printf("2. %s\n", q.option2);
printf("3. %s\n", q.option3);
printf("4. %s\n", q.option4);
}
int getAnswer(GameState* game) {
int answer;
printf("\nEnter your answer (1-4): ");
scanf("%d", &answer);
while (answer < 1 || answer > 4) {
printf("Invalid! Enter 1-4: ");
scanf("%d", &answer);
}
return answer;
}
int checkAnswer(Question q, int answer) {
char* opts[4] = {q.option1, q.option2, q.option3, q.option4};
return strcmp(opts[answer-1], q.answer) == 0;
}
void useLifeline(GameState* game) {
int choice;
printf("\nChoose lifeline:\n1. 50/50\n2. Ask Audience\n3. Phone Friend\nChoice: ");
scanf("%d", &choice);
while (choice < 1 || choice > 3) {
printf("Invalid! 1-3: ");
scanf("%d", &choice);
}
if (game->lifelineUsed[choice-1]) {
printf("Already used!\n");
return;
}
game->lifelineUsed[choice-1] = 1;
printf("Lifeline activated! (Implementation needed)\n");
}
void playGame(Question* questions, int numQuestions) {
GameState game = {0, 0, {0,0,0}, 0};
while (!game.gameOver) {
displayQuestion(questions[game.currentQuestion]);
printf("\nUse lifeline? (y/n): ");
char c;
scanf(" %c", &c);
if (c == 'y') useLifeline(&game);
int ans = getAnswer(&game);
if (checkAnswer(questions[game.currentQuestion], ans)) {
printf("Correct!\n");
game.moneyWon += 1000 * (game.currentQuestion + 1);
if (++game.currentQuestion == numQuestions) {
game.gameOver = 1;
printf("\nCongratulations! You won $%d!\n", game.moneyWon);
}
} else {
printf("Incorrect!\nGame over. You won $%d.\n", game.moneyWon);
game.gameOver = 1;
}
}
}
int main() {
srand(time(NULL));
int numQuestions;
Question* questions = loadQuestions("questions.txt", &numQuestions);
if (!questions) return 1;
printf("\n=== WHO WANTS TO BE A MILLIONAIRE ===\n");
playGame(questions, numQuestions);
free(questions);
return 0;
}
10
What is the capital of France?
Paris
Berlin
London
Rome
Tokyo
... (repeat for each question)
Compile with: gcc millionaire.c -o millionaire