Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question -question: string -answer : string +Question() //constructor. Set insta

ID: 3546468 • Letter: Q

Question


Question

-question: string -answer : string


+Question()

//constructor. Set instance variables

+Question(query : string, result : string)

//constructor. Set instance variables

+getQuestion() : string

//returns the question

+getAnswer : string

//returns the answer to this question

+answerCorrect(candidateAnswer :string) : boolean

//returns true if the candidate answer matches the answer

+displayQuestion() : void

//displays this question and its answer


const int MAX_QUESTIONS = 25; //the size of the array


Quiz

-questions : Question[

MAX_QUESTIONS] //an array named questions that holds Question objects

-current : int

//value of the index of the current location available in the questions array

-correct : int

// number of correct answers

-incorrect : int

//number of incorrect answers

+Quiz( )

//constructor. Instance variables current, correct, and incorrect set to 0.

+add(newQuestion : Question) : void

//add the specified question to the questions array if room available

+giveQuiz( ) : void

// present each question to the user, accept an answer for each one,

// and keep track of the results (the number of correct and incorrect answers).

+getNumCorrect() : int

//returns the number of correct answers

+getNumIncorrect() : int

//returns the number of incorrect answers




SO if already have a quiz.h and question.h

Define a tester/driver class called

QuizShow with a main method that creates a Quiz object. Create 5 Question objects (read in a question and answer from a file) and populate the quiz using the add method. Then give the quiz and print the final results.

Question

-question: string -answer : string


+Question()

//constructor. Set instance variables

+Question(query : string, result : string)

//constructor. Set instance variables

+getQuestion() : string

//returns the question

+getAnswer : string

//returns the answer to this question

+answerCorrect(candidateAnswer :string) : boolean

//returns true if the candidate answer matches the answer

+displayQuestion() : void

//displays this question and its answer

Explanation / Answer

//Question.h

#ifndef QUESTION_H
#define QUESTION_H

#include <string>
#include <iostream>

class Question
{
public:
    Question() : question(""), answer("") { }
    Question(const std::string& query, const std::string& result)
    : question(query), answer(result) { }
    const std::string& getQuestion()const { return question; }
    const std::string& getAnswer()const { return answer; }
    bool answerCorrect(const std::string& candidateAnswer)const
    { return candidateAnswer == answer; }
    void displayQuestion()const
    {
        std::cout << "Question: " << question << " "
                  << "Answer: " << answer << " ";
    }
private:
    std::string question;
    std::string answer;
};

#endif // QUESTION_H




//Question.cpp is not needed




//Quiz.h

#ifndef QUIZ_H
#define QUIZ_H

#include "Question.h"

const int MAX_QUESTIONS = 25;

class Quiz
{
public:
    Quiz() : current(0), correct(0), incorrect(0) { }
    void add(const Question& newQuestion);
    void giveQuiz();
    int getNumCorrect()const { return correct; }
    int getNumIncorrect()const { return incorrect; }
private:
    Question questions[MAX_QUESTIONS];
    int current;
    int correct;
    int incorrect;
};

#endif // QUIZ_H




//Quiz.cpp

#include "Quiz.h"
#include <iostream>

void Quiz::add(const Question& newQuestion)
{
    if (current == MAX_QUESTIONS) return;
    questions[current++] = newQuestion;
}

void Quiz::giveQuiz()
{
    std::string ans;
    for (int i = 0; i < current; ++i)
    {
        std::cout << "Question " << i+1 << " "
                  << questions[i].getQuestion() << " ";
        std::cout << "Your answer: ";
        std::getline(std::cin, ans);
        if (questions[i].answerCorrect(ans))
        {
            ++correct;
            std::cout << "Correct. ";
        }
        else
        {
            ++incorrect;
            std::cout << "Incorrect. The answer is "
                      << questions[i].getAnswer() << " ";
        }
    }
}




//QuizShow.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "Quiz.h"

int main()
{
    Quiz quiz;
    std::string question;
    std::string answer;

    // Open questions file
    std::ifstream questionStream("questions.dat");
    if (!questionStream)
    {
        std::cerr << "Cannot open questions data.";
        return 1;
    }

    // Populate the quiz using add method
    while (std::getline(questionStream, question))
    {
        std::getline(questionStream, answer);
        quiz.add(Question(question, answer));
    }
    questionStream.close(); //close file

    // Give the quiz
    quiz.giveQuiz();

    // Print the final results
    std::cout << " Quiz result: "
              << " Correct answer(s): " << quiz.getNumCorrect() << " "
              << "Incorrect answer(s): " << quiz.getNumIncorrect() << " ";
}




//Content of questions.dat:

What famous document begins: "When in the course of human events..."?
The Declaration of Independence
What so-called "war" spawned the dueling slogans "Better Dead Than RED" and "Better Red Than Dead" in the 1950's?
The Cold War
Who said: "I'm the president of the United States and I'm not going to eat any more broccoli"?
George Bush
What 20th-century conflict was dubbed the "forgotten war" despite 54,246 U.S. deaths?
The Korean War
Who was the last president of the Soviet Union?
Mikail Gorbachev