Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <chrono>
- #include <map>
- #include <vector>
- #include <fstream>
- using namespace std;
- template <typename T>
- struct container {
- int size;
- T* tainer;
- container<T>() : size(0), tainer(new T[1]){}
- container<T>(int s) : tainer(new T[s]), size(s){}
- container<T>(const vector<T>& v) : size(v.size()), tainer(new T[size]){
- T* cc = tainer;
- for (auto i = v.begin(); i < v.end(); i++) {
- *cc = *i;
- cc++;
- }
- }
- container<T>(int s, T val) : size(s), tainer(new T[size]){
- auto cc = tainer;
- while (cc < tainer + size) { *cc = val; cc++; }
- }
- ~container<T>() { delete[] tainer; }
- void shuffle() {
- srand(time(0));
- int a = rand() % 100;
- while (a) {
- swap(this->tainer[rand() % (size - 1)], this->tainer[rand() % (size - 1)]);
- a--;
- }
- }
- T& get(int index) {
- return this->tainer[index];
- }
- void push_back(T val) {
- T* c = new T[this->size+1];
- auto cc = c;
- auto cc_t = this->tainer;
- while (cc_t < this->tainer + this->size) {
- *cc = *cc_t;
- cc++; cc_t++;
- }
- c[this->size] = val;
- //delete this->tainer; //<~~~~~~~~~~not sure why this delete was giving me issues something going out of scope most likely
- this->tainer = c;
- this->size++;
- }
- T& operator[](int index) { return this->get(index); }
- };
- struct question {
- string q, a;
- question() :q(""), a("") {}
- question(string qu, string ans) :q(qu), a(ans) {}
- string get_q() {
- return this->q;
- }
- string get_a() {
- return this->a;
- }
- void set_q(string que) {
- this->q = que;
- }
- void set_a(string ans) {
- this->a = ans;
- }
- void ask() { cout << this->q << "\n\n"; }
- void show_answer() { cout << "The Answer is = " << this->a << "\n\n"; }
- bool compare(string a) { //this isn't the greatest but working ok for the moment
- if (this->a.find(a) != this->a.npos) {
- return true;
- }
- return false;
- }
- bool operator==(string a) {
- return this->compare(a);
- }
- bool get_answer() {
- string ans{};
- int c = 0;
- while (c < 3) {
- cout << "Please Enter Your answer............Tries Left : " << (c == 0 ? "X X X" : c==1 ? "X X" : "X") << " \n\n";
- cin >> ans;
- if (this->compare(ans)) {
- cout << "Correct!!!\n\n";
- break;
- }
- c++;
- if(c<3){
- cout << "Incorrect, Try again\n\n";
- this->ask();
- }
- }
- if (c < 3) { return true; }
- else { this->show_answer(); }
- return false;
- }
- };
- struct quiz {
- int size,correct;
- container<question> con;
- quiz() :size(0), con(),correct(0) {}
- quiz(int size) :size(size), con(size),correct(0) {}
- quiz(vector<question>& v) :size(v.size()), con(v),correct(0) {}
- void add_question(question q) {
- this->con.push_back(q);
- this->update_size();
- }
- void add_question(string q, string a) {
- this->con.push_back(question(q, a));
- this->update_size();
- }
- void add_question(ifstream& ifs) {
- char s[156]{};
- bool t = false;
- question q{};
- while (ifs.getline(s,156,'\n')) {
- if (!t) { q.set_q(string(s)); }
- else {
- q.set_a(string(s));
- this->con.push_back(q);
- }
- t = !t;
- this->update_size();
- }
- }
- question& get(int index) {
- return this->con[index];
- }
- question& operator[](int index) {
- return this->get(index);
- }
- void update_size() { this->size = this->con.size; }
- void ask(question& q) { q.ask(); }
- bool get_answer(question& q) { return q.get_answer(); }
- void shuffle() { this->con.shuffle(); }
- void welcome() { cout << "Welcome to the Quiz Game Try your luck at all " << this->size << " questions \n Do you have what it takes to beat the high score???\n\n"; }
- void exit_msg() {cout << "Nice job you got " << this->correct << " answers correct out of " << this->size<<" \n\n Try again Can you get a perfect score ? \n"; }
- void execute() {
- welcome();
- for (auto i = this->con.tainer; i < con.tainer+con.size; i++) {
- ask(*i);
- if (get_answer(*i)) { this->correct++; }
- }
- exit_msg();
- }
- };
- int main() {
- ifstream ifs;
- ifs.open("qs.txt");
- if (!ifs.is_open()) { cout << "file not open "; return 0; }
- quiz test{};
- test.add_question(ifs);
- //test.add_question("how many nuggets are in a 4 piece?", "4");
- //test.add_question("how many days in a month?", "28 30 31");
- //test.add_question("how many hours are there in a week?", "168");
- //test.add_question("how many days in a weeK?", "7");
- //test.add_question("how much wood would a wood chuck chuck if a wood chuck could chuck wood?", "73");
- test.shuffle();
- //test.con[0].ask();
- //test.con[1].ask();
- //test.con[2].ask();
- //test.con[3].ask();
- test.execute();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement