Code:
//Author: Emmanuel Adenola
//Group A, Nov 13/11/2009
/*this code is for to ask a number of questions in a quiz contest re-displaying a menu */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
int i, one=2, three=4, four= 4 ;
int answer=0, num1=0;
int correct=0, wrong=0 ;
char yesno; char option = '0' ;
do
{
printf(" \n ") ;
printf(" ((----))\n") ;
printf(" <--<\\\\_//>->\n") ;
printf(" | | \n") ;
printf(" (===)\n") ;
printf("\n") ;
printf(" QUIZZITCH CUP\n") ;//This is just some drawing of the prize cup that the winner would have
printf("\n") ;
printf("1. Enter the number of questions to be asked for this round of the quiz\n") ;
printf("2. Start quiz\n") ;
printf("3. Display the numbers of questions that were (i) correctly (ii) incorrectly\n") ;
printf("4. Exit Program\n") ;
scanf("%1s", &option) ;//waits for an input for 1, 2, 3 or 4
while(option < '1'&&option>'4') //if option is not =1 it willforce user to enter1. but if user chooses 3..it would go to the other while loop//this enables it to display the results of option3 if variable num1 isn't empty. if it is empty and user chose 3..then menu would be redisplayed
{
if(option!='1'&&num1!=0)
{
break ;
}
if(i=getchar())
{
printf("invalid\n");
break ;
}
printf("You must choose 1 before you can proceed to quiz\n") ;
scanf("%1s", &option) ;
if(option=='4'||option=='1')
{
break ;
}
i++ ;
}
switch(option) //the switch option allows me to choose number of questions i want .
{
case '1':
{
printf("1. One question\n") ;
printf("2. Two questions\n") ;
printf("3. Three questions\n") ;
printf("4. Four questions\n") ;
scanf("%d", &num1) ; //this scanf to read in the option for the number questions to be answered
while(num1 != 1 && num1 != 2&& num1 != 3 && num1 != 4)
{
if(i=getchar()) ;
{
printf("invalid character\n") ;
printf("You must choose from 1-4 before you can proceed to quiz\n") ;
scanf("%d", &num1) ;
}
printf("Invalid choice. You must choose from 1-4 before you can proceed to quiz\n") ;
scanf("%d", &num1) ;
i++ ;
}
printf("You chose %d..Enter 2 to continue quiz", num1) ;
break ;
}
case '2':
{
if(option=='2' && option!='1'&&num1==0)
{
printf("you must choose 1 before proceeding\n") ;
break ;
}
if(correct!=0||wrong!=0 &&option!='3')//user can't choose 2 until 3 has been chosen
{
printf("You must view answers, enter 4 to exit or enter1 to restart to quiz\n") ;
break ;
}
switch(num1)//start of option 2 using num1 as a variable of numbers of questions to be asked
{//the index i will always be less than num1 the number of questions..the value of one and i will keep changing or be incremented to create a semblance of randomness
case 1:
{
printf("What is 1+1: *\n") ;
scanf("%d", &answer) ;//the answer is stored in a variable one, two, three, four, and matched with answer-5. if it matches..then result is display
if(answer==1+1)
{
printf("%d is Correct!\n", answer) ;
correct++ ;
}
else
{
printf("%d is Incorrect. Answer is: %d!\n", answer, 1+1) ;
wrong++ ;
}
break;
}//end of case 1 of question 1
case 2:
{
for(i=0; i<num1; one++, i++)
{
printf("what is %d * %d\n", one, i+3) ;
scanf("%d", &answer) ;
if(answer==one*(i+3))
{
printf("%d is Correct!\n", answer) ;
correct++ ;
}
else
{
printf("%d is Incorrect. Answer is: %d!\n", answer, one*(i+3)) ;
wrong++ ;
}
}
break ;
}//end of question 2
//case 3
case 3:
{
for(i=0; i<num1; three*=2, i++)
{
printf("what is %d - %d\n",three, i+2) ;
scanf("%d", &answer) ;
if(answer==(three)-(i+2))
{
printf("%d is Correct!\n", answer) ;
correct++ ;
}
else
{
printf("%d is Incorrect. Answer is: %d\n", answer, (three)-(i+2)) ;
wrong++ ;
}
}
break ;
}
case 4:
{
for(i=0; i<num1; i++,four)
{
printf("what is %d / %d\n", four,i+1) ;
scanf("%d", &answer) ;
if(answer==(four)/(i+1))
{
printf("%d is Correct!\n", answer) ;
correct++ ;
}
else
{
printf("%d is Incorrect. Answer is: %d!\n", answer,(four)/(i+1)) ;
wrong++ ;
}
}
break ;
}
}
break ;
}//end of case 2 of switch(option)
case '3': //beginning of case 3. case 3 shows how many questions the user answer correctly
{
if(option == '3' && answer == 0)//if condition is true it is going to break..it can't show answers if question hasn't been answered
{
printf("You haven't answered question\n") ;
break ;
}
printf("You got %d Correct!\n", correct) ;
printf("You got %d Wrong!\n", wrong) ;
correct = 0;
wrong = 0;//will clear the value of correct and wrong or it will be incrementing if program hasn't been closed by user
break ;
}
case '4':
{
printf("Are you sure you want to exit quiz?\n") ;
printf("Y/N") ;
printf("\n") ;
scanf("%1s", &yesno) ;
while(yesno!='Y' && yesno!='y'&&yesno!='N' && yesno!='n')
{
printf("Invalid choice\n") ;
break ;//invalid choice will return to main menu
}
if(yesno== 'Y'|| yesno == 'y')
{
exit(0) ;//quits program
}
if(yesno=='N'|| yesno == 'n')
{
clrscr() ;//return to main meni
break ;
}
break ; /*user must input y to quit*/
}
default:
{
printf("Please input valid character\n") ;
break ;
}
}
}while(option!='1' || option!='2' ||option!='3' || option != '4') ;
getch() ;
}