Advertisement
dmilicev

quiz_v1.c

Jun 6th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. /*
  2.  
  3.     quiz_v1.c
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <string.h>
  15.  
  16. #define NUM_OF_QUESTIONS 5      // for less or more questions change 5
  17. #define NUM_OF_OPTIONS 4        // for less or more options for answers change 4
  18. #define MAX_STRING_LENGTH 250   // maximum length of strings for questions and their options
  19. #define ESC 27                  // key Escape
  20.  
  21. void quiz(void)
  22. {
  23.     int i,j;
  24.     char question[NUM_OF_QUESTIONS][MAX_STRING_LENGTH];                 // questions
  25.     char option[NUM_OF_QUESTIONS][NUM_OF_OPTIONS][MAX_STRING_LENGTH];   // options for answers
  26.     char answer[NUM_OF_QUESTIONS]={'a','b','c','d','a'};    // here write correct answers
  27.     char ans;
  28.     int score=0;
  29.  
  30.     system("cls");
  31.  
  32.     // Below write your questions and options for answers.
  33.     // For less or more questions change 5 in #define NUM_OF_QUESTIONS 5
  34.     // For less or more options for answers change 4 in #define NUM_OF_OPTIONS 4
  35.  
  36.     strcpy(question[0], "This is question no 1");
  37.     strcpy(option[0][0], "option11");
  38.     strcpy(option[0][1], "option12");
  39.     strcpy(option[0][2], "option13");
  40.     strcpy(option[0][3], "option14");
  41.  
  42.     strcpy(question[1], "This is question no 2");
  43.     strcpy(option[1][0], "option21");
  44.     strcpy(option[1][1], "option22");
  45.     strcpy(option[1][2], "option23");
  46.     strcpy(option[1][3], "option24");
  47.  
  48.     strcpy(question[2], "This is question no 3");
  49.     strcpy(option[2][0], "option31");
  50.     strcpy(option[2][1], "option32");
  51.     strcpy(option[2][2], "option33");
  52.     strcpy(option[2][3], "option34");
  53.  
  54.     strcpy(question[3], "This is question no 4");
  55.     strcpy(option[3][0], "option41");
  56.     strcpy(option[3][1], "option42");
  57.     strcpy(option[3][2], "option43");
  58.     strcpy(option[3][3], "option44");
  59.  
  60.     strcpy(question[4], "This is question no 5");
  61.     strcpy(option[4][0], "option51");
  62.     strcpy(option[4][1], "option52");
  63.     strcpy(option[4][2], "option53");
  64.     strcpy(option[4][3], "option54");
  65.  
  66.     for(i=0;i<NUM_OF_QUESTIONS;i++)
  67.     {
  68.         system("cls");
  69.  
  70.         printf("\n %s \n",question[i]);
  71.  
  72.         for(j=0;j<NUM_OF_OPTIONS;j++)
  73.             printf("\n %c) %s \n",97+j,option[i][j]);
  74.  
  75.         do{
  76.             printf("\n\n Please choose answer using key a, b, c, or d ");
  77.             ans=tolower(getch());
  78.             if(ans=='a'||ans=='b'||ans=='c'||ans=='d')
  79.                 break;
  80.         } while(1);
  81.  
  82.         if(ans==answer[i]){
  83.             printf("\n\n Correct Answer ! \n\n");
  84.             score++;
  85.         }else{
  86.             printf("\n\n Wrong Answer ! \n");
  87.             printf("\n Correct answer is %c) \n\n",answer[i]);
  88.         }
  89.  
  90.         printf("\n Press any key to continue ... \n\n");
  91.         getch();
  92.  
  93.     } // for(i=0;i<NUM_OF_QUESTIONS;i++)
  94.  
  95.     system("cls");
  96.     printf("\n You have %d correct answers out of %d questions. \n",score,NUM_OF_QUESTIONS);
  97.  
  98. } // quiz()
  99.  
  100.  
  101. int main(void)
  102. {
  103.     quiz();
  104.  
  105.     return 0;
  106.  
  107. } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement