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 154 155 156 157 158 159 160 161 162
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//question class
class Question
{
protected:
string question;
int value;
public:
Question(string theQuestion, int pointValue)
{
question = theQuestion;
value = pointValue;
}
string getQuestion(string qu)
{
question = qu;
return question;
cout << "\t" << question << endl;
}
int getValue(int val)
{
value = val;
return value;
cout << "Worth " << value << " points" << endl;
}
virtual ~Question(){}
virtual string printOptions()
{
return NULL;
}
virtual string getAnswer()
{
return NULL;
}
}; //end question class
//true false class
class QuestionTF : public Question
{
private:
string answer;
public:
QuestionTF(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue)
{
answer = theAnswer;
}
string printOptions() const
{
cout << "\t(true/false)" << endl;
return NULL;
}
string getAnswer(string ans)
{
answer = ans;
return answer;
cout << "\tAnswer - " << answer << endl;
}
}; //end true false class
//multiple choice class
class QuestionMC : public Question
{
private:
string answer, options[];
public:
QuestionMC(string theQuestion, int pointValue, string theAnswer) : Question(theQuestion, pointValue)
{
theAnswer = answer;
}
string addOption(string anOption)
{
for (int i = 0; i < 4; i++)
{
anOption = options[i];
}
return 0;
}
string printOptions()
{
return 0;
}
string getAnswer(string ans)
{
ans = answer;
return answer;
cout << "\tAnswer - " << answer << endl;
}
}; //end multiple choice class
//function to read the bank in
void testBank()
{
//declarations
int numQuestions, numOptions, points;
string questionType, questionText, answerText, mcOption;
//arrays
string questionBank[numQuestions] = {}, answerBank[numQuestions] = {}, mcOptions[numOptions] = {};
//class objects
Question test(questionText, points);
QuestionTF tf(questionText, points, answerText);
QuestionMC mc(questionText, points, answerText);
//read number of questions
fstream infile;
infile >> numQuestions;
//loop for test questions
for (int i = 1; i <= numQuestions; i++)
{
infile >> questionType >> points;
cout << "Question " << i << ":" << endl;
test.getValue(points);
test.getQuestion(questionType);
//true/false
if (questionType == "TF")
{
getline(infile, questionText);
getline(infile, answerText);
test.getQuestion(questionText);
tf.printOptions();
tf.getAnswer(answerText);
}//end true/false
//multiple choice
else if (questionType == "MC")
{
getline(infile, questionText);
infile >> numOptions;
for (int j = 1; j <= numOptions; j++)
{
getline(infile, mcOption);
mc.addOption(mcOption);
}
getline(infile, answerText);
mc.getAnswer(answerText);
}//end multiple choice
}//end loop
};//end test bank function
int main()
{
//open test bank file
ifstream infile;
infile.open ("testbank.txt");
//error if file can't open
if (!infile)
{
cout << "File could not be opened" << endl;
}
testBank();
infile.close();
return 0;
}//end main
|