I created a very simple multiple choice math practice game in C for a fun project. Essentially the user first enters how many questions they want, then they enter what operation (+, -, *, /) they want to practice. The game then presents a set of 4 multiple choice answers. The user enters which choice thy think is correct and the program tells them if they were right or wrong. This process continues until the last question, after that the program tells the user the time they took and the percent they got correct.
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <Windows.h> int TwoDigNum = 0; int ThreeDigNum = 0; int Answer = 0; int NumCorrect = 0; int TotalProbs = 0; char StudentAns; char AnswerLoc; time_t Start,End; void AddProbScript(int TwoDigNum, int ThreeDigNum) { printf("Solve the following: %d + %d\n", TwoDigNum, ThreeDigNum); printf("Here are your choices:\n"); } void SubProbScript(int TwoDigNum, int ThreeDigNum) { printf("Solve the following: %d - %d\n", ThreeDigNum, TwoDigNum); printf("Here are your choices:\n"); } void MultProbScript(int TwoDigNum, int ThreeDigNum) { printf("Solve the following: %d x %d\n", TwoDigNum, ThreeDigNum); printf("Here are your choices:\n"); } void DivProbScript(int TwoDigNum, int ThreeDigNum) { printf("Solve the following: %d / %d\n", ThreeDigNum, TwoDigNum); printf("Here are your choices:\n"); } int FindAns(char C, int TwoDigNum, int ThreeDigNum) { int Ans = 0; switch (C) { case '+': Ans = TwoDigNum + ThreeDigNum; break; case '-': Ans = ThreeDigNum - TwoDigNum; break; case 'x': Ans = TwoDigNum * ThreeDigNum; break; case '*': Ans = TwoDigNum * ThreeDigNum; break; default: Ans = ThreeDigNum/TwoDigNum; } return Ans; } void MakeAnswers(int Ans) { srand(time(NULL)); int WhereToPut = rand() % 999; int Rand1 = rand() % 99; int Rand2 = (rand() % 9) * 2; int Rand3 = ((rand() % 9) * (rand() % 9))+1; if (WhereToPut < 250) { printf("A.) %d\n", Ans + Rand1); printf("B.) %d\n", Ans); printf("C.) %d\n", Ans + Rand2); printf("D.) %d\n", Ans - Rand3); AnswerLoc = 'b'; } if (WhereToPut >= 250 && WhereToPut < 500) { printf("A.) %d\n", Ans - Rand1); printf("B.) %d\n", Ans + Rand2); printf("C.) %d\n", Ans + Rand3); printf("D.) %d\n", Ans); AnswerLoc = 'd'; } if (WhereToPut >= 500 && WhereToPut < 750) { printf("A.) %d\n", Ans); printf("B.) %d\n", Ans - Rand1); printf("C.) %d\n", Ans + Rand2); printf("D.) %d\n", Ans + Rand3); AnswerLoc = 'a'; } if (WhereToPut >= 750) { printf("A.) %d\n", 2*Rand1); printf("B.) %d\n", Ans + Rand2); printf("C.) %d\n", Ans); printf("D.) %d\n", Ans - Rand3); AnswerLoc = 'c'; } } void CheckAnswer(char AnswerLoc) { printf("Enter the choice you think is the answer:"); StudentAns = getch(); printf("%c", StudentAns); if (StudentAns == AnswerLoc) { printf("\nGOOD JOB! You got it right!"); NumCorrect++; } else { printf("\nThat's wrong, but keep trying!"); } } int main() { srand(time(NULL)); printf("How many problems do you want to do?"); scanf("%d", &TotalProbs); Start = clock(); for (int i = 0; i<TotalProbs ;i++) { ThreeDigNum = (rand() % 999); TwoDigNum = (rand() % 99) + 1; printf("\nEnter what operation you want to practice (+,-,x,/):"); char C = getch(); printf("%c\n", C); switch (C) { case '+': AddProbScript(TwoDigNum, ThreeDigNum); Answer = FindAns(C, TwoDigNum, ThreeDigNum); MakeAnswers(Answer); CheckAnswer(AnswerLoc); break; case '-': SubProbScript(TwoDigNum, ThreeDigNum); Answer = FindAns(C, TwoDigNum, ThreeDigNum); MakeAnswers(Answer); CheckAnswer(AnswerLoc); break; case 'x': MultProbScript(TwoDigNum, ThreeDigNum); Answer = FindAns(C, TwoDigNum, ThreeDigNum); MakeAnswers(Answer); CheckAnswer(AnswerLoc); break; default: DivProbScript(TwoDigNum, ThreeDigNum); Answer = FindAns(C, TwoDigNum, ThreeDigNum); MakeAnswers(Answer); CheckAnswer(AnswerLoc); } } End = clock(); double PercentCorrect = ((double)(NumCorrect) / TotalProbs)*100; double t = (End - Start) / CLOCKS_PER_SEC; printf("\nWow you got %d out %d correct! Thats %.2f percent!", NumCorrect, TotalProbs, PercentCorrect); printf("Time taken: %.2f seconds\n\n\n", t); if (PercentCorrect >= 90.0) { for (int x = 0; x < 100; x++) { system("color 0a"); Sleep(100); system("color 0b"); Sleep(100); system("color 0c"); Sleep(100); system("color 0d"); Sleep(100); system("color 0e"); Sleep(100); system("color 0f"); Sleep(100); } } else exit(0); }