Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are being asked to implement a timed quiz which stores the questions and answers in different files named quest.txt and ans.txt (Note: these are

You are being asked to implement a timed quiz which stores the questions and answers in different files named quest.txt and ans.txt (Note: these are standard text files to allow you to edit the questions and answers in a text editor; them being text files does not alter how you use the read system call to read the data.

The quiz should display a question and wait 20 seconds for the user to enter an answer; if the user enters an answer within the time limit, it should display whether the answer was correct. If the user failed to enter an answer within the time limit, the program should inform the user the question has expired; in either case, the program should reset the timer and display the next question.

This program should be implemented using interval timers, signals, and any other necessary system calls.

/* All of the includes for the project go here those includes must support signals, interval timers, read, open, and close.

You will also need support for error numbers */

#include #include

Problem 1 complete the list of include files

#define BUF_SIZE 1024

int timedOut; // this integer represents a C Boolean value that is set to true if the SIGALRM is received

/* The PrintS function takes a constant c-string and outputs it to STDOUT using write feel free to use string functions like strcmp, strcat, etc to make this one work. PrintS returns 0 on failure and 1 on success */

int PrintS(const char *str)

{

Problem 2 implement PrintS using write

}

/* the PrintInt function takes a constant int and outputs it to STDOUT using write feel free to use of a function like sprint to convert an integer to a string PrintInt returns 0 on failure and 1 on success

Note: you will need to convert the int to a c-string to make it readable */

int PrintInt(const int val)

{

Problem 3 Implement PrintInt, you can use write or a function you have written

}

/* implement the signal handler below ... use the provided prototype It must handle two signals, SIGINT and SIGALRM if the signal handler receives SIGINT prompt the user if they want to exit or not on yes, exit successfully, on no, just return from the handler on SIGALRM, set the timedOut flag to 1 */

void signalHandler(int sig)

{

/* Problem 4 implement the signalHandler */

/* when writing the prompt for SIGINT and reading the reply use system calls please also remember to read more than 1 character when reading the response since that response has at least two characters in it. Yes the responses of Y and n involve multiple characters */

}

/* This function reads a line of text from a file we are simulating another functions behavior here .. the basic idea is to read the file character by character til we find the or (newline and carriage return characters) Go ahead and tack the null character on here for simplicity Return a 0 if no characters were read, return a 1 otherwise */

int readLine(int fd, char *line)

{

/* Problem 5 implement readLine as described above */

/* Remember to use read to do this . This is probably best accomplished by reading in 1 character at a time and then adding them to the array you passed in as line*/

}

/* This function reads a question answer pairing from the provided pair of file descriptors It returns 0 if the files are empty and 1 if if successfully reads the pairing notice it calls the custom function readLine since we need to read only one line at a time*/

int readQA(int questFd, int ansFd, char *quest, char *ans)

{

if (readLine(questFd,quest) == 0) return 0; if (readLine(ansFd, ans) == 0) return 0; return 1;

}

int main(int argc, char *argv[])

{

int numRead = 0; int numWrite = 0; int question = 1; int correct = 0;

char buf[BUF_SIZE]; char quest[BUF_SIZE]; char ans[BUF_SIZE]; int questFd, ansFd;

/* declare structures for signals and timers. you will need to turn the timer on and off during this program turning the timer on sets the timers value to 20 secs turning it off sets the timers value to 0 secs you can do this with one or two struct itimervals I am using two, so the questions are designed to support two*/

/* Problem 6a declare the structures for the signals and timers */

/* initialize structures for signals and timers make certain to actually set a mask this time add both SIGINT and SIGALRM to the mask*/

/* Problem 6b initialize the structure for the signal handlers */

/* you get 20 seconds a question ... this timer has an interval of 0 and a value of 20*/

/* Problem 6c initialize the structure for the 20 sec timer */

/* you also need to disable the timer when testing this is done by setting interval and value to 0 I suggest creating another struct itimerval */

/* Problem 6d initialize the structure for the 0 sec timer,

ignore this prompt if you chose to use only one struct itimerval */

/* set up the signal handlers */

/* Problem 7 associate the SIGINT and SIGALRM with their signal handler */

/* open the question and answer files .. these are simple text files called quest.txt and ans.txt.*/

/* Problem 8 open the files quest.txt and ans.txt */

/* this loop handles the Q/A stuff I have included some of it to make your life simpler I have also left some comments to assist you as well read the first question, answer pairing prior to entering the loop */

readQA(questFd, ansFd, quest, ans); while (1)

{

/* output the current question */

PrintS("#"); PrintInt(question); PrintS(" "); PrintS(quest); PrintS("? ");

/* we will set the timedOut flag to 0, since it hasn't yet */

timedOut = 0;

/* now set the interval timer prior to reading in the user's response */

/* Problem 9 set the interval timer to go off in 20 seconds */

/* read in the user's response from STDIN, use read instead of scanf */

/* Problem 10 read a buffer from STDIN */

/* make certain to store the return value of your read in the correct location, it is used in the provided code */

if (numRead == 0) break;

if (numRead == -1)

{

if (errno == EINTR)

{

if (timedOut)

{

PrintS(" Time's up, next question ");

if (readQA(questFd, ansFd, quest, ans) == 0) break; question++;

}

continue;

}

perror("read"); exit(EXIT_FAILURE);

}

/* disable the timer here */

/* Problem 11 disable the time by setting the timers values to 0 seconds */

/* check the answer */

buf[numRead-1] = 0;

if (strncmp(buf,ans) == 0)

{

correct++; PrintS(" correct ");

} else

{

PrintS(ans); PrintS(" wrong ");

}

/* read the next question .. break if there are no more questions */

if (readQA(questFd, ansFd, quest, ans) == 0) break;

question++;

}

PrintS("final score is ");

PrintInt(correct); PrintS(" out of "); PrintInt(question);

/* Problem 12 close both files */

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 15th International Conference Dasfaa 2010 Tsukuba Japan April 2010 Proceedings Part 1 Lncs 5981

Authors: Hiroyuki Kitagawa ,Yoshiharu Ishikawa ,Wenjie Li ,Chiemi Watanabe

2010th Edition

3642120253, 978-3642120251

More Books

Students also viewed these Databases questions