*NEED HELP WITH NUMBER 9* Write a C++ documented program to perform the role of
ID: 3689719 • Letter: #
Question
*NEED HELP WITH NUMBER 9*
Write a C++ documented program to perform the role of a quiz maker. The program should display True/False questions, record user's answers, keep the time, record the scores and more. Execution of the program should be similar to this:
1.Prompt the student to enter his/her name, ID number, and then start the quiz.
2. Read 10 random questions and their answers (solution) from a text file (.txt).
3. Display first question
4. Get answer from the user
5. Update user's score if he answered correctly
6. Display next question
7. Repeat steps 4, 5, and 6 until the final question.
8. Record the user's name and ID, total score, and detailed answers into a text file and to the screen.
9. Extra credits if your program provides a login option where only the user's listed in an external file can take the quiz
Requirements:
1. In each quiz, your program should display 10 unique questions (no repeats)
2. User's have 10 minutes to complete the quiz, after 10 minutes the program should automatically terminate the quiz and report the outputs to screen and files
3. Optimize the code by using functions and arrays. Your program should include at least two functions and one array.
Explanation / Answer
/** C++ code to perform the role of a quiz maker **/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <time.h> //used to terminate code after 10 minutes
using namespace std;
int read(vector<string> &question, vector<string> &answer)
{
ifstream file_("inputfilename"); // opening file
int i = 0;
string line;
string result;
while(file_)
{
getline(file_,line);
question.push_back(line);
getline(file_,result);
answer.push_back(result);
}
int n = question.size();
file_.close(); // closing file
return n;
}
int quiz(vector<string> &question, vector<string> &answer,int size)
{
ofstream file("outputfilename"); // opening output file
string name;
cout << "Enter name: ";
getline(cin,name);
string ID;
cout << "Enter ID: ";
cin >> ID;
file << "Name: " << name << endl;
file << "ID: " << ID << endl;
int score = 0;
string response;
for (int i = 0; i < size; ++i)
{
cout << question[i] << " ";
cin >> response;
if(response == answer[i])
{
score++;
cout << "Correct answer ";
}
else cout << "Incorrect ";
file << "your response: " << response << endl;
file << "Correct answer: " << answer[i] << endl;
file << "Score: " << score << endl;
}
return score;
}
int main()
{ time_t start, stop;
start = time(NULL);
vector<string> question;
vector<string> answer;
int size = read(question,answer);
int score = quiz(question,answer,size);
stop = time(NULL);
if(stop-start > 600) {
printf(" Your 10 minutes are up! Ending program! ");
return 0;
}
cout << "your score is " << score << "/" << size << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.