Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- quiz_v1.c
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #define NUM_OF_QUESTIONS 5 // for less or more questions change 5
- #define NUM_OF_OPTIONS 4 // for less or more options for answers change 4
- #define MAX_STRING_LENGTH 250 // maximum length of strings for questions and their options
- #define ESC 27 // key Escape
- void quiz(void)
- {
- int i,j;
- char question[NUM_OF_QUESTIONS][MAX_STRING_LENGTH]; // questions
- char option[NUM_OF_QUESTIONS][NUM_OF_OPTIONS][MAX_STRING_LENGTH]; // options for answers
- char answer[NUM_OF_QUESTIONS]={'a','b','c','d','a'}; // here write correct answers
- char ans;
- int score=0;
- system("cls");
- // Below write your questions and options for answers.
- // For less or more questions change 5 in #define NUM_OF_QUESTIONS 5
- // For less or more options for answers change 4 in #define NUM_OF_OPTIONS 4
- strcpy(question[0], "This is question no 1");
- strcpy(option[0][0], "option11");
- strcpy(option[0][1], "option12");
- strcpy(option[0][2], "option13");
- strcpy(option[0][3], "option14");
- strcpy(question[1], "This is question no 2");
- strcpy(option[1][0], "option21");
- strcpy(option[1][1], "option22");
- strcpy(option[1][2], "option23");
- strcpy(option[1][3], "option24");
- strcpy(question[2], "This is question no 3");
- strcpy(option[2][0], "option31");
- strcpy(option[2][1], "option32");
- strcpy(option[2][2], "option33");
- strcpy(option[2][3], "option34");
- strcpy(question[3], "This is question no 4");
- strcpy(option[3][0], "option41");
- strcpy(option[3][1], "option42");
- strcpy(option[3][2], "option43");
- strcpy(option[3][3], "option44");
- strcpy(question[4], "This is question no 5");
- strcpy(option[4][0], "option51");
- strcpy(option[4][1], "option52");
- strcpy(option[4][2], "option53");
- strcpy(option[4][3], "option54");
- for(i=0;i<NUM_OF_QUESTIONS;i++)
- {
- system("cls");
- printf("\n %s \n",question[i]);
- for(j=0;j<NUM_OF_OPTIONS;j++)
- printf("\n %c) %s \n",97+j,option[i][j]);
- do{
- printf("\n\n Please choose answer using key a, b, c, or d ");
- ans=tolower(getch());
- if(ans=='a'||ans=='b'||ans=='c'||ans=='d')
- break;
- } while(1);
- if(ans==answer[i]){
- printf("\n\n Correct Answer ! \n\n");
- score++;
- }else{
- printf("\n\n Wrong Answer ! \n");
- printf("\n Correct answer is %c) \n\n",answer[i]);
- }
- printf("\n Press any key to continue ... \n\n");
- getch();
- } // for(i=0;i<NUM_OF_QUESTIONS;i++)
- system("cls");
- printf("\n You have %d correct answers out of %d questions. \n",score,NUM_OF_QUESTIONS);
- } // quiz()
- int main(void)
- {
- quiz();
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement