unhandled exception at 0x00EEFB5D in Project1.exe: 0xC0000094: Integer division by zero.

hello. been working on this code for a while now and had lots of help from the people here

The code runs normally up to the second question, however at this point gets an unhandled exception occurs and I have had no experience in dealing with these and do not know how to solve this as it does not tell me any errors in the error list.

ideally it would not be duplicated code 5 times, however because I want to pull from different files I am unsure how to do this in another way

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(nullptr)));

	//introduction

	cout << "The epic quiz.\n\n\n";
	cout << "The objective of this round is to unscramble the coded word, you will be given the category as the clue and you have to type out what you belive the answer to be, with a capital letter\n\n";

	string Name;

	cout << "But First, Enter your first name: ";
	getline(cin, Name);

	cout << "Welcome contestant " << Name << ". Are you ready to begin the quiz? ";
	cout << "please type Yes or No (case sensitive): ";

	string respond;

	cin >> respond;
	if (respond == "Yes")
		cout << "\nGood luck!";
	else {
		cout << "Maybe next time!\n";
		return 0;
	}


	//question 1


	cout << "The first category is...\n";
	cout << "Premier League Football Teams!\n";

	vector<string> lines;
	ifstream file("Premier_League_Teams.txt");

	if (!file)
		return (std::cout << "Cannot open file\n"), 1;

	for (string line; getline(file, line); lines.push_back(line));

	const auto random_number{ rand() % lines.size() };
	const auto word1{ lines[random_number] };
	const auto codes1{ new int[word1.length()] {} };

	for (size_t i = 0; i < word1.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word1[i]))) {
			codes1[i] = std::tolower(static_cast<unsigned char>(word1[i])) - 'a' + 1;
			cout << codes1[i] << ' ';
		}

	delete[] codes1;
	string answer1;
	cout << "\nPlease input your answer (as one word no spaces): ";
	cin >> answer1;
	
	if (answer1 == word1)
		cout << "Correct!\n";
	else 
		cout << "Incorrect :(\n";


//question 2


	cout << "The second category is...\n";
	cout << "Premier League Football Teams!\n";
	
	vector<string> lines2;
	for (string line; getline(file, line); lines2.push_back(line));

	const auto random_number2{rand() % lines2.size() };
	const auto word2{lines2[random_number2] };
	const auto codes2{new int[word2.length()] {} };

	for (size_t i = 0; i < word2.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word2[i]))) {
			codes2[i] = std::tolower(static_cast<unsigned char>(word2[i])) - 'a' + 1;
			cout << codes2[i] << ' ';
		}

	delete[] codes2;
	string answer2;
	cout << "\nPlease input your answer (as one word no spaces): ";
	cin >> answer2;

	if (answer2 == word2)
		cout << "Correct!\n";
	else
		cout << "Incorrect :(\n";


//question 3

	cout << "The third category is...\n";
	cout << "Hobbit Characters!\n";

	vector<string> lines3;
	ifstream file2("Hobbit_Characters");

	if (!file)
		return (std::cout << "Cannot open file\n"), 1;


	for (string line; getline(file2, line); lines3.push_back(line));

	const auto random_number3{rand() % lines3.size() };
	const auto word3{lines3[random_number3] };
	const auto codes3{new int[word3.length()] {} };

	for (size_t i = 0; i < word3.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word3[i]))) {
			codes3[i] = std::tolower(static_cast<unsigned char>(word3[i])) - 'a' + 1;
			cout << codes3[i] << ' ';
		}

	delete[] codes3;
	string answer3;
	cout << "\nPlease input your answer (as one word no spaces): ";
	cin >> answer3;

	if (answer3 == word3)
		cout << "Correct!\n";
	else
		cout << "Incorrect :(\n";


//question 4


	cout << "The fourth category is...\n";
	cout << "Hobbit Characters!\n";

	vector<string> lines4;
	for (string line; getline(file, line); lines4.push_back(line));

	const auto random_number4{rand() % lines4.size() };
	const auto word4{lines4[random_number4] };
	const auto codes4{new int[word4.length()] {} };

	for (size_t i = 0; i < word4.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word4[i]))) {
			codes4[i] = std::tolower(static_cast<unsigned char>(word4[i])) - 'a' + 1;
			cout << codes4[i] << ' ';
		}

	delete[] codes4;
	string answer4;
	cout << "\nPlease input your answer (as one word no spaces): ";
	cin >> answer4;

	if (answer4 == word4)
		cout << "Correct!\n";
	else
		cout << "Incorrect :(\n";


