OBJECTIVES This assignment will exercise the use of OOP classes, accessor functi
ID: 3860409 • Letter: O
Question
OBJECTIVES
This assignment will exercise the use of OOP classes, accessor function, mutator functions, and arrays of objects.
Write a program quiz generator program to help students study for quizzes and exams. The program must call a function that reads a set of multiple choice questions from a file, storing the questions in an array of object of the class Question. The class maintains the question text, an array of possible answers, and the correct answer.
After loading the questions, the program should loop through the array prompting the student with the question and checking the student’s answer. The program should indicate “correct” to the student, if the answer is correct, otherwise indicate “incorrect” and display the correct answer (see sample) below.
After all questions have been answered, the program should display the student score in points, and correct score percentage.
The question file format is as follows:
(1) question text?
first possible answer
second possible answer
third possible answer
fourth possible answer
fifth possible answer
x correct answer number.
(2) question text?
first possible answer
second possible answer
third possible answer
fourth possible answer
fifth possible answer
x correct answer number.
…
(n) question text?
first possible answer
second possible answer
third possible answer
fourth possible answer
fifth possible answer
x correct answer number.
Your program should meet the following requirements
main()
Declare variables and Question array. Use a constant for the number_of_questions.
Call initQuestions() function to load array with questions.
Use a loop to prompt student with question and read answer.
Call displayQuestion to display question.
use cin >> ans to read student answer.
If the answer is correct, display correct
Else display incorrect and then display the correct answer.
Increment array index and repeat loop until all questions
After all questions have been issues, display student results.
initQuestions()
Define a function (initQuestions()) that implements the following logic:
Opens questions.txt and checks for a valid open – issues error and exits if not successful.
Uses a while loop to read the question text, five answers, and the correct answer number. Use setter methods to store the questions in a Question object array. Be certain to protect against data overrunning the array.
displayQuestion()
Define a function (displayQuestion())) that displays the question text and possible answers. (See sample run for format).
Question Class
Define a class called Question in its own header file Question.h
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
#define NO_OF_QUESTIONS 100
class Question{
private:
string text;
string options[5];
int answer;
public:
Question(){
text = "";
for (int i = 0; i<5; i++)
options[i] = "";
answer = 0;
}
void setText(string str){
text = str;
}
void setAnswer(int b){
answer = b;
}
string getText(){
return text;
}
void setOptions(string a[]){
for (int i = 0; i<5; i++)
options[i] = a[i];
}
void getOptions(){
for (int i = 0; i<5; i++)
cout << options[i] << endl;
}
int getAnswer(){
return answer;
}
};
void initQuestions(Question data[], int &count){
ifstream fin;
string line;
string option_data[5];
int ans, value;
int option;
fin.open("questions.txt");
option = -1;
ans = 0;
count = 0;
if (fin) {
while (getline(fin,line)){
if (line.find("?") != std::string::npos && option == -1 && ans == 0){// for identifying end of question
data[count].setText(data[count].getText()+line);
option = 0;
continue;
}
if (line.find("?") == std::string::npos && option == -1 && ans == 0){
data[count].setText(data[count].getText()+line);
continue;
}
if (option >= 0){
option_data[option] = line;
if (option == 4){// All the options have been read.
data[count].setOptions(option_data);
option = -1;
ans = 1;
}
if (option != -1)
option++;
continue;
}
if (ans == 1 && option == -1){ //For reading the answer and also restting to go for the next question
istringstream iss(line);
iss >> value;
data[count].setAnswer(value);
ans = 0;
count++;
option = -1;
}
}
fin.close();
}
else {
cout << "Error opening file" << endl;
}
}
void displayQuestion(Question a){
cout << a.getText() << endl;
a.getOptions();
}
int main(){
Question data[NO_OF_QUESTIONS];
int count = 0;
int choice;
int correct_ans_count = 0;
initQuestions(data, count);
cout << count << endl;
for (int i = 0; i<count; i++){
displayQuestion(data[i]);
cout << "Enter your choice :";
cin >> choice;
if (choice == data[i].getAnswer()){
cout << "Correct" << endl;
correct_ans_count++;
}
else {
cout << "Incorrect Answer" << endl;
cout << "Correct Answer is " << data[i].getAnswer() << endl;
}
}
cout << "Total Score: "<< correct_ans_count << " correct answers" << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.