Quiz game

this code is generates Assertion failure at line 33

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
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<time.h>

using namespace std;

struct optn
{
	char o[50];
};

class question
{
	char que[500];
	char ans[50];
	optn options[6];
public:
	char* retans()
	{
		return ans;
	}
	char putq();
};

char question::putq()
{
	srand((unsigned) time(0));
	optn choices[4];
	for (int i = 0; i<4; i++)
	{
		strcpy_s(choices[i].o, options[rand() % 6].o);
	}
	for (int i = 0; i<4; i++)
	{
		for (int j = i + 1; j<4; j++)
		{
			if (choices[i].o == choices[j].o)
			{
				srand((unsigned) time(0));
				strcpy_s(choices[j].o, options[rand() % 6].o);
				i = 0;
				break;
			}
		}
	}
	srand((unsigned) time(0));
	int c = rand() % 4;
	strcpy_s(choices[c].o, ans);
	char cor;
	switch (c)
	{
	case 0: cor = 'a'; break;
	case 1: cor = 'b'; break;
	case 2: cor = 'c'; break;
	case 3: cor = 'd'; break;
	}
	cout << que << endl;
	cout << "\nOptions : \n";
	char t = 'a';
	for (int i = 0; i<4; i++)
	{
		cout << t << ". " << choices[i].o<<'\t'; t++;
	}
	cout << "\nEnter your choice : ";
	cin >> t;
	if (t == cor)
	{
		cout << "\a\nYour answer is right!!! \n";
		return 'r';
	}
	else
	{
		cout << "\a\nYour answer is wrong!!! \n";
		return 'w';
	}

}

int game();
//	void highscore();
void about();
int main()
{
l:	system("cls");
	int mench, score;
	cout << "****************Welcome to the quiz****************";
	cout << "\n                    1. Play Quiz                   \n";
	cout << "\n                  2. Add questions                 \n";
	//	cout<<"\n                   3. High Score                   \n";
	cout << "\n                     4. About                      \n";
	cout << "\nEnter your choice number or any other number to exit : ";
	cin >> mench;
	switch (mench)
	{
	case 1:score = game();
		cout << "\aYour score is : " << score;
		//	cout<<"\nHigh Score    : "<<highscore()<<endl;system("pause");break;			
	case 2: system("qpopulator.exe"); cout << endl; system("pause"); break;
		//		case 3: cout<<"\n\aHigh Score    : "<<highscore();cout<<endl;system("pause");break;
	case 4: about(); cout << endl; system("pause"); break;
	}
	goto l;

	return 0;
}
int game()
{
	int score = 0, qno = 1;
	question q;
	fstream fin("qlist.qaf", ios::binary | ios::in);
	while (qno>-1)
	{
		cout << "\n\aQestion " << qno << " :\n";
		fin.read((char *) &q, sizeof(q));
		if (q.putq() == 'r')
		{
			score = qno * 10;
			qno++;
			continue;
		}
		else
		{
			cout << "\a\n YOUR ANSWER IS WRONG!!! GAME OVER!!!";
			return score;
		}

	}
	fin.close();
	return score;
}

void about()
{
	fstream filein("about.txt", ios::in);
	char x[63];
	while (!filein.eof())
	{
		
		filein.getline(x,63,'\n');
		//filein >> x;
		cout << x <<'\n';
	}
}
This assertion might be raised due to a lack of the terminating '\0'.

How is the data stored in "qlist.qaf"?

srand() should appear only once at the top of main if you want [pseudo] random numbers.
Topic archived. No new replies allowed.