//question 5
	
	cout << "The final category is...\n";
	cout << "South American Countries!\n";

	vector<string> lines5;
	ifstream file5("South_American_countries");

	if (!file)
		return (std::cout << "Cannot open file\n"), 1;

	for (string line; getline(file, line); lines5.push_back(line));

	const auto random_number5{rand() % lines5.size() };
	const auto word5{lines[random_number5] };
	const auto codes5{new int[word5.length()] {} };

	for (size_t i = 0; i < word5.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word5[i]))) {
			codes5[i] = std::tolower(static_cast<unsigned char>(word5[i])) - 'a' + 1;
			cout << codes5[i] << ' ';
		}

	delete[] codes5;
	string answer5;
	cout << "\nPlease input your answer (as one word no spaces): ";
	cin >> answer5;

	if (answer5 == word5)
		cout << "Correct!\n";
	else
		cout << "Incorrect :(\n";
}
You're using codes4 as a null terminated string, without null terminating it.
 
const auto codes4{new int[word4.length()] {} };


Maybe something like:
1
2
const auto codes4{new int[word4.length() + 1] {} };
codes[word4.length()] = '\0';


But you could define codes4 as:
 
std::string code4{word4};


Same for word5
this seems to have helped, but still getting the unhandled exception on question 2 before it loads the encrypted word
same for word2:)
didnt work. still getting same exception :(
L80. file is at eof - so no more data can be read! lines2 is hence empty with a size of 0. Hence ...boooom!

before L80 either you need to open a new file or reset the existing to read again.
how do i open a new file without having it conflict? when I have it open the file normally It gives me like 14 errors haha
Maybe use a function for the question part of the quiz? Perhaps something like:

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;

void question(const std::string& fname)
{
	vector<string> lines;
	ifstream file(fname);

	if (!file) {
		std::cout << "Cannot open file " << fname << '\n';
		return;
	}

	for (string line; getline(file, line); lines.push_back(line));

	const auto random_number {rand() % lines.size()};
	const auto word1 {lines[random_number]};
	const auto codes1 {new int[word1.length()] {}};

	for (size_t i = 0; i < word1.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word1[i]))) {
			codes1[i] = std::tolower(static_cast<unsigned char>(word1[i])) - 'a' + 1;
			cout << codes1[i] << ' ';
		}

	delete[] codes1;

	string answer1;

	cout << "\nPlease input your answer (as one word no spaces): ";
	cin >> answer1;

	if (answer1 == word1)
		cout << "Correct!\n";
	else
		cout << "Incorrect :(\n";
}

struct Quiz {
	string fname;
	string title;
	string catnum;
};

int main()
{
	const Quiz qz[] {{"Premier_League_Teams.txt", "Premier League Football Teams!", "first"},
		{"c.txt", "C language", "second"},
		{"Hobbit_Characters.txt", "Hobbit Characters", "third"},
		{"South_American_Countries.txt", "South American Countries", "final"}};

	srand(static_cast<unsigned int>(time(nullptr)));

	cout << "The epic quiz.\n\n\n";
	cout << "The objective of this round is to unscramble the coded word.\n";
	cout << "You will be given the category as the clue\n";
	cout << "and you have to type out what you believe the answer to be, with a capital letter\n\n";

	string Name;

	cout << "But First, Enter your first name: ";
	getline(cin, Name);

	cout << "\nWelcome contestant " << Name << ". Are you ready to begin the quiz?\n";
	cout << "Please type Yes or No (case sensitive): ";

	string respond;

	cin >> respond;

	if (respond != "Yes") {
		cout << "Maybe next time!\n";
		return 0;
	}

	cout << "Good luck!\n";

	for (const auto& [fnam, title, cat] : qz) {
		cout << "The " << cat << " category is...\n" << title << '\n';
		question(fnam);
	}
}

