Quiz Game in C

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<conio.h>  
  4.   
  5. struct play {  
  6.             char name[20];  
  7.             int score;  
  8.             int rat;       //star ratings  
  9. }p;  
  10.   
  11. void high_score();  
  12. void start();  
  13. void help();  
  14.   
  15. void main()  
  16. {  
  17.     clrscr();  
  18.        int chc=0;  
  19.   
  20.     printf("Press 1 to start the game\n2 to see high scores \n3 to see game help\n0 to exit :   ");  
  21.     scanf("%d",&chc);  
  22.   
  23.     switch(chc)  
  24.     {  
  25.         case 1:  
  26.         start();  
  27.         break;  
  28.   
  29.         case 2:  
  30.         high_score();  
  31.         break;  
  32.   
  33.         case 3:  
  34.         help();  
  35.         break;  
  36.   
  37.           default:  
  38.           exit(1);  
  39.     }  
  40.   
  41.   getch();  
  42. }  
  43.   
  44. void help()  
  45. {  
  46.      int ch;  
  47.   
  48.      printf("\n\n ************************* GAME HELP *************************");  
  49.      printf("\n -------------------------------------------------------------------------");  
  50.      printf("\n .................... C program Quiz Game...........\n");  
  51.      printf("\n >> There will be a total of 20 questions");  
  52.      printf("\n >> You will be given 4 options and you have to press 1, 2 ,3 or 4 for the");  
  53.      printf("\n    right option");  
  54.      printf("\n >> Each question will carry 5 points");  
  55.      printf("\n >> You will be asked questions continuously.");  
  56.      printf("\n >> There is no negative marking for wrong answer");  
  57.   
  58.     printf("\n\n ************************* BEST OF LUCK *************************\n\n");  
  59.   
  60.   
  61.     printf("\nContinue playing ? (1/0) :  ");  
  62.     scanf("%d",&ch);  
  63.   
  64.     if(ch==1)  
  65.     {  
  66.         start();  
  67.     }  
  68.     else  
  69.     {  
  70.         exit(1);  
  71.     }  
  72.   
  73. }  
  74.   
  75. void start()  
  76. {  
  77.           int ans;  
  78.           int count=0;  
  79.           char rating[20];  
  80.   
  81.           FILE *fp;  
  82.   
  83.           if((fp=fopen("\\tmp\play2.txt","a"))==NULL)  
  84.              {  
  85.                  printf("error opening file\n");  
  86.              }  
  87.   
  88.   
  89.         printf("\nPlease enter your name: ");  
  90.         scanf("%s",p.name);  
  91.   
  92.   
  93.   
  94.   
  95.         printf("******************* Welcome \" %s \"  to C Quiz Game *************************** \n\n", p.name);  
  96.   
  97.   
  98.         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");  
  99.   
  100.         printf("Your answer: ");  
  101.         scanf("%d",&ans);  
  102.   
  103.         if(ans==1)  
  104.         {  
  105.             printf("Correct! +5 points\n\n");  
  106.             ++count;  
  107.   
  108.         }  
  109.         else  
  110.         {  
  111.             printf("Wrong answer! Correct answer is 1.largest railway station\n\n");  
  112.         }  
  113.   
  114.         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");  
  115.         printf("Your answer: ");  
  116.         scanf("%d",&ans);  
  117.   
  118.         if(ans==2)  
  119.         {  
  120.             printf("Correct! +5 points\n\n");  
  121.   
  122.                 ++count;  
  123.   
  124.         }  
  125.         else  
  126.         {  
  127.             printf("Wrong answer! Correct answer is 2.Insects\n\n");  
  128.         }  
  129.   
  130.         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");  
  131.   
  132.         printf("Your answer: ");  
  133.         scanf("%d",&ans);  
  134.   
  135.         if(ans==2)  
  136.         {  
  137.             printf("Correct! +5 points\n\n");  
  138.   
  139.                 ++count;  
  140.         }  
  141.         else  
  142.         {  
  143.             printf("Wrong answer! Correct answer is 2.Africa\n\n");  
  144.         }  
  145.   
  146.   
  147.         printf("Q(4).Garampani sanctuary is located at \n1.Junagarh, Gujarat\t2.Diphu, Assam\n3.Kohima, Nagaland\t4.Gangtok, Sikkim\n\n");  
  148.         printf("Your answer: ");  
  149.         scanf("%d",&ans);  
  150.         if(ans==2)  
  151.         {  
  152.             printf("Correct! +5 points\n\n");  
  153.   
  154.                 ++count;  
  155.         }  
  156.         else  
  157.         {  
  158.             printf("Wrong answer! Correct answer is 2.Diphu, Assam\n\n");  
  159.         }  
  160.   
  161.         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");  
  162.         printf("Your answer: ");  
  163.         scanf("%d",&ans);  
  164.         if(ans==4)  
  165.         {  
  166.             printf("Correct! +5 points\n\n");  
  167.   
  168.                 ++count;  
  169.         }  
  170.         else  
  171.         {  
  172.             printf("Wrong answer! Correct answer is 4.All of the above\n\n");  
  173.         }  
  174.   
  175.         printf("Q(6).Hitler party which came into power in 1933 is known as \n1.Labour Party\t2.Nazi Party\n3.Ku-Klux-Klan\t4.Democratic Party\n\n");  
  176.         printf("Your answer: ");  
  177.         scanf("%d",&ans);  
  178.         if(ans==2)  
  179.         {  
  180.             printf("Correct! +5 points\n\n");  
  181.   
  182.                 ++count;  
  183.         }  
  184.         else  
  185.         {  
  186.             printf("Wrong answer! Correct answer is 2.Nazi Party\n\n");  
  187.         }  
  188.   
  189.         printf("Q(7). FFC stands for \n1.Foreign Finance Corporation\t2.Film Finance Corporation\n3.Federation of Football Council\t4.None of the above\n\n");  
  190.         printf("Your answer: ");  
  191.         scanf("%d",&ans);  
  192.         if(ans==2)  
  193.         {  
  194.             printf("Correct! +5 points\n\n");  
  195.   
  196.                 ++count;  
  197.         }  
  198.         else  
  199.         {  
  200.             printf("Wrong answer! Correct answer is 2.Film Finance Corporation\n\n");  
  201.         }  
  202.   
  203.         printf("Q(8). Fastest shorthand writer was \n1.Dr. G. D. Bist\t2. J.R.D. Tata\n3. J.M. Tagore\t4. Khudada Khan\n\n");  
  204.         printf("Your answer: ");  
  205.         scanf("%d",&ans);  
  206.         if(ans==1)  
  207.         {  
  208.             printf("Correct! +5 points\n\n");  
  209.   
  210.                 ++count;  
  211.         }  
  212.         else  
  213.         {  
  214.             printf("Wrong answer! Correct answer is 1.Dr. G. D. Bist\n\n");  
  215.         }  
  216.   
  217.         printf("Q(9). Epsom (England) is the place associated with \n1.Horse racing\t2.Polo\n3.Shooting\t4.Snooker\n\n");  
  218.         printf("Your answer: ");  
  219.         scanf("%d",&ans);  
  220.         if(ans==1)  
  221.         {  
  222.             printf("Correct! +5 points\n\n");  
  223.   
  224.                 ++count;  
  225.         }  
  226.         else  
  227.         {  
  228.             printf("Wrong answer! Correct answer is 1.Horse racing\n\n");  
  229.         }  
  230.   
  231.         printf("Q(10). First human heart transplant operation conducted by Dr. Christian Bernard on Louis Washkansky, was conducted in \n1.1967\t2.1968\n3.1958\t4.1922\n\n");  
  232.         printf("Your answer: ");  
  233.         scanf("%d",&ans);  
  234.         if(ans==1)  
  235.         {  
  236.             printf("Correct! +5 points\n\n");  
  237.   
  238.                 ++count;  
  239.         }  
  240.         else  
  241.         {  
  242.             printf("Wrong answer! Correct answer is 1. 1967\n\n");  
  243.         }  
  244.   
  245.         printf("Q(11). Frames from one LAN can be transmitted to another LAN via the device \n1. Router\t2. Bridge\n3. Repeater\t4. Modem\n\n");  
  246.         printf("Your answer: ");  
  247.         scanf("%d",&ans);  
  248.         if(ans==2)  
  249.         {  
  250.             printf("Correct! +5 points\n\n");  
  251.   
  252.                 ++count;  
  253.         }  
  254.         else  
  255.         {  
  256.             printf("Wrong answer! Correct answer is 2. Bridge\n\n");  
  257.         }  
  258.   
  259.         printf("Q(12). Which of the following condition is used to transmit two packets over a medium at the same time?\n1. Contention\t2.  Collision\n3.   Synchronous\tD. Asynchronous\n\n");  
  260.         printf("Your answer: ");  
  261.         scanf("%d",&ans);  
  262.         if(ans==2)  
  263.         {  
  264.             printf("Correct! +5 points\n\n");  
  265.   
  266.                 ++count;  
  267.         }  
  268.         else  
  269.         {  
  270.             printf("Wrong answer! Correct answer is 2. Collision\n\n");  
  271.         }  
  272.   
  273.         printf("Q(13). What are the most commonly used transmission speeds in BPS used in data communication?\n1.   300\t2. 1200\n3.   2400\t4. 9600\n");  
  274.         printf("Your answer: ");  
  275.         scanf("%d",&ans);  
  276.         if(ans==4)  
  277.         {  
  278.             printf("Correct! +5 points\n\n");  
  279.   
  280.                 ++count;  
  281.         }  
  282.         else  
  283.         {  
  284.             printf("Wrong answer! Correct answer is 4. 9600\n\n");  
  285.         }  
  286.   
  287.         printf("Q(14).  Which of the following is used for modulation and demodulation?\n1. modem\t2. protocols\n3. gateway\t4. multiplexer\n");  
  288.         printf("Your answer: ");  
  289.         scanf("%d",&ans);  
  290.         if(ans==1)  
  291.         {  
  292.             printf("Correct! +5 points\n\n");  
  293.   
  294.                 ++count;  
  295.         }  
  296.         else  
  297.         {  
  298.             printf("Wrong answer! Correct answer is 1.modem\n\n");  
  299.         }  
  300.   
  301.         printf("Q(15). Contention is \n1. One or more conductors that serve as a common connection for a related group of devices\n2.   a continuous frequency capable of being modulated or impressed with a second signal\n3.  the condition when two or more stations attempt to use the same channel at the same time\n4.   a collection of interconnected functional units that provides a data communications service among stations attached to the network\n");  
  302.         printf("Your answer: ");  
  303.         scanf("%d",&ans);  
  304.         if(ans==3)  
  305.         {  
  306.             printf("Correct! +5 points\n\n");  
  307.   
  308.                 ++count;  
  309.         }  
  310.         else  
  311.         {  
  312.             printf("Wrong answer! Correct answer is  3. the condition when two or more stations attempt to use the same channel at the same time\n\n");  
  313.         }  
  314.   
  315.         printf("Q(16). When you ping the loopback address, a packet is sent where?\n1. On the network\n2. Down through the layers of the IP architecture and then up the layers again\n3. Across the wire\n4. through the loopback dongle\n");  
  316.         printf("Your answer: ");  
  317.         scanf("%d",&ans);  
  318.         if(ans==2)  
  319.         {  
  320.             printf("Correct! +5 points\n\n");  
  321.   
  322.                 ++count;  
  323.         }  
  324.         else  
  325.         {  
  326.             printf("Wrong answer! Correct answer is  2. Down through the layers of the IP architecture and then up the layers again\n\n");  
  327.         }  
  328.   
  329.         printf("Q(17). Which of the following TCP/IP protocol is used for transferring electronic mail messages from one machine to another?\n1. FTP\t2.    SNMP\n3. SMTP\t4. RPC\n");  
  330.         printf("Your answer: ");  
  331.         scanf("%d",&ans);  
  332.         if(ans==3)  
  333.         {  
  334.             printf("Correct! +5 points\n\n");  
  335.   
  336.                 ++count;  
  337.         }  
  338.         else  
  339.         {  
  340.             printf("Wrong answer! Correct answer is  3. SMTP\n\n");  
  341.         }  
  342.   
  343.         printf("Q(18). Which of the following device is used to connect two systems, especially if the systems use different protocols?\n1. hub\t2. bridge\n3. gateway\t4. repeater\n");  
  344.         printf("Your answer: ");  
  345.         scanf("%d",&ans);  
  346.         if(ans==3)  
  347.         {  
  348.             printf("Correct! +5 points\n\n");  
  349.   
  350.                 ++count;  
  351.         }  
  352.         else  
  353.         {  
  354.             printf("Wrong answer! Correct answer is  3. gateway\n\n");  
  355.         }  
  356.   
  357.         printf("Q(19). The slowest transmission speeds are those of\n1. twisted-pair wire\t2. coaxial cable\n3. fiber-optic cable\t4.   microwaves\n");  
  358.         printf("Your answer: ");  
  359.         scanf("%d",&ans);  
  360.         if(ans==1)  
  361.         {  
  362.             printf("Correct! +5 points\n\n");  
  363.   
  364.                 ++count;  
  365.         }  
  366.         else  
  367.         {  
  368.             printf("Wrong answer! Correct answer is  1. twisted-pair wire\n\n");  
  369.         }  
  370.   
  371.         printf("Q(20). What is the port number for NNTP?\n1. 119\t2. 80\n3. 79\t4. 70\n");  
  372.         printf("Your answer: ");  
  373.         scanf("%d",&ans);  
  374.         if(ans==1)  
  375.         {  
  376.             printf("Correct! +5 points\n\n");  
  377.   
  378.                 ++count;  
  379.         }  
  380.         else  
  381.         {  
  382.             printf("Wrong answer! Correct answer is  1. 119 \n\n");  
  383.         }  
  384.   
  385.   
  386.         //counting number of correct answers and, ratings  
  387.         if(count > 0)  
  388.         {  
  389.                 printf("Thanks for playing, Your scored: %d points \t", count*5);  
  390.   
  391.                 p.score=count*5;  
  392.   
  393.                 if(p.score >= 80)  
  394.                 {  
  395.                     printf("Rating: * * * * *");  
  396.                     p.rat=5;  
  397.                 }  
  398.                 else if(p.score >= 60 && p.score < 80)  
  399.                 {  
  400.                     printf("Rating: * * * *");  
  401.                     p.rat=4;  
  402.                 }  
  403.                 else if(p.score >= 40 && p.score < 60)  
  404.                 {  
  405.                     printf("Rating: * * *");  
  406.                   p.rat=3;  
  407.                 }  
  408.                 else if(p.score >= 20 && p.score < 40)  
  409.                 {  
  410.                     printf("Rating: * *");  
  411.                     p.rat=2;  
  412.                 }  
  413.                 else if(p.score < 20)  
  414.                 {  
  415.                     printf("Rating: *");  
  416.                      p.rat=1;  
  417.                 }  
  418.   
  419.             //writing to file  
  420.   
  421.                     fprintf(fp,"%s %d %d", p.name,p.score,p.rat);  
  422.                     fclose(fp);  
  423.         }  
  424.         else  
  425.         {  
  426.             printf("Try again!");  
  427.         }  
  428. }  
  429.   
  430. void high_score()  
  431. {  
  432.   
  433.     int ch;  
  434.   
  435.     FILE *fp;  
  436.     if((fp=fopen("\\tmp\play2.txt""r"))==NULL)  
  437.           {  
  438.                 //printf("error opening file\n");  
  439.                 printf("\nNo games played yet!\n");  
  440.             }  
  441.             else  
  442.             {  
  443.   
  444.     printf("\n******************************* HIGH SCORES *******************************\n\n");  
  445.     printf("NAME     POINTS     RATING\n");  
  446.     while(fscanf(fp,"%s %d %d",p.name,&p.score, &p.rat) !=EOF)  
  447.     {  
  448.         printf("____________________________\n");  
  449.         printf("%s     %d     %d star(s)\n\n",p.name,p.score,p.rat);  
  450.     }  
  451.   
  452.     fclose(fp);  
  453.   
  454.     }  
  455.     printf("\nContinue playing ? (1 - Yes and 0 - No) :  ");  
  456.     scanf("%d",&ch);  
  457.   
  458.     if(ch==1)  
  459.     {  
  460.         start();  
  461.     }  
  462.     else  
  463.     {  
  464.         exit(1);  
  465.     }  
  466.   
  467. }