Advertisement
markyrocks

c++ quiz game

Jun 3rd, 2021 (edited)
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <chrono>
  4. #include <map>
  5. #include <vector>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. template <typename T>
  11. struct container {
  12.     int size;
  13.     T* tainer;
  14.     container<T>() : size(0), tainer(new T[1]){}
  15.     container<T>(int s) : tainer(new T[s]), size(s){}
  16.     container<T>(const vector<T>& v) : size(v.size()), tainer(new T[size]){
  17.         T* cc = tainer;
  18.         for (auto i = v.begin(); i < v.end(); i++) {
  19.             *cc = *i;
  20.             cc++;
  21.         }
  22.     }
  23.     container<T>(int s, T val) : size(s), tainer(new T[size]){
  24.             auto cc = tainer;
  25.             while (cc < tainer + size) { *cc = val; cc++; }
  26.         }
  27.     ~container<T>() { delete[] tainer; }
  28.  
  29.     void shuffle() {
  30.         srand(time(0));
  31.         int a = rand() % 100;
  32.         while (a) {
  33.             swap(this->tainer[rand() % (size - 1)], this->tainer[rand() % (size - 1)]);
  34.             a--;
  35.         }
  36.     }
  37.     T& get(int index) {
  38.         return this->tainer[index];
  39.     }
  40.     void push_back(T val) {
  41.         T* c = new T[this->size+1];
  42.         auto cc = c;
  43.         auto cc_t = this->tainer;
  44.         while (cc_t < this->tainer + this->size) {
  45.             *cc = *cc_t;
  46.             cc++; cc_t++;
  47.         }
  48.         c[this->size] = val;
  49.         //delete this->tainer; //<~~~~~~~~~~not sure why this delete was giving me issues something going out of scope most likely
  50.         this->tainer = c;
  51.         this->size++;
  52.     }
  53.     T& operator[](int index) { return this->get(index); }
  54.  
  55. };
  56.  
  57. struct question {
  58.     string q, a;
  59.  
  60.     question() :q(""), a("") {}
  61.     question(string qu, string ans) :q(qu), a(ans) {}
  62.  
  63.     string get_q() {
  64.         return this->q;
  65.     }
  66.     string get_a() {
  67.         return this->a;
  68.     }
  69.     void set_q(string que) {
  70.         this->q = que;
  71.     }
  72.     void set_a(string ans) {
  73.         this->a = ans;
  74.     }
  75.     void ask() { cout << this->q << "\n\n"; }
  76.     void show_answer() { cout << "The Answer is = " << this->a << "\n\n"; }
  77.     bool compare(string a) {  //this isn't the greatest but working ok for the moment
  78.        
  79.         if (this->a.find(a) != this->a.npos) {
  80.             return true;
  81.         }
  82.         return false;
  83.     }
  84.     bool operator==(string a) {
  85.         return this->compare(a);
  86.     }
  87.     bool get_answer() {
  88.         string ans{};
  89.         int c = 0;
  90.         while (c < 3) {
  91.             cout << "Please Enter Your answer............Tries Left : " << (c == 0 ? "X X X" : c==1 ? "X X" : "X") << " \n\n";
  92.             cin >> ans;
  93.             if (this->compare(ans)) {
  94.                 cout << "Correct!!!\n\n";
  95.                 break;
  96.             }
  97.             c++;
  98.              if(c<3){
  99.                 cout << "Incorrect, Try again\n\n";
  100.                 this->ask();
  101.             }
  102.            
  103.         }
  104.         if (c < 3) { return true; }
  105.         else { this->show_answer(); }
  106.         return false;
  107.     }
  108. };
  109.  
  110. struct quiz {
  111.     int size,correct;
  112.     container<question> con;
  113.  
  114.     quiz() :size(0), con(),correct(0) {}
  115.     quiz(int size) :size(size), con(size),correct(0) {}
  116.     quiz(vector<question>& v) :size(v.size()), con(v),correct(0) {}
  117.  
  118.     void add_question(question q) {
  119.         this->con.push_back(q);
  120.         this->update_size();
  121.     }
  122.     void add_question(string q, string a) {
  123.         this->con.push_back(question(q, a));
  124.         this->update_size();
  125.     }
  126.     void add_question(ifstream& ifs) {
  127.         char s[156]{};
  128.         bool t = false;
  129.         question q{};
  130.         while (ifs.getline(s,156,'\n')) {
  131.             if (!t) { q.set_q(string(s)); }
  132.             else {
  133.                 q.set_a(string(s));
  134.                 this->con.push_back(q);
  135.             }
  136.             t = !t;
  137.             this->update_size();
  138.         }
  139.     }
  140.     question& get(int index) {
  141.         return this->con[index];
  142.     }
  143.     question& operator[](int index) {
  144.         return this->get(index);
  145.     }
  146.     void update_size() { this->size = this->con.size; }
  147.     void ask(question& q) { q.ask(); }
  148.  
  149.     bool get_answer(question& q) { return q.get_answer(); }
  150.     void shuffle() { this->con.shuffle(); }
  151.     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"; }
  152.     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"; }
  153.     void execute() {
  154.         welcome();
  155.         for (auto i = this->con.tainer; i < con.tainer+con.size; i++) {
  156.             ask(*i);
  157.             if (get_answer(*i)) { this->correct++; }
  158.         }
  159.         exit_msg();
  160.     }
  161.    
  162. };
  163.  
  164. int main() {
  165.    
  166.     ifstream ifs;
  167.     ifs.open("qs.txt");
  168.     if (!ifs.is_open()) { cout << "file not open "; return 0; }
  169.  
  170.     quiz test{};
  171.     test.add_question(ifs);
  172.     //test.add_question("how many nuggets are in a 4 piece?", "4");
  173.     //test.add_question("how many days in a month?", "28 30 31");
  174.     //test.add_question("how many hours are there in a week?", "168");
  175.     //test.add_question("how many days in a weeK?", "7");
  176.     //test.add_question("how much wood would a wood chuck chuck if a wood chuck could chuck wood?", "73");
  177.     test.shuffle();
  178.     //test.con[0].ask();
  179.     //test.con[1].ask();
  180.     //test.con[2].ask();
  181.     //test.con[3].ask();
  182.     test.execute();
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement