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
|
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int no = 1;
class quizz {
public:
char question[100];
char answer[10];
void get() {
cout << "Enter the question" << endl;
gets(question);
cout << "Enter the answer" << endl;
gets(answer);
}
};
class participant {
char name[10];
int sno;
float score;
public:
void add() {
sno = 0;
cout << "Enter your name :" << endl;
gets(name);
score = 0;
}
void assign(int k) {
sno = k;
}
void show() {
cout << sno << " " << name << "\t" << score << endl;
}
void calc(float s) {
score = s;
}
int retsno() {
return sno;
}
float retscore() {
return score;
}
char *retname() {
return name;
}
};
void main()
{
system("cls");
char ch = 'Y';
int choice;
while (ch == 'Y' || ch == 'y') {
cout << "Press 1 to Enroll for quiz." << endl;
cout << "Press 2 to take up the quiz" << endl;
cout << "Press 3 to input a question into database" << endl;
cout << "Press 4 to display reults" << endl;
cout << "Press 5 to exit" << endl;
cin >> choice;
switch (choice) {
case 1:
{
participant p;
fstream f;
f.open("participant.dat", ios::out | ios::ate | ios::binary);
p.add();
p.assign(no);
no++;
f.write((char *) &p, sizeof(p));
cout << "Your are enrolled succesfully." << endl;
cout << "Your participant number is " << p.retsno() << endl;
f.close();
break;
}
case 2:
{
participant p, r;
quizz q;
char na[25];
float s = 0;
fstream f, f1;
char ques[30], ans[10];
int n, flag = 0;
f.open("quiz.dat", ios::in | ios::binary);
f1.open("participant.dat", ios::in | ios::binary);
cout << "Enter your participant number ! ";
cin >> n;
cout << "Enter your name ! ";
gets(na);
while (!f.eof()) {
f1.read((char *) &p, sizeof(p));
if ((strcmp(na, p.retname()) == 0) && p.retsno() == n) {
flag = 1;
break;
}
}
f1.close();
if (flag == 1) {
cout << "Quiz has begun." << endl <<
"1. For every correct answer you will get 4 marks" << endl;
cout << "2. For every incorrect answer you will lose 1 mark" << endl;
cout << "Please answer the following questions." << endl;
for (int i = 0; i < 3; i++) {
f.read((char *) &q, sizeof(q));
cout << q.question << endl;
gets(ans);
if (strcmp(q.answer, ans) == 0)
s += 4;
else
s -= 1;
}
f.close();
p.calc(s);
f1.open("participant.dat", ios::in | ios::out | ios::binary);
while (!f.eof()) {
f1.read((char *) &r, sizeof(r));
if ((strcmp(r.retname(), p.retname()) == 0)
&& r.retsno() == p.retsno()) {
f1.seekg(-sizeof(p), ios::cur);
f1.write((char *) &p, sizeof(p));
break;
}
}
}
f1.close();
break;
}
case 3:
{
char ch;
do {
fstream f;
quizz q;
f.open("quiz.dat", ios::out | ios::ate | ios::binary);
q.get();
f.write((char *) &q, sizeof(q));
f.close();
cout << "Do you want to input more questions ? (Y/N)";
cin >> ch;
}
while (ch == 'y' || ch == 'Y');
break;
}
case 4:
{
fstream f;
participant o, m;
float max = 0;
f.open("particpant.dat", ios::in | ios::binary);
while (f.read((char *) &o, sizeof(o))) {
o.show();
if (o.retscore() > max) {
m = o;
max = o.retscore();
}
}
f.close();
cout << "The winner of the quiz is..." << endl;
m.show();
break;
}
case 5:
{
exit(0);
}
default:
break;
}
cout << "Do you want to try again ? (Y/N)";
cin >> ch;
}
}
|