thats a brilliant idea! would i be able to integrate a scoring system using this method?
also is their a difference between Fnam and fname
fnam is what I used for the name in the structured binding on L84. Any names can be used here. I used a different name to highlight this.

fname is the name used in the struct L46
i keep getting undefined errors on lines 84 and 85
1
2
3
4
	for (const auto& quiz : qz) {
		cout << "The " << quiz.catnum << " category is...\n" << quiz.title << '\n';
		question(quiz.fname);
	}
perfect. only error now is that it says the answer is incorrect regardless haha
Debug the code and break before line 39 [in seeplus's post] and examine the difference between answer1 and word1. Work backwards to when things first start to diverge from what you expect those values to be.
Last edited on
If you're getting errors on my L84/85 then you're not compiling as C++17 - which you need to for my code.

Is the data in the file what you'd expect? Is there a header line - which isn't taken into account in this code??

After the vector has been filled from the file, display it and confirm it's right.

thats a brilliant idea!


Once you're got this working, it can be expanded more generally. Instead of having the file names/titles specified in the code as array qz, you could have a control file called say quiz.txt that contains a list of files to use. Each file would then have the first line as the description. You'd iterate the quiz.txt file to find the names of the file to use and then get the category name from the file. The position names would then be in an array called say position containing "first", "second", "third" etc.

Then to change the quiz, all you (or anyone else) has to do is to edit files. No compilation of the program needed!
after ive got it working like this I wanna do some stuff with boost which will likely incorporate the files into one and allow me to write to them as well if I wish. it compiles at the moment however it is still saying that every input (correct or otherwise) is incorrect. not sure why this is happening as I cant find anywhere that this would be occurring, here is the code in the form I have it currently


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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;

void question(const std::string& fname)
{
	vector<string> lines;
	ifstream file(fname);

	if (!file) {
		std::cout << "Cannot open file " << fname << '\n';
		return;
	}

	for (string line; getline(file, line); lines.push_back(line));

	const auto random_number{ rand() % lines.size() };
	const auto word1{ lines[random_number] };
	const auto codes1{ new int[word1.length()] {} };

	for (size_t i = 0; i < word1.length(); ++i)
		if (isalpha(static_cast<unsigned char>(word1[i]))) {
			codes1[i] = std::tolower(static_cast<unsigned char>(word1[i])) - 'a' + 1;
			cout << codes1[i] << ' ';
		}


	string answer1;

	cout << "\nPlease input your answer (as one word no spaces): ";
	cin >> answer1;

	if (answer1 == word1)
		cout << "Correct!\n";
	else
		cout << "Incorrect :(\n";
}

struct Quiz {
	string fname;
	string title;
	string catnum;
};

int main()
{
	const Quiz qz[]{ {"Premier_League_Teams.txt", "Premier League Football Teams!", "first"},
		{"Premier_League_Teams.txt", "Premier League Football Teams!", "second"},
		{"Hobbit_Characters.txt", "Hobbit Characters", "third"},
		{"Hobbit_Characters.txt", "Hobbit Characters", "fourth"},
		{"South_American_Countries.txt", "South American Countries", "final"} };

	srand(static_cast<unsigned int>(time(nullptr)));

	cout << "epic quiz.\n\n\n";
	cout << "The objective of this round is to unscramble the coded word.\n";
	cout << "You will be given the category as the clue\n";
	cout << "and you have to type out what you believe the answer to be, with a capital letter\n\n";

	string Name;

	cout << "But First, Enter your first name: ";
	getline(cin, Name);

	cout << "\nWelcome contestant " << Name << ". Are you ready to begin the quiz?\n";
	cout << "Please type Yes or No (case sensitive): ";

	string respond;

	cin >> respond;

	if (respond != "Yes") {
		cout << "Maybe next time!\n";
		return 0;
	}

	cout << "Good luck!\n";

	for (const auto& quiz : qz) {
		cout << "The " << quiz.catnum << " category is...\n" << quiz.title << '\n';
		question(quiz.fname);
	}
}
Last edited on
L37 insert

 
std::cout << "!" << word1 << "!  !" << answer1 << "!\n";


For a correct answer, these should be the same within the !. Are they?

But now is the time to get to grips with the debugger. Put a break point on L 38 and see what are the values.
Last edited on
Topic archived. No new replies allowed.