Write a C++ documented program to perform the role of a quiz maker. The program
ID: 3824452 • Letter: W
Question
Write a C++ documented program to perform the role of a quiz maker. The program should randomly display True/False questions, record student's answers, keep track of the timing, and calculate the final scores out of 10.The program should work as follows:
Prompt the student to enter their First name and Last name (both of type string), and ID (a number of 6 digits)
Validate the entered ID, if the length of ID is not 6 digits, stay at step 2 and prompt the user to re-enter the correct ID. After 3 failed attempts, exit the program. If the student entered a valid name and ID go to step 3.
Randomly pick ten questions with their answers form the Q and A arrays.
Display one question at a time. Get the answer then move to the next question.
Do not accept answers other than true or false (T or F should be fine too). The answers should not be case sensitive.
When the user is done with the quiz, print out a report (On screen) with the below information in it:
First name and Last name
Score
Elapsed time
Student’s answers and the correct answer.
Prompt the student to enter Q to exit the program, or to enter S to clear the screen and start a new quiz for another student.
Requirements
The program skeleton will include two string arrays Q for questions and A for answers, that you will use to pick the quiz questions from.
In each quiz, your program should display ten unique random questions (no repeated questions).
Users have 10 minutes to complete the quiz, after 10 minutes the program should automatically terminates the quiz and report the outputs to the screen.
All questions have the same weight. 10 questions for 10 points.
Code optimization (breaking your code to functions):
Modify your code to include the following functions:
bool Validate_ID (int ID): to validate the length of the ID, if 6 returns true otherwise returns false.
bool Unique_Question (int questionNumber): to check if the question already was used before in the quiz or not, if the question was used before returns false, otherwise returns true.
Grading Rubric:
Workable Program 35
Using Code Optimization (Functions) 15
Correct output 25
Using descriptive variables names 10
Comments/readability of program 15
Extra Credits:
(15 points) By default the quiz maker, will display 10 questions for the student, each question weight 1 point. Give an option for student to choose the number of questions in the quiz, options will be 10 questions or 20 questions before starting the exam:
If the student chose 10 questions, then each question weight 1 point
If the student chose 20 questions, then each question weight 0.5 point
(25 points) Modify your program so instead of having the questions and answers in your code, questions can be read from a text file name it “TestBank.txt” and answers from a text file “Answers.txt”, and dynamically during runtime assign the questions to Q array and answers to A array. Write a C++ documented program to perform the role of a quiz maker. The program should randomly display True/False questions, record student's answers, keep track of the timing, and calculate the final scores out of 10.
The program should work as follows:
Prompt the student to enter their First name and Last name (both of type string), and ID (a number of 6 digits)
Validate the entered ID, if the length of ID is not 6 digits, stay at step 2 and prompt the user to re-enter the correct ID. After 3 failed attempts, exit the program. If the student entered a valid name and ID go to step 3.
Randomly pick ten questions with their answers form the Q and A arrays.
Display one question at a time. Get the answer then move to the next question.
Do not accept answers other than true or false (T or F should be fine too). The answers should not be case sensitive.
When the user is done with the quiz, print out a report (On screen) with the below information in it:
First name and Last name
Score
Elapsed time
Student’s answers and the correct answer.
Prompt the student to enter Q to exit the program, or to enter S to clear the screen and start a new quiz for another student.
Requirements
The program skeleton will include two string arrays Q for questions and A for answers, that you will use to pick the quiz questions from.
In each quiz, your program should display ten unique random questions (no repeated questions).
Users have 10 minutes to complete the quiz, after 10 minutes the program should automatically terminates the quiz and report the outputs to the screen.
All questions have the same weight. 10 questions for 10 points.
Code optimization (breaking your code to functions):
Modify your code to include the following functions:
bool Validate_ID (int ID): to validate the length of the ID, if 6 returns true otherwise returns false.
bool Unique_Question (int questionNumber): to check if the question already was used before in the quiz or not, if the question was used before returns false, otherwise returns true.
Grading Rubric:
Workable Program 35
Using Code Optimization (Functions) 15
Correct output 25
Using descriptive variables names 10
Comments/readability of program 15
Extra Credits:
(15 points) By default the quiz maker, will display 10 questions for the student, each question weight 1 point. Give an option for student to choose the number of questions in the quiz, options will be 10 questions or 20 questions before starting the exam:
If the student chose 10 questions, then each question weight 1 point
If the student chose 20 questions, then each question weight 0.5 point
(25 points) Modify your program so instead of having the questions and answers in your code, questions can be read from a text file name it “TestBank.txt” and answers from a text file “Answers.txt”, and dynamically during runtime assign the questions to Q array and answers to A array. Write a C++ documented program to perform the role of a quiz maker. The program should randomly display True/False questions, record student's answers, keep track of the timing, and calculate the final scores out of 10.
The program should work as follows:
Prompt the student to enter their First name and Last name (both of type string), and ID (a number of 6 digits)
Validate the entered ID, if the length of ID is not 6 digits, stay at step 2 and prompt the user to re-enter the correct ID. After 3 failed attempts, exit the program. If the student entered a valid name and ID go to step 3.
Randomly pick ten questions with their answers form the Q and A arrays.
Display one question at a time. Get the answer then move to the next question.
Do not accept answers other than true or false (T or F should be fine too). The answers should not be case sensitive.
When the user is done with the quiz, print out a report (On screen) with the below information in it:
First name and Last name
Score
Elapsed time
Student’s answers and the correct answer.
Prompt the student to enter Q to exit the program, or to enter S to clear the screen and start a new quiz for another student.
Requirements
The program skeleton will include two string arrays Q for questions and A for answers, that you will use to pick the quiz questions from.
In each quiz, your program should display ten unique random questions (no repeated questions).
Users have 10 minutes to complete the quiz, after 10 minutes the program should automatically terminates the quiz and report the outputs to the screen.
All questions have the same weight. 10 questions for 10 points.
Code optimization (breaking your code to functions):
Modify your code to include the following functions:
bool Validate_ID (int ID): to validate the length of the ID, if 6 returns true otherwise returns false.
bool Unique_Question (int questionNumber): to check if the question already was used before in the quiz or not, if the question was used before returns false, otherwise returns true.
Grading Rubric:
Workable Program 35
Using Code Optimization (Functions) 15
Correct output 25
Using descriptive variables names 10
Comments/readability of program 15
Extra Credits:
(15 points) By default the quiz maker, will display 10 questions for the student, each question weight 1 point. Give an option for student to choose the number of questions in the quiz, options will be 10 questions or 20 questions before starting the exam:
If the student chose 10 questions, then each question weight 1 point
If the student chose 20 questions, then each question weight 0.5 point
(25 points) Modify your program so instead of having the questions and answers in your code, questions can be read from a text file name it “TestBank.txt” and answers from a text file “Answers.txt”, and dynamically during runtime assign the questions to Q array and answers to A array.
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
/*
This program qives a student a true/false quiz. Student enters T for true answer and F for the
false answer. Program stores all three: questions, answers, and student responses in three different arrays.
The questions are stored in a string array and read from a file. [One question per line].
The answers are stored in an int array and read from a file.
In answers file 1 represent true and 0 false.
Both array have same length.
Student responses are recorded in another (third) int array.
The answer and student response arrays are compared. Student score is computed.
If student scores 60% or more the message is given that student passed. Otherwise
message is given that student failed.
*/
const int MAX = 20;
void openFile(ifstream & input, const string & message);
int fillQuestionsArray(string questions[], ifstream & inq);
int fillAnswersArray(char answers[], ifstream & inq);
void fillResponseArray(const string questions[], int response[], int lenQ);
int GradeResponses(const char answers[], const int response[], int len);
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len);
int main(){
cout << "Welcome to the quiz." << endl;
ifstream in;
openFile(in, "TestBank.txt");
string questions[MAX] = {};
int lenQ = fillQuestionsArray(questions, in);
{
for (int i = 0; i < MAX; i++);
}
in.close();
openFile(in, "Answers.txt");
char answers[MAX];
int lenA = fillAnswersArray(answers, in);
if (lenQ != lenA){
cout << "Number of questions and answers are different. Exiting the program." << endl;
exit(0);
}
do{
cout<<"Enter the number of questions 10 = 1 / 20 = 2";cin>>lenQ;
}while(!(lenQ>=1 && lenQ<=2));
lenQ*=10;
int response[MAX] = {};
fillResponseArray(questions, response, lenQ);
double score = 0;
score = GradeResponses(answers, response, lenQ);
printAnswersAndResponses(questions, answers, response, lenQ);
cout << "You scored " << score << " points, out of maximum " << lenQ << " points." << endl;
double percent = (double(score) / lenQ) * 100;
if (percent >= 60.0){
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "You passed the quiz." << endl;
}
else{
cout << "You scored " << percent << " % in the quiz." << endl;
cout << "Sorry you failed the quiz." << endl;
}
in.close();
cout << "--------------------------------------------------------------------------------------------------------" << endl;
return 0;
}
void printAnswersAndResponses(const string questions[], const char answers[], const int response[], int len){
cout << "*****************************************************" << endl;
cout << "Here are correct answers and your responses." << endl;
cout << "*****************************************************" << endl;
cout << setw(55) << left << "Question" << setfill(' ') << setw(18) << left << "Correct Answer" << setfill(' ') << left << setw(15) << "Your Response" << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
for (int i = 0; i<len; i++){
cout << setw(55) << left << questions[i] << setfill(' ') << setw(18) << left << answers[i] << setfill(' ') << setw(15) << left << response[i] << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
cout << "--------------------------------------------------------------------------------------------------------" << endl;
}
double GradeResponses(const char answers[], const int response[], int len){
double score = 0;
for (int i = 0; i<len; i++){
if (answers[i] == response[i]){
if (len>10)
score+=0.5;
else
score++;
}
}
return score;
}
void fillResponseArray(const string questions[], int response[], int lenQ){
cout << "Please answer the questions as asked. Questions are true or false. "
<< "Enter T for a true answer and F for a false answer." << endl;
int ques[lenQ];
for (int i = 0; i<lenQ; ++i){
do{
int k=0;bool oldQues;
srand();
do{ //get non-repeatable random number
oldQues=false;
k=rand()%lenQ;
for (int t=0;t<i;t++){
oldQues=(ques[t]==k);
break;
}
ques[i]=k;
} while (oldQues);
char option;
cout << questions[k] << " : ";
cin >> option;
switch(option){
case 'T':
case 't': response[k]=1;break;
case 'F':
case 'f': response[k]=0;break;
default : cout<<"Invalid input";option='X';
}
} while (option=='X');
}
}
int fillAnswersArray(char answers[], ifstream & inA){
char ch = ' ';
int i = 0;
while ((ch = inA.peek()) != EOF && i<MAX){
inA >> answers[i];
i++;
}
return i;
}
int fillQuestionsArray(string questions[], ifstream & inq){
char ch = ' ';
int i = 0;
while ((ch = inq.peek()) != EOF && i<MAX){
getline(inq, questions[i]);
i++;
}
return i;
}
void openFile(ifstream & input, const string & message){
bool done = false;
string In_File = "";
//Loop to get the name of a file that truly exists
// and check that file is not empty.
while (!done){
input.clear();
cout << "Please input the name of the data file with " << message << " to be read : ";
getline(cin, In_File);
cout << "The file name entered is : " << In_File << endl;
input.open(In_File.c_str());
if (input.fail()){
cout << "The file " << In_File << " does not exist. ";
done = false;
}
else{
input.peek();//peek at the input file
if (input.eof()){//If file is empty then the function eof() returns true
cout << "The file has no data in it ";
done = false;
input.close();
}
else{
done = true;
}
}
}//end of while loop
cout << "File " << In_File << " opened and has data in it." << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.