From 2ff53d4770db0fbb0e957bc870461a6bf9604c84 Mon Sep 17 00:00:00 2001 From: ridhima6111 <82163633+ridhima6111@users.noreply.github.com> Date: Sun, 17 Oct 2021 00:30:57 +0530 Subject: [PATCH] Create quiz game This is a general knowledge quiz game, which tests the user's awareness of his surroundings. --- quiz game | 259 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 quiz game diff --git a/quiz game b/quiz game new file mode 100644 index 00000000..13cd1e42 --- /dev/null +++ b/quiz game @@ -0,0 +1,259 @@ +#include +#include +#include + +struct play { + char name[20]; + int score; + int rat; //star ratings +}p; + +void high_score(); +void start(); +void help(); + +void main() +{ + + int chc=0; + + printf("Press 1 to start the game\n2 to see high scores \n3 to see game help\n0 to exit : "); + scanf("%d",&chc); + + switch(chc) + { + case 1: + start(); + break; + + case 2: + high_score(); + break; + + case 3: + help(); + break; + + default: + exit(1); + } + + getch(); +} + +void help() +{ + int ch; + + printf("\n\n ************************* GAME HELP *************************"); + printf("\n -------------------------------------------------------------------------"); + printf("\n .................... C program Quiz Game...........\n"); + printf("\n >> There will be a total of 5 questions"); + printf("\n >> You will be given 4 options and you have to press 1, 2 ,3 or 4 for the"); + printf("\n right option"); + printf("\n >> Each question will carry 5 points"); + printf("\n >> You will be asked questions continuously."); + printf("\n >> There is no negative marking for wrong answer"); + + printf("\n\n ************************* BEST OF LUCK *************************\n\n"); + + + printf("\nContinue playing ? (1/0) : "); + scanf("%d",&ch); + + if(ch==1) + { + start(); + } + else + { + exit(1); + } + +} + +void start() +{ + int ans; + int count=0; + char rating[20]; + + FILE *fp; + + if((fp=fopen("\\tmplay2.txt","a"))==NULL) + { + printf("error opening file\n"); + } + + + printf("\nPlease enter your name: "); + scanf("%s",p.name); + + + + + printf("******************* Welcome \" %s \" to C Quiz Game *************************** \n\n", p.name); + + + printf("Q(1).Grand Central Terminal, Park Avenue, New York is the world's \n 1.largest railway station \t\t2.highest railway station\n 3.longest railway station\t\t 4.None of the above\n\n"); + + printf("Your answer: "); + scanf("%d",&ans); + + if(ans==1) + { + printf("Correct! +5 points\n\n"); + ++count; + + } + else + { + printf("Wrong answer! Correct answer is 1.largest railway station\n\n"); + } + + printf("Q(2) Entomology is the science that studies \n 1.Behavior of human beings\t\t2.Insects \n3.The origin and history of technical and scientific terms\n4.The formation of rocks\n\n"); + printf("Your answer: "); + scanf("%d",&ans); + + if(ans==2) + { + printf("Correct! +5 points\n\n"); + + ++count; + + } + else + { + printf("Wrong answer! Correct answer is 2.Insects\n\n"); + } + + printf("Q(3) Eritrea, which became the 182nd member of the UN in 1993, is in the continent of \n1.Asia\t\t2.Africa\n3.Europe\t4.Australia\n\n"); + + printf("Your answer: "); + scanf("%d",&ans); + + if(ans==2) + { + printf("Correct! +5 points\n\n"); + + ++count; + } + else + { + printf("Wrong answer! Correct answer is 2.Africa\n\n"); + } + + + printf("Q(4).Garampani sanctuary is located at \n1.Junagarh, Gujarat\t2.Diphu, Assam\n3.Kohima, Nagaland\t4.Gangtok, Sikkim\n\n"); + printf("Your answer: "); + scanf("%d",&ans); + if(ans==2) + { + printf("Correct! +5 points\n\n"); + + ++count; + } + else + { + printf("Wrong answer! Correct answer is 2.Diphu, Assam\n\n"); + } + + printf("Q(5).For which of the following disciplines is Nobel Prize awarded? \n1.Physics and Chemistry\t2.Physiology or Medicine\n3.Literature, Peace and Economics\t4.All of the above\n\n"); + printf("Your answer: "); + scanf("%d",&ans); + if(ans==4) + { + printf("Correct! +5 points\n\n"); + + ++count; + } + else + { + printf("Wrong answer! Correct answer is 4.All of the above\n\n"); + } + + + + + //counting number of correct answers and, ratings + if(count > 0) + { + printf("Thanks for playing, Your scored: %d points \t", count*5); + + p.score=count*5; + + if(p.score == 25) + { + printf("Rating: * * * * *"); + p.rat=5; + } + else if(p.score == 20) + { + printf("Rating: * * * *"); + p.rat=4; + } + else if(p.score == 15) + { + printf("Rating: * * *"); + p.rat=3; + } + else if(p.score == 10) + { + printf("Rating: * *"); + p.rat=2; + } + else if(p.score == 5) + { + printf("Rating: *"); + p.rat=1; + } + + //writing to file + + fprintf(fp,"%s %d %d", p.name,p.score,p.rat); + fclose(fp); + } + else + { + printf("Try again!"); + } +} + +void high_score() +{ + + int ch; + + FILE *fp; + if((fp=fopen("\\tmplay2.txt", "r"))==NULL) + { + //printf("error opening file\n"); + printf("\nNo games played yet!\n"); + } + else + { + + printf("\n******************************* HIGH SCORES *******************************\n\n"); + printf("NAME POINTS RATING\n"); + while(fscanf(fp,"%s %d %d",p.name,&p.score, &p.rat) !=EOF) + { + printf("____________________________\n"); + printf("%s %d %d star(s)\n\n",p.name,p.score,p.rat); + } + + fclose(fp); + + } + printf("\nContinue playing ? (1 - Yes and 0 - No) : "); + scanf("%d",&ch); + + if(ch==1) + { + start(); + } + else + { + exit(1); + } + +}