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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
//*Looking forward to add exciting Features to this Console Based application
//
// If there is any thing one can help with to update and to make this MCQ app great
//
// please Address it to me anything or functions I may have to add to make it work well
//
// I will really appreciate and more
//
//Currently planning to add methods like display, play, highscore, addQuestions,
// Question vary in level and time based*/
* */
//
// Created by Sithesh on 5/27/2021.
//
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include "Quiz.h"
using namespace std;
Quiz::Quiz(const string& question, const vector<string>& answers, int correct, const string& level, const string& type) :
question(question), answers(answers), correct(correct), level(level), type(type) {
}
const string &Quiz::getQuestion() const {
return question;
}
const string &Quiz::getType() const {
return type;
}
const vector<string> &Quiz::getAnswers() const {
return answers;
}
int Quiz::getCorrect() const {
return correct;
}
const string &Quiz::getLevel() const {
return level;
}
void load1(istream& in, vector<Quiz>& quiz) {
string question, A, B, C, D, type, level, emptyLine;
string correctIndex;
vector<string> answers;
if(!in) { //Always test the file open.
cout<<"Error opening output file"<<endl;
system("pause");
}
while (getline(in, question) && getline(in, type)
&& getline(in, level) && getline(in, correctIndex)){
//I find this interesting but I am not sure how can I got about it to ensure that thi
/*Within the text file it can detect which of the questions are true or False and
* which Are made out of 4 choices and to final add the them in the vector quiz */
/*getline(in, question);
getline(in, type );
getline(in, level);
getline(in, correctIndex );
cout<<question<<endl;
if(type=="TF"){
getline(in, A);
getline(in, B);
cout<<A<<endl;
answers ={A, B};
} else{
getline(in, A );
getline(in, B);
getline(in, C);
getline(in, D);
answers ={A, B, C, D};
}
getline(in, emptyLine);
quiz.emplace_back(question, answers, stoi(correctIndex),level,type);
*/
// Trying to use the data from the load(...) method it gives me error of "
/* terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi*/
}
}
void load(istream& in, vector<Quiz>& quiz){
string question, A, B, C, D, type, level, emptyLine;
string correctIndex;
vector<string> answers;
if(!in) { //Always test the file open.
cout<<"Error opening output file"<<endl;
system("pause");
}
while(!in.eof()){
getline(in, question, '\n');
getline(in, type, '\n');
getline(in, level, '\n');
getline(in, correctIndex , '\n');
if(type=="TF"){
getline(in, A, '\n');
getline(in, B, '\n');
answers ={A, B};
} else{
getline(in, A, '\n');
getline(in, B, '\n');
getline(in, C, '\n');
getline(in, D, '\n');
answers ={A, B, C, D};
}
getline(in, emptyLine , '\n');
quiz.emplace_back(question, answers, stoi(correctIndex),level,type);
}
}
int main(){
string fileName="example.txt";
ifstream in(fileName.c_str());
vector<Quiz> quizzes;
load(in, quizzes);
for (int i = 0; i <quizzes.size(); ++i) {
cout<< quizzes.at(i).getQuestion()<< endl;
cout<<(quizzes.at(i).getAnswers()).at(quizzes.at(i).getCorrect())<<endl;
}
return 0;
}
//The "Quiz.h" File below;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//
// Created by Sithe on 5/27/2021.
//
#ifndef COMP315FINALPROJECT_QUIZ_H
#define COMP315FINALPROJECT_QUIZ_H
class Quiz{
public:
Quiz(){
}
Quiz(const string&, const vector<string>&, int, const string& , const string&);
void askQuestion();
vector<string> getChoices();
void display();
bool getFileContent(string,vector<string>&);
const string &getQuestion() const;
const string &getType() const;
const vector<string> &getAnswers() const;
int getCorrect() const;
const string &getLevel() const;
private:
string question, type;
vector<string> answers;
int correct;
string level;
};
#endif //COMP315FINALPROJECT_QUIZ_H
|