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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
// quiz class.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <ctime>
#include<iostream>
#include <sstream>
using namespace std;
#include<fstream>
#include<string>
#include<iomanip>
template <class T>
string ToString(T thing) //to convert int to string to start printing when a random no is found
{
ostringstream oss;
oss << thing;
return oss.str();
}
class Scoremanager
{
private:
public:
void showscoreboard();
};
class Player
{
private:
char* m_name;
int m_score;
public:
friend class Scoremanager;
Player()
{
m_name = " ";
m_score = 0;
}
void setscore(int score)
{
m_score += score;
}
void setname(char* name)
{
m_name = name;
}
int getscore()
{
return m_score;
}
char* getname()
{
return m_name;
}
};
class QnA
{
private:
int m_Qno;
int category;
public:
void showQuestion(int cQno, string cquesfname)
{
//int c_randnum = Qno;
readques(cQno,cquesfname);
}
void showAnswer(int cQno, string cans, string cquesfname, string cansfname)
{
readans(cQno,cans,cquesfname,cansfname);
}
void readques(int randNum,string filename)
{
int n=0;
string ans;
string saveans;
string print;
ifstream ques(filename); //passing a string containing the name of the file to be opened for reading the questions from
ifstream anss("ans.txt");
int a=0;
string hash = "*";
string chk;
int c[200]={0};
while(a<1) // To get you all the lines.
{
again:
getline(ques,chk); // Saves the line in STRING.
while(a<1)
{
if (chk == ToString(randNum))
{
getline(ques,print); // Saves the line in STRING.
if (print != hash)
cout<<print<<endl; // Prints our STRING.
else
goto ibreak;
}
else
goto again;
}
}
ibreak:
;
}
void readans(int randNum,string ans,string cquesfname,string cansfname)
{
int n=0;
string saveans="";
string print="";
ifstream ques(cquesfname); //passing a string containing the name of the file to be opened for reading the questions from
ifstream anss(cansfname);
int a=0;
string hash = "*";
string chk="";
int c[200]={0};
ques.seekg(0);
anss.seekg(0);
n=n+1;
while(a<1)
{
getline(anss,saveans); // Saves the line in STRING.
if (saveans == ToString(randNum))
{
getline(anss,saveans);
if (saveans == ans)
{
cout<<"correct!"<<endl;
cout<<"# next question # "<<endl;
}
else
{
cout<<"wrong answer!"<<endl; // Prints our STRING
cout<<"# next question # "<<endl;
}
break;
}
}
}
};
class SciTech : public QnA
{
public:
string quesfilename()
{
return "question_read.txt";
}
string ansfilename()
{
return "ans.text";
}
};
class History : public QnA{};
class Sports : public QnA{};
class Geography : public QnA{};
class Music : public QnA{};
class Politics : public QnA{};
class Mathematics : public QnA{};
class Movies : public QnA{};
int main()
{
SciTech o_SciTech;
History o_History;
Sports o_Sports;
Geography o_Geography;
Music o_Music;
Politics o_Politics;
Mathematics o_Mathematics;
Movies o_Movies;
int num_players ;
string player_name = "";
int choice;
int min=1,max=20;
string main_quesfname;
string main_ansfname;
string ans;
cout << "welcome to the quiz" << endl;
cout << "enter the no of players" << endl;
cin >> num_players ;
for(int i=1; i<=num_players; i++)
{
cout << "enter the name of " << i << "th player" << endl;
cin >> player_name;
}
Player *player = new Player[num_players];
cout << "Player 1 " << endl;
cout << "choose a category for the round 1 from the list" << endl;
cout << " 1) Science and technology " << endl
<< " 2) History " << endl
<< " 3) Sports " << endl
<< " 4) Movies " << endl
<< " 5) Geography " << endl
<< " 6) Music " << endl
<< " 7) Politics " << endl
<< " 8) Mathematics " << endl;
cin >> choice;
cout<< "lets begin Round 1" << endl;
switch (choice)
{
case 1:
for(int i=1; i<=10; i++)
{
int Q_Rand = (rand()%(max-min + 1)) + min;
main_quesfname = o_SciTech.quesfilename();
main_ansfname = o_SciTech.ansfilename();
o_SciTech.showQuestion(Q_Rand,main_quesfname);
cout << "what is your answer a, b, c or d " << endl;
cin >> ans;
o_SciTech.showAnswer(Q_Rand,ans,main_quesfname,main_ansfname);
}
break;
case 2:
//o_History.showQuestion(Q_Rand,choice);
break;
case 3:
//o_Sports.showQuestion(Q_Rand,choice);
break;
case 4:
//o_Movies.showQuestion(Q_Rand,choice);
break;
case 5:
//o_Geography.showQuestion(Q_Rand,choice);
break;
case 6:
//o_Music.showQuestion(Q_Rand,choice);
break;
case 7:
//o_Politics.showQuestion(Q_Rand,choice);
break;
case 8:
//o_Mathematics.showQuestion(Q_Rand,choice);
break;
}
return 0;
}
|