Quiz (C++)

By sunslayer on Feb 15, 2011

haven't touched C++ for almost a year so I'm a bit rusty but this is basically http://www.hawkee.com/snippet/8379/ in C++

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool isans(string);

struct Questions
{
    string Q,ans,a,b,c,d;
    bool t;
};

class Quiz
{
    public:
        ifstream file;
        ifstream::pos_type len;
        string line;
        char const* fname;
        Questions *quiz;
        int lines,r,w,t;
        Quiz(char const*);
        ~Quiz(void);
        int getSize(ifstream&);
        void start(void);
        void collect(ifstream&);
        void shuffle(void);
};

Quiz::Quiz(char const* f)
{
    fname = f;
    file.open(f);
    quiz = 0;
    r = w = t = 0;
    lines = getSize(file);
    quiz = new (nothrow)Questions[lines];
    if(quiz == 0)
    {
        file.close();
        cerr << "Error allocating memory!";
    }
    collect(file);
}

Quiz::~Quiz()
{
    if(file.is_open())
        file.close();
    if(quiz != 0)
        delete[] quiz;
}

int Quiz::getSize(ifstream& file)
{
    int a = 0;
    file.seekg(0,ios::beg);
    for(string line;file.good();)
        if (file >> line && line.length() == 1 && isans(line))
            a++;
    return a;
}

void Quiz::collect(ifstream& file)
{
    file.close();
    file.open(fname);
    Questions *p;
    string l;
    for(int a = 1,b = 0;file.good() && getline(file,l) && l != "end";a++)
    {
        p = &quiz[b];
        switch(a)
        {
            case 1:
                p->ans = l;
                break;
            case 2:
                p->Q = l;
                break;
            case 3:
                p->a = l;
                break;
            case 4:
                p->b = l;
                break;
            case 5:
                p->c = l;
                break;
            case 6:
                p->d = l;
                p->t = true;
                b++;a = 0;
        }
    }
}

void Quiz::start()
{
    string input;
    Questions *p;

    for(int a = 0;quiz[a].t;a++)
    {
        p = &quiz[a];
        cout << "Question " << a+1 << ": " << p->Q << endl;
        cout << "A. " << p->a << endl;
        cout << "B. " << p->b << endl;
        cout << "C. " << p->c << endl;
        cout << "D. " << p->d << endl;
        getline(cin,input);
        if(input == "exit" || input == "quit")
            break;
        (input==p->ans)?r++:w++;
        t++;
    }
}

int main()
{
    Quiz Q("PATH\TO\QUIZ\FILE");

    Q.start();
    cout << "\nYou answered " << Q.r << " questions out of " << Q.t << " Correctly.";

    return 0;
}

bool isans(string l)
{
    string a = "A", b = "B", c = "C", d = "D";
    return ((l==a||l==b||l==c||l==d)?true:false);
}

Comments

Sign in to comment.
Lizt[N]ight~   -  Apr 30, 2016

Create some file like " Question.txt "

your answer <<<< line 1
Question <<<< line 2
Option A <<<< line 3
Option B <<<< line 4
Option C <<<< line 5
Option D <<<< line 6

Lizt[N]ight~  -  Apr 30, 2016

and link of the question "D:\Folder\Question.txt"

Sign in to comment

Lizt[N]ight~   -  Mar 26, 2016

how to insert the question text ?

 Respond  
stop   -  Oct 17, 2011

Hi, what program will i use to make my own C/C++? and how to save it?
Any tutorial,video. etc. pls.
Thank you. :)

 Respond  
Hawkee   -  Feb 19, 2011

I appreciate you posting snippets in languages other than mIRC, it's great to see some variety.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.