Application Flow Chart:
Start: Quiz program
start
Main Menu: Program
display menu to the user to start the game or to quit.
User Input: The application stops to get user input.
Decision Tree: If
user input is S then Quiz game start else if user input is Q then the program will
quit.
Quiz Game: User gets
10 questions one by one and each correct score is marked as 1.
Display Score: It
display final score after ending the game and the user is directed to the main menu.
Here is the main
menu code for the Quiz game used in this article:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include "gotoxy.h"
/* Define variable*/
#define ESC 27
/* Function declaration*/
void mainMenu();
void gamePlay();
int main() {
mainMenu();
return 0;
}
/* Function definition*/
void mainMenu(){
char choice;
system("cls");
gotoxy(30,8);
printf("WELCOME TO I.Q TEST PROGRAM");
gotoxy(30,10);
printf("----------------------------");
gotoxy(30,12);
printf("Enter S to start game");
gotoxy(30,14);
printf("Enter Q or ESc to quit");
gotoxy(30,16);
printf("----------------------------");
choice=toupper(getch());
switch(choice){
case 'S':
gamePlay();
break;
case 'Q':
exit(0);
case ESC:
exit(0);
default:
gotoxy(30,18);
printf("Wrong Entry!!");
gotoxy(30,20);
printf("Press any key and re-enter correct option");
if(getch())
mainMenu();
}
}
//Function defination
void gamePlay(){
system("cls");
printf("Choice S selected");
}
Explaining the Code:
Let's start digging the code. Here you will learn to breakdown application in function and using switch statements to switch between functions based on user input.
Let's start digging the code. Here you will learn to breakdown application in function and using switch statements to switch between functions based on user input.
First, we will include the user define header "gotoxy.h"
and system library headers "stdio.h", "conio.h" and
"ctype.h". The "stdio.h" is C Standard Input Output
library, "conio.h" is not a C Standard Input Output but required for
user input function getch() and "ctype.h" is C Standard library
required for toupper() function. To learn about "gotoxy.h" library function please follow my previous article gotoxy in codeblock.
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include "gotoxy.h"
27 is the ASCII decimal
code for the "ESC" character. In a switch statement, this value is used
to check the user input "ESC" key.
/* Define variable*/
#define ESC 27
/* Function declaration*/
void mainMenu();
void gamePlay();
int main() {
mainMenu();
return 0;
}
This is another line please :DmainMenu() function displays user options and waits for the user input. Switch statement is used to switch the application state. If user input "S" gamePlay() function is called to start the game, else if user input "Q" program exits by calling exit(0) and if user input other then these two options then again mainMenu() function is called to return program in the current state. This technique will help to create an infinite loop, until and unless the user input correct option program will remain in the current sate.
You may have noticed that at top of each function system("cls") function is called which is used to clear screen.
Finally, gamePlay() function is the main part of this application which we will discuss in the next part.
why gotoxy is used over here i mean what is its purpose and kindly explain gotoxy(30,8)/(30,10)/(30,12).... etc.
ReplyDeleteIn short, gotoxy is used for text alignment.
DeleteKindly follow my previous article https://www.codeincodeblock.com/2011/03/gotoxy-in-codeblock.html. You will understand why it is used.