In: Computer Science
Question 4
For a family day function at Wawasan Open University, the computer department is going to conduct quiz session to give prizes to the participants. They need the sample software to conduct this quiz session. The prototype software contains two sample questions.
First, display question 1. Give 30 seconds to answer the question, showing the countdown.
If answered correctly, move to question 2 else if time out or wrong answer, prompt other participants to try to answer with another 10 second before moving to next question. End the session.
language C
CODE FOR QUIZ QUESTION...
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
int main()
{
char ans, ans2, b=' ', e=' ';
int t=0;
for(int i=30;i>=1;i--)
{
system("cls"); //to clear the screen
cout<<"Timer: "<<i;
cout<<"\n\nQuestion Number 1? \n\ta) OPTION A \tb) OPTION
B\tc) OPTION C d) OPTION D\n\n"<<"Your Answer: ";
if(_kbhit())
{
ans=_getch();
if(int(ans)==13){
e=ans;
}
else
b=ans;
}
cout<<b;
if(int(e)==13 && b=='d')
{
t=1;
break;
}
else
if(int(e)==13 && b!='d')
break;
Sleep(1000);
}
if(t==0)
cout<<"\n\nTIME IS UP | WRONG ANSWER.";
else
cout<<"\n\nCONGRATS! YOUR ANSWER IS CORRECT.";
goto Ques2;
Ques2:
return 0;
}
YOY CAN ALSO PLAY AROUND WITH FOLLOWING CODE FOR THE COMPLETE QUIZ...
#include <iostream>
#include <conio.h>
//to include functions from Windows API
#include <Windows.h>
using namespace std;
int main()
{
//declaring variables
char answer;
int t=0, b=' ', e=' ';
//setting the timer for 30 seconds
for(int i=30; i>=0; i--)
{
system("cls"); //clears the screen
cout<<"Timer: ";
cout<<i<<" "; //prints the time remaining
cout<<"\n\nTo confirm, press ENTER.";
cout<<"\n\nQues.1: This is the
question?"<<endl<<endl;
cout<<"\ta) Option 1 \t b) Option 2 \t c) Option 3 \t d)
Option 4"<<"\n\nYour Answer: ";
if(_kbhit()) //_khbit() to determine is a key has been pressed
or not
{
answer=_getch(); //_getch() to get character from console without
echo
if(int(answer)==13){
e=answer;
}
else
b=answer;
}
//matching the correct answer
if(answer=='b' || answer=='B')
{
//answer correct message
cout<<"\n\nCONGRATS! YOUR ANSWER IS CORRECT";
}
//if answer is incorrect
else
{
//answer incorrect message
cout<<"\n\n\nYour answer is incorrect. Looser!";
//display question again for 10 seconds for other
participants
for(int i=10; i>=0; i--)
{
system("cls"); //clears the screen
cout<<"Timer: ";
cout<<i<<" "; //prints the time remaining
cout<<"\n\nOther participants can answer now.";
cout<<"\n\nQues.1: This is the
question?"<<endl<<endl;
cout<<"\ta) Option 1 \t b) Option 2 \t c) Option 3 \t d)
Option 4"<<"\n\nYour Answer: ";
if(_kbhit()) //_khbit() to determine is a key has been pressed
or not
{
answer=_getch(); //_getch() to get character from console without
echo
if(answer=='b' || answer=='B')
{
cout<<"\n\nCONGRATS! YOUR ANSWER IS CORRECT";
goto Ques2;
}
else if(t==0)
{
cout<<"\n\nYour time is up!";
goto Ques2;
}
else
{
cout<<"\n\n\nYour answer is incorrect. Looser!";
goto Ques2;
}
}
}
}
}
Ques2:
return 0;
}