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);
}
and link of the question "D:\Folder\Question.txt"