Quiz Program Problem?

Hello, this is my second program so far, and I've got everything working alright except for one small thing... When the user types 'help' when they start up the program, I'm expecting it to show the information I've written, but instead it doesn't do anything, and when you press a key it exits out of the program. My first program is a calculator, and it uses the same concept as the help 'function' in this program, the only difference is that the help information is not the same. The calculator runs perfectly fine. Here is the code:

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string start = "";

    int a;
    int s;

    cout << "Type 'Go' to begin the quiz, 'Help' for instructions, or 'Exit' to quit\n" << endl;
    cin >> start;
    cout << "\n" << endl;

    if (start == "exit")
    {
        cout << "Thanks for taking the quiz! Press Enter to exit\n";
        cin.ignore();
        cin.get();
        return 0;
    }

    if (start == "help")
    {
        cout << "All questions require a number as an answer. Some questions ask for a one-digit answer, "
             << "and others a two-digit number. An example of a question that requires a one-digit number "
             << "as an answer could be: 1 = What colour is grass. In this case, you would type '1' to choose "
             << "this answer. Here is an example of a possible answer of a question which requires a two-"
             << "digit number as an answer: 74. Notice that there is no '=' or words, but just a number, "
             << "unlike a question that asks for a one-digit number as an answer\n" << endl;
    }

    if (start == "go")
    {
        string q1 = "";

        cout << "Question 1:\nWhat is Effie Trinket's favourite quote?\n\n1 = 'Let the odds be in your favour'\n"
             << "2 = 'Shall the odds be ever in your favour'\n3 = 'May the odds be ever in your favour'\n4 ="
             << " 'May the odds last forever in your favour'\n" << endl;
        cin >> q1;
        cout << "\n" << endl;

        if (q1 == "3")
        {
            a = 0;
            a = a + 1;
        }

        if (q1 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q2 = "";
        cout << "Question 2:\nWhat district are Cato and Clove from?\n\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
             << "11\n12\n" << endl;
        cin >> q2;
        cout << "\n" << endl;

        if (q2 == "2")
        {
            a = a + 1;
        }

        if (q2 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q3 = "";
        cout << "Question 3:\nHow many tributes are chosen each year to participate in the Hunger Games?\n\n"
             << "2\n4\n6\n8\n10\n12\n14\n16\n18\n20\n22\n24\n26\n28\n30\n" << endl;
        cin >> q3;
        cout << "\n" << endl;

        if (q3 == "24")
        {
            a = a + 1;
        }

        if (q3 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q4 = "";
        cout << "Question 4:\nWho is the first tribute Katniss kills directly?\n\n1 = Marvel\n2 = Glimmer\n"
             << "3 = Cato\n4 = Clove\n5 = Foxface\n6 = Thresh\n7 = Rue\n" << endl;
        cin >> q4;
        cout << "\n" << endl;

        if (q4 == "1")
        {
            a = a + 1;
        }

        if (q4 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q5 = "";
        cout << "Question 5:\nWhat is Prim's cat's name?\n\n1 = Furball\n2 = Honeyball\n3 = Buttercup\n4 = Lady"
             << "\n5 = Butterball\n" << endl;
        cin >> q5;
        cout << "\n" << endl;

        if (q5 == "3")
        {
            a = a + 1;
        }

        if (q5 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q6 = "";
        cout << "Question 6:\nHow old was Katniss when she volunteered as tribute?\n\n12\n13\n14\n15\n16\n17\n"
             << "18\n" << endl;
        cin >> q6;
        cout << "\n" << endl;

        if (q6 == "16")
        {
            a = a + 1;
        }

        if (q6 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q7 = "";
        cout << "Question 7:\nWhat is Gale's hunting specialty?\n\n1 = Gun\n2 = Bow & Arrow\n3 = Snares\n4 = "
             << "Machete\n" << endl;
        cin >> q7;
        cout << "\n" << endl;

        if (q7 == "3")
        {
            a = a + 1;
        }

        if (q7 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q8 = "";
        cout << "Question 8:\nEvery how many years does a Quarter Quell take place?\n\n5\n10\n15\n20\n25\n30\n35\n"
             << "40\n45\n50\n55\n" << endl;
        cin >> q8;
        cout << "\n" << endl;

        if (q8 == "25")
        {
            a = a + 1;
        }

        if (q8 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q9 = "";
        cout << "Question 9:\nWhich Hunger Games did Haymitch win? Was it a Quarter Quell?\n\n1 = 34th, No\n2 = 34th"
             << ", Yes\n3 = 48th, No\n4 = 48th, Yes\n5 = 50th, Yes\n6 = 50th, No\n7 = 52nd, No\n8 = 52nd, Yes\n9 = 54"
             << "th, No\n" << endl;
        cin >> q9;
        cout << "\n" << endl;

        if (q9 == "5")
        {
            a = a + 1;
        }

        if (q9 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        string q10 = "";
        cout << "Question 10:\nWhat is Gale's sister's name?\n\n1 = Victoria\n2 = Lily\n3 = Posy\n4 = Emily\n" << endl;
        cin >> q10;
        cout << "\n" << endl;

        if (q10 == "3")
        {
            a = a + 1;
        }

        if (q10 == "exit")
        {
            cout << "Thanks for taking the quiz! Press Enter to exit\n";
            cin.ignore();
            cin.get();
            return 0;
        }
        s = a * 10;

        if (s == 100)
            cout << "Congratulations! You got 100% of the answers right!\n" << endl;

        else
            cout << "You answered " << s << "% of questions correct!\n" << endl;

        cin.ignore();
        cin.get();
        return 0;
    }
}
I tried your program and it works. If you type "help" not "Help".
I would suggest you make a recursive version of the program,
adding main(); before the 230th line where you wrote return 0;

Also, you better make a boolean start and not a string start, for the program to work better.
Try using switches instead of if else.
Last edited on
Hmm that's strange, because when I run it, when I type help, it immediately exits the program... Any idea why that is? I've been typing 'help' (lowercase letters) all this time, yet it still isn't working as expected. Also, how would I use boolean instead of string and switches instead of else or if?
I still need help everyone :(
Well, let's start with:

cout << start << '\n' << (start=="help") << endl;

What does it print?
Dude. Have you logically gone through your code?
If you type 'help' it will end the program because there is no loop.

What IDE are you using? Are you testing your file in a console/terminal or are you testing right out of the IDE? Are you using one of Microsoft's Visual Studio software? Do you know what happens when you run out of their IDE without the proper settings? It runs and exits your program, which exits the console because the program is finished.

Also, you have logical errors in your code.
Why is "return 0;" inside an if statement?
I guess my follow up question should be: If you wanted "return 0;" inside the if statement, where is the return for the main function if none of your if statements evaluate to true? You should be getting warnings about "not all paths return an integer" or something of the sort.
Last edited on
If you type 'help' it will end the program because there is no loop.

That's what ignore()/get() is for.

Why is "return 0;" inside an if statement?

To exit the program.

You should be getting warnings about "not all paths return an integer" or something of the sort.

No, that does not apply to the main function. It implicitly returns 0 when it reaches the end.
I got it fixed on my own, what I had to do was actually mess with the if (start == "go"); thing. Here is the part before I fixed it:

1
2
3
    if (start == "go")
    {
        string q1 = "";


Here is the part after it was fixed:

1
2
3
4
5
6
7
    if (start == "go")
    {

    }
    {

        string q1 = "";


It's actually really strange, and I don't really know how it works at all... O.o
I got this from my first program, a calculator, by the way.
to Athar. The ignore()/get() DOES NOT APPLY TO "HELP" because it is INSIDE an IF STATEMENT that does not EVALUATE HELP. Good going there bra.

The return 0; needs to be at the END of the program. Not inbetween if statements. Functions DO NOT LOOP on their own. The MAIN function is NOT A LOOP.

The warning DOES apply to the main function. Good going bro. I KNOW it implies a 0. That's because there are people called geniuses who build compilers for you to do that handling automatically. However, notice how I said something of the sort. If he's using a compiler that does not give warnings. Way to go! Not all compilers are that nice.

to doompickle. Good job dude. That is exactly what I was hinting to. Does your program work all better now?
Last edited on
The return 0; needs to be at the END of the program. Not inbetween if statements.

It has to be wherever you want to exit the function.

Functions DO NOT LOOP on their own. The MAIN function is NOT A LOOP.

It sounds like you're confusing return with continue. They have nothing to do with each other.

That's because there are people called geniuses who build compilers for you to do that handling automatically.

And that's because the C++ standard tells them so:

Standard wrote:
3.6.1.5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

Last edited on
I think everyone's confused. Just put the whole program into another function and loop that function, or loop the whole thing, but you should still put the return outside the loop so that the user won't exit the code immediately after answering questions. I would also suggest using else ifs instead of just ifs, and you can't use switches because they don't accept strings. You should use #include <algorithm> and transform(str1.begin(), str1.end(), str2.begin(), tolower) toupper also works where tolower is. This will allow the user to input a string with any combination of upper and lower case letters and still get the strings you are looking for. Also use getline(cin, str). This function gets rid of the enter character that any cin >> will not get rid of. Finally, and least importantly, make a zero outside of the first question's if statement, otherwise the user may get 367 questions right because they missed the first one. Line 47.

To make your life easier, look into arrays so that you don't have to have ten variables that mean the same thing. q1, q2, q3 . . .q1 =, q2 =, q3 = . . . turns into q[10], q[0] =, q[1] = , q[2] = . . ., but you can have a loop that does the numbers for you.
Thanks Vlykarye :) And yes, my program is working just fine now, i had to change one small thing
Lol. I'm sorry doompickle for adding to this argument on your post, but I don't want you to get confused with what this guy is saying.

GRex has the best idea here. Put your program into a function, and loop that function.

Return: exits a function and returns a value of a certain type.

Continue: has nothing to do with functions, so I don't know where he got that from. But continue is used in loops to skip the remainder of the code in a loop and start from the top of the loop.

Break: is what exits a loop.

Standard wrote:
3.6.1.5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;


Well duh! So why not just put a return 0; at the end of the main function so everyone knows you know what you're doing? The point of programming in a higher language than asm or machine code is for ease and readability. Don't go around leaving main functions without returns, especially if you have a return somewhere else in the code. That's just confusing for someone trying to debug. And they might take it as an error on the coder's part.
Take a look at this code. It is an example of what you could do to make your program more efficient and easier to work with.

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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <string>

using namespace std;

// new function to make lower case
string & makeLowerCase ( string & String );

int main ( )
{
    string catchGarbage; // This is used for exiting purposes.

    string userInput = ""; // changed "start" to "userInput

    const int NQS = 10;

    string userAnswers[NQS];
    string quizQuestions[NQS];
    string quizAnswers[NQS];

    // Question 1
    quizQuestions[0] =
            "Question 1:\n"
            "What is Effie Trinket's favourite quote?\n\n"
            "1 = 'Let the odds be in your favour'\n"
            "2 = 'Shall the odds be ever in your favour'\n"
            "3 = 'May the odds be ever in your favour'\n"
            "4 = 'May the odds last forever in your favour'\n";

    quizAnswers[0] = "3";

    // Question 2
    quizQuestions[1] =
            "Question 2:\n"
            "What district are Cato and Clove from?\n\n"
            "1\n"
            "2\n"
            "3\n"
            "4\n"
            "5\n"
            "6\n"
            "7\n"
            "8\n"
            "9\n"
            "10\n"
            "11\n"
            "12\n";

    quizAnswers[1] = "2";

    // Question 3
    quizQuestions[2] =
            "Question 3:\n"
            "How many tributes are chosen each year to participate in the"
            " Hunger Games?\n\n"
            "2\n"
            "4\n"
            "6\n"
            "8\n"
            "10\n"
            "12\n"
            "14\n"
            "16\n"
            "18\n"
            "20\n"
            "22\n"
            "24\n"
            "26\n"
            "28\n"
            "30\n";

    quizAnswers[2] = "24";

    // Question 4
    quizQuestions[3] =
            "Question 4:\n"
            "Who is the first tribute Katniss kills directly?\n\n"
            "1 = Marvel\n"
            "2 = Glimmer\n"
            "3 = Cato\n"
            "4 = Clove\n"
            "5 = Foxface\n"
            "6 = Thresh\n"
            "7 = Rue\n";

    quizAnswers[3] = "1";

    // Question 5
    quizQuestions[4] =
            "Question 5:\n"
            "What is Prim's cat's name?\n\n"
            "1 = Furball\n"
            "2 = Honeyball\n"
            "3 = Buttercup\n"
            "4 = Lady\n"
            "5 = Butterball\n";

    quizAnswers[4] = "3";

    // Question 6
    quizQuestions[5] =
            "Question 6:\n"
            "How old was Katniss when she volunteered as tribute?\n\n"
            "12\n"
            "13\n"
            "14\n"
            "15\n"
            "16\n"
            "17\n"
            "18\n";

    quizAnswers[5] = "16";

    // Question 7
    quizQuestions[6] =
            "Question 7:\n"
            "What is Gale's hunting specialty?\n\n"
            "1 = Gun\n"
            "2 = Bow & Arrow\n"
            "3 = Snares\n"
            "4 = Machete\n";

    quizAnswers[6] = "3";

    // Question 8
    quizQuestions[7] =
            "Question 8:\n"
            "Every how many years does a Quarter Quell take place?\n\n"
            "5\n"
            "10\n"
            "15\n"
            "20\n"
            "25\n"
            "30\n"
            "35\n"
            "40\n"
            "45\n"
            "50\n"
            "55\n";
    quizAnswers[7] = "25";

    // Question 9
    quizQuestions[8] =
            "Question 9:\n"
            "Which Hunger Games did Haymitch win? Was it a Quarter Quell?\n\n"
            "1 = 34th, No\n"
            "2 = 34th, Yes\n"
            "3 = 48th, No\n"
            "4 = 48th, Yes\n"
            "5 = 50th, Yes\n"
            "6 = 50th, No\n"
            "7 = 52nd, No\n"
            "8 = 52nd, Yes\n"
            "9 = 54th, No\n";

    quizAnswers[8] = "5";

    // Question 10
    quizQuestions[9] =
            "Question 10:\n"
            "What is Gale's sister's name?\n\n"
            "1 = Victoria\n"
            "2 = Lily\n"
            "3 = Posy\n"
            "4 = Emily\n";

    quizAnswers[9] = "3";

    // Other variables
    int a = 0; // set these right away to 0.
    int s = 0;

    cout << "Type 'Go' to begin the quiz, 'Help' for instructions, or 'Exit' to"
            "quit\n" << endl;
    cin >> userInput;
    cout << "\n" << endl;

    if ( makeLowerCase ( userInput ) == "exit" )
    {
        // Using our "makeLowerCase" function, we can change all the letters
        // to lowercase whenever we need.

        cout << "Thanks for taking the quiz! Press Enter to exit\n";
        //        cin.ignore ( );
        //        cin.get ( );
        getline ( cin, catchGarbage );
        // Using ignore and get is perfectly fine, and some people will argue
        // that it is the better way. I like to use getline.
        // It's up to your preference.

        //return 0;
        // This return is unnecessary, because no other if statement will
        // evaluate to true after this.
    }

    if ( makeLowerCase ( userInput ) == "help" )
    {
        cout << "All questions require a number as an answer. Some questions"
                " ask for a one-digit answer, "
                << "and others a two-digit number. An example of a question"
                " that requires a one-digit number "
                << "as an answer could be: 1 = What colour is grass. In this"
                " case, you would type '1' to choose "
                << "this answer. Here is an example of a possible answer of a"
                " question which requires a two-"
                << "digit number as an answer: 74. Notice that there is no '='"
                " or words, but just a number, "
                << "unlike a question that asks for a one-digit number as an"
                " answer\n" << endl;


        cout << "Type 'Go' to begin the quiz or 'Exit' to quit\n" << endl;
        cin >> userInput;
        cout << "\n" << endl;
    }

    if ( makeLowerCase ( userInput ) == "go" )
    {
        int currentQuestion;

        for ( currentQuestion = 0; currentQuestion < NQS; currentQuestion++ )
        {
            cout << quizQuestions[currentQuestion] << endl;
            cin >> userInput;
            cout << endl << endl;

            // check for "exit" right away.
            if ( makeLowerCase ( userInput ) == "exit" )
            {
                // now we can check if userInput is == "exit".
                cout << "Thanks for taking the quiz! Press Enter to exit\n";
                getline ( cin, catchGarbage );
                break;
            }

            userAnswers[currentQuestion] = userInput;
            if ( userAnswers[currentQuestion] == quizAnswers[currentQuestion] )
            {
                a++; // you can increment the score like this.
            }
        }

        // If all the questions were successfully asked and answered, display
        // the score. Otherwise, the program exits.
        if ( currentQuestion == NQS )
        {
            s = a * 10;

            if ( s == 100 )
                cout << "Congratulations! You got 100% of the answers right!"
                    "\n" << endl;

            else
                cout << "You answered " << s << "% of questions correct!\n"
                    << endl;
        }
    }

    return 0;
    // Like the other guy said, this isn't needed. However, there is now no
    // confusion as to where the program exits.
}

string & makeLowerCase ( string & String )
{
    // I used String as the parameter name because I don't know what else to
    // call it. You can change the name if you want. Here we need to loop
    // through String and make every letter lower case.

    for ( int index = 0; index < String.length ( ); index++ )
    {
        tolower ( String[index] );
    }

    // After all the letters are converted, return the modified version.
    return String;
}
Topic archived. No new replies allowed.