Exe error.

I am writing a simple program (more of a template for later programs, really) to quickly program quizzes to help me in school. There are no compiling errors, but when I open the cmd prompt exe, it says "Quiz.exe has stopped working." The code's too long to put in here, so I'll try to fit it in the replies.
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
#include <iostream>

using namespace std;

int question1;
int question2;
int question3;
int question4;
int question5;
int question6;
int question7;
int question8;
int question9;
int question10;
int question11;
int question12;
int question13;
int question14;
int question15;
int question16;
int question17;
int question18;
int question19;
int question20;

int totalquestions = 0;
int correctanswers = 0;
int incorrectanswers = 0;

int questions;

int percent = correctanswers/totalquestions*100;

int main()
{
    cout << "Question 1" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question1;
        if(question1 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 2" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question2;
        if(question2 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 3" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question3;
        if(question3 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 4" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question4;
        if(question4 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 5" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question5;
        if(question5 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 6" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question6;
        if(question6 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 7" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question7;
        if(question7 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 8" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question8;
        if(question8 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 9" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question9;
        if(question9 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 10" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question10;
        if(question10 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 11" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question11;
        if(question11 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 12" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer 4" << endl;
        cin >> question12;
        if(question12 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

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
    cout << "Question 13" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question13;
        if(question13 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 14" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question14;
        if(question14 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 15" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question15;
        if(question15 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 16" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question16;
        if(question16 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 17" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question17;
        if(question17 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 18" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question18;
        if(question18 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 19" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question19;
        if(question19 == 1){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << "Question 20" << endl;
        cout << "Answer 1" << endl;
        cout << "Answer 2" << endl;
        cout << "Answer 3" << endl;
        cout << "Answer4" << endl;
        cin >> question20;
        if(question1 == 20){
            cout << "You are correct!" << endl;
            correctanswers++;
        }else{
            cout << "You are incorrect!" << endl;
            incorrectanswers++;
        }
        questions++;

    cout << percent;

}
I got rid of all of the int questions, made it one int "answer" and put a bunch of switch statements instead of the ifs. But nothing's worked so far.
What compiler? Right off the bat though, I know you didn't put a return statement in main().


Another note, use arrays and functions, it would really clean up this code.
Last edited on
The default compiler with CodeBlocks, GNU I think? And I realized that and put the return statement with no luck. I've learned about arrays, but I've been struggling to actually put them in code and I tried using functions but that hasn't changed anything either.
I just compiled it, should've seen it earlier:

1
2
3
4
5
int totalquestions = 0;
int correctanswers = 0;
int incorrectanswers = 0;

int percent = correctanswers/totalquestions*100;


This is telling the program to do 0/0 * 100. Dividing by zero is not possible, and it fails. Try setting total questions to the actual number of questions (20).

About arrays, what exactly are you having problems with?
Last edited on
Just put it at the bottom, it works! Thank you! And I'm just not sure how they're useful, instead of just declaring separate variables.
Arrays would just help condense those 20 different names for questions into this:

int questions[20]; //declares an array of 20 int variables

It makes it much easier to read. But if you're fine with those variables, it's you're code, and you're the boss so it's up to you.

Also, what about those functions? You mean they just didn't serve a purpose to you or they actually weren't doing anything?
Last edited on
Oh, I just changed those into one variable, answer, that just changed with each question and user input. And the functions just didn't serve much of a purpose to me. I'm pretty new to programming, only been doing C++ for a few months, so I'm not too great at keeping it clean.
I am having trouble setting up the percent though. I'm trying to set it up like this:
1
2
    double percent = correctanswers/questions;
    cout << percent;

but it returns 0. I thought making it a double would solve that, but it's not doing anything. Any ideas?
Nope, got it. Just had to make correctanswers and questions doubles as well. Thank you for all of the help! I'd still be stuck on my original problem. It works like a charm now! :)
Good luck. Learning can be hard, but once you start working on a project it all falls into place.
Topic archived. No new replies allowed.