1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
const int s_questionScore = 4; // Points rewarded for each correct answer.
class Question {
public:
void setValues(std::string, std::string, std::string, std::string, std::string, char);
int askQuestion(int num = -1);
friend std::istream& operator >> (std::istream& is, Question& ques);
private:
std::string Question_Text;
std::string answer_1;
std::string answer_2;
std::string answer_3;
std::string answer_4;
char correct_answer;
};
void load(std::istream& is, std::vector<Question>& vques);
void load_sstream(std::istringstream & ss);
int main()
{
// std::ifstream fin("quiz_data.txt");
std::istringstream fin;
load_sstream(fin);
std::vector<Question> questions;
load(fin, questions);
std::cout << "Number of questions = " << questions.size() << '\n';
int total = 0;
for (size_t i=0; i<questions.size(); ++i)
{
total += questions[i].askQuestion(i+1);
std::cout << "Total score = " << total << '\n';
}
}
std::istream& operator >> (std::istream& is, Question& ques)
{
std::string line;
while (getline(is, line))
{
if (line.size() == 0)
continue;
break;
}
ques.Question_Text = line;
getline(is, ques.answer_1);
getline(is, ques.answer_2);
getline(is, ques.answer_3);
getline(is, ques.answer_4);
is >> ques.correct_answer;
return is;
}
void load(std::istream& is, std::vector<Question>& vques)
{
Question q;
while (is >> q)
vques.push_back(q);
}
//Format for possible answers displayed when program executes.
int Question::askQuestion(int num)
{
int score = 0;
// Ask the question.
std::cout << "\n";
if (num > 0)
std::cout << num << ". ";
std::cout << Question_Text << "\n";
std::cout << "a. " << answer_1 << "\n";
std::cout << "b. " << answer_2 << "\n";
std::cout << "c. " << answer_3 << "\n";
std::cout << "d. " << answer_4 << "\n";
std::cout << "\n";
char guess;
//Ask user for their answer.
std::cout << "What is your answer?" << "\n";
std::cin >> guess;
//If their answer is correct, message is displayed and 4 points are added to their score.
if (guess == correct_answer) {
std::cout << "\n";
std::cout << "Correct!" << "\n";
score = s_questionScore;
std::cout << "\n";
std::cout << "Press enter to continue." << "\n";
std::cin.get();
std::cin.ignore();
}
else //If their answer is incorrect, message is displayed, no points added.
//Correct answer displayed.
{
std::cout << "\n";
std::cout << "Sorry, you're wrong..." << "\n";
std::cout << "The correct answer is " << correct_answer << "." << "\n";
std::cout << "\n";
std::cout << "Press enter to continue." << "\n";
std::cin.get();
std::cin.ignore();
}
return score;
}
void load_sstream(std::istringstream & ss)
{
ss.str(R"(
What command prints something to the screen?
cin
cout
char
print
b
Which of the following categories does C++ belong to?
Operating System
High-level programming language
low-level programming language
Compiler
b
Which command is correctly written?
cout >>
cin <<
cout <>
cin >>
d
What is this called, <iostream>?
directive
pre-processor directive
file
command
b
)");
}
|