Random Quiz Questions

Pages: 12
Hi, I'm fairly new to C++, and an thinking of creating a simple program to help me study. The user would select topics to chose from, and then receive questions. I'm stuck on how to (listed in priority): 1) Create a way to get to different topics by typing them in (i.e. Type in "Geometry" for the whole set of chapters on geometry (and maybe an "overall" choice)), and then type in the chapter (again, probably have an "overall" choice too), and then the section in that chapter. And 2) have the program chose, say, 20 random questions from that topic. I want it to be random, otherwise I could just memorize the answers. I don't need specific source code, just how to do it (i.e. what operator/function/whatever it is that I need to do it). For 1), I have no idea what I could use to get that to work, but 2) is probably some kind of variation on a loop (I'm guessing)
Last edited on
What part are you having trouble with? Do you have any code?
I'm having trouble with how to get the user to access the questions (i.e. If I were to type in Geometry, I'd get geometry questions).

I also don't know how to get the program to randomly select x amount of questions out of, say, a total of 100 or so (i.e. I type in the amount of questions that I want, and it randomly chooses which questions to use).

I do not have any code so far only because the rest of the code would just be the actual "question" being stated and the answer marked as "right" or "wrong" (using a conditional operator :p). As I said, I'm really new, but if someone could explain which function to use and how, I think that'd be enough
There is no function to do what you describe, there are multiple functions that you can use to create the program to do what you want, but you're not really going to get much help here if you're going to just say (the equivalent of) "Write my program for me", which is what it seems like you are doing. The lack of postings here would seem to indicate that most other users here either agree with me, or think your question is off-topic, or both.

This is why I asked to see some code, because you're not going to get any help here unless people feel that YOU are putting effort into you're project.

If you're new to c++, you may want to set your sights a little lower and look at some c++ tutorials, and write some very small programs first before you tackle a project like this.

If you're set on writing this quiz program, then I suggest you google rand() and srand(), after that you could could work on your user interface so you have some code. Eventually you're going to need to look up i/o streams, focusing on file access.

Next time you post, show what research you've done, and a little code wouldn't hurt either.

I'm not trying to be mean or snotty, just helpful.

Remember, the more specific your questions are, the more people will be inclined to help.

__Edit__

Actually, look around at some of the other posts and see what the one's with few replies have in common, and what the ones with at least 4 or more replies have in common.
Last edited on
Actually, I wasn't looking for the exact code, just something similar to the 4th paragraph of your post. But I do get at what you're saying, sorry for being a newfag :P

[EDIT]

As for code, it basically looks like this:


#include <iostream>

using namespace std;

int main ()

cout << "What is the square route of 144?" << endl;

int b, iNumber;

cin >> iNumber;

iNumber=b;

b==12 ? 1 : 0;

cout << b;

cout << "Next question!" << endl;


(1 is "Correct!" and 0 is "Wrong!", but I bet you probably figured that out)
I'm also having trouble with this, when I get the console to appear (whatever the Ctr+F5 thing does (again I know I'm a newfag :P)) I get a message saying:

Debug Error!

Program: ...s\visual studio 2010\Projects\Tutorial 1\Debug\Tutorial 2.exe
Module: ...s\visual studio 2010\Projects\Tutorial 1\Debug\Tutorial 2.exe
File:

Run-Time Check Failure #3 - The variable 'b' is being used without being initialized.

(Press Retry to debug the application)

I would love to know what I'm doing wrong.
Last edited on
closed account (D80DSL3A)
Your use of the conditional operator is flawed. I think the following will do what you have in mind:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;

int main()
{
	cout << "What is the square route of 144?" << endl;
	int b, iNumber;
	cin >> iNumber;

	b = iNumber==12 ? 1 : 0;// b is assigned 1 or 0 here. Users answer was stored in iNumber.
	cout << b;
	cout << " Next question!" << endl;
	
	cout << endl;
	return 0;
}

Hope this helps you to get your ambitious project off the ground!

P.S. This in place of cout << b; may add some flair:
1
2
3
4
if( b )
	cout << "Correct!";
else
	cout << "Wrong!";
Oh okay! Thanks a lot!
;)
closed account (D80DSL3A)
You're welcome. Here's an idea for the programs basic framework. Functions are used to split up the tasks and keep the main() simple.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// function prototypes
int mainMenu(void);// display main menu and get topic choice as a selection #
int subMenu(int menuNumber);// use a switch statement to call GeometryMenu(), etc.
int takeQuiz(int mainChoice, int subChoice );// administers quiz and returns the score

int GeometryMenu(void);// these would be similar to mainMenu()
int HistoryMenu(void);
int GeographyMenu(void);

//** main function **
int main()
{	
	int mainChoice = mainMenu();
	int subChoice  = subMenu( mainChoice );
	int quizScore = takeQuiz( mainChoice, subChoice );// this would branch out to other functions
	
	cout << "You scored " << quizScore << " points." << endl;
	return 0;
}// end of main() 


Here's an example of what the xyzMenu() functions could be like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int mainMenu(void)
{
	int choice = 0;

	cout << "    *** Main Menu ***" << endl << endl;
	cout << "Please choose a topic below" << endl;
	cout << "---------------------------" << endl;
	cout << "      1. Geometry" << endl;
	cout << "      2. History" << endl;
	cout << "      3. Geography" << endl;
	cout << "     --------------" << endl;
	cout << "Your choice? (1-3): ";
	cin >> choice;// NOTE: No input validation here yet. 1st version = simple!

	return choice;
}// end of mainMenu() 


Hope this seems sensible. Good luck.
Thanks a lot! Should I just put these two blocks in the way your ordered them or should it just be all one block? Also, is it possible to create something like:

int takeQuiz (int mainChoice, int subChoice, int subsubChoice)

or should it just be:

int takeQuiz (int mainChoice, int subChoice, int subChoice)

And how do you do that box thing?

Again, thanks a lot for helping me with this, you'll all definitely be on the credits page :)
Last edited on
closed account (D80DSL3A)
You're welcome. Enter the code as 1 "block" as if I hadn't split them.
I split the code up because the code for mainMenu() is meant to serve as an example for the rest of the menu functions. You would need to write definitions for GeometryMenu(), HistoryMenu(), GeographyMenu(), subMenu() and takeQuiz().

BTW thank YOU for the project idea. I'm using it to learn about STL containers (vectors and strings) for myself. My version of this is now quite different from what I've posted here.

Are you familiar with writing and using functions? Those skills are needed here.

What would a subsubChoice be? A further subject split?
EG: Suppose the mainChoice was 1 (Geometry), causing the function subMenu() to call:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int GeometryMenu(void)
{
	int choice = 0;

	cout << "*** Geometry Menu ***" << endl << endl;
	cout << "Please choose a topic below" << endl;
	cout << "---------------------------" << endl;
	cout << "      1. Circles" << endl;
	cout << "      2. Squares" << endl;
	cout << "      3. Lines" << endl;
	cout << "      4. Planes" << endl;
	cout << "     --------------" << endl;
	cout << "Your choice? (1-4): ";
	cin >> choice;

	return choice;
}// end of GeometryMenu() 

Then suppose that subChoice = 2 (Squares) was made. Do you want another subsubMenu to appear listing topics on squares? I hope not! The project will be plenty challenging at just this mainChoice + subChoice level of complexity.

I'm not sure what you mean by "that Box Thing".

Since this project of yours appears to not be a homework problem I could simply give you the code for all of the functions that are prototyped above main(). I wrote them to make sure the basic program idea would work. Doing this may rob you of a chance to learn by working it out yourself though.
I'll post the whole thing if you wish.
Oh I see, thanks for explaining. I wanted to use subsubChoice for sections in chapters (ex. Geometry>Chapter 3>Section 2) but now that I think about it, it seems to be a little overkill.

By that box thing I mean the box with line numbers and code that you posted.

I don't want all the code as I am trying to learn C++ on my own time (since my school is so small, there aren't any classes I can take that involve programming >.<). I may seem like a slow learner, but after a few examples, I grasp the concept quickly.

After reading about rand() and srand(), I think I know how I can get the random order to work, but it seems a bit complex. I was planning on having it select either 0 or 1, and then put:

if( 0 )

cout << "......"
cin >> iNumber (or use getline if its a word/sentence answer, but same idea)
b = iNumber==12 ? 1 : 0;
if ( b )
cout << "Correct!";
else
cout << "Wrong!";

cout << " Next question!" << endl;


else
cout << "......"

and so on, but as I mentioned, it seemed a bit complex for only getting two different lists of questions. Or I could have it choose a random number after each question, but that makes it even more complex.

[EDIT]
I don't know much about functions, other than they allow you to perform certain actions. Like how double lets you input/show a number with 3 decimal places. Or something along those lines.

[EDIT][EDIT]
You're probably going to hate me for asking this, but, what do you mean by writing definitions? *hides under bed*

If it's this, then I already did it:

int mainMenu(void);
int subMenu(int menuNumber);
int takeQuiz(int mainChoice, int subChoice );


int GeometryMenu(void);
int HistoryMenu(void);
int GeographyMenu(void);

[EDIT][EDIT][EDIT]
Also, should each menu look exactly like the menus that you posted? Each with its own
"return choice" ? Or should there only be one "return choice" and after it "return 0" ?
Last edited on
closed account (D80DSL3A)
Easy one first: "that Box thing" comes from using code tags. When you are entering a post you will see Format: to the right. Click on the symbol below that looks like <>. This will insert "code tags" into your post. Insert your code between these 2 tags. You can experiment with the format options. Click on "Preview" below to see what your post will look like.

About functions. This is a function prototype:
int subMenu(int menuNumber);
It allows the compiler to recognize the function call in your code, but it doesn't say anything about what the function actually does.
This is the corresponding function definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int subMenu(int menuNumber)
{
	int choice = 0;

	cout << "\n\n\n\n";//to put some space between the menus
	switch( menuNumber )
	{
	case 1:
		choice = GeometryMenu();
		break;
	case 2:
		choice = HistoryMenu();
		break;
	case 3:
		choice = GeographyMenu();
		break;
	default:
		break;
	}

	return choice;
}// end of subMenu() 

This includes the exact instructions which the function will execute.

Lastly, re:
should each menu look exactly like the menus that you posted? Each with its own
"return choice" ?

Yes. The job of each of the Menu() functions is to display a menu then get (and return) the users menu selection (as choice). These functions are identical aside from the particular messages displayed to the user.
I see. So I would need to write a definition similar to this for int mainMenu (void) and int takeQuiz (int mainChoice, int subChoice ) as well? And each of these definitions should be in their own block as well, right?

Should I put a return 0 at the very end as well?

Should I change the variable choice for each menu and its corresponding definition?

Lastly, (this is more of a general question really) could I put, say, the definition for a menu in a header file, and then write #include "subMenuDef.h" or would it just not work?

Again, thanks so much. I think that with this, I don't think I will have any more problems. Maybe.

[EDIT]
For one of the sub menus (i.e. The geometry menu), should I use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int GeometryMenu (int mainChoice, int subChoice )
{
       int choice = 0;
       
       cout << "/n/n/n/n";
       switch (takeQuiz)
       {
        case 1:
             choice = Chapter1 ();
             break;

        case 2:
             choice = Chapter2 ();
             break;
        }
return choice
}


Because I don't know what to put in the () after switch. Or should I just leave it blank?

If you need to see it to be sure, I'll post/pm my current code.
Last edited on
closed account (D80DSL3A)
Hello again. I'm going to post the whole thing for you.
This will give you something that works so you can examine it and tinker with it.
This code only handles displaying the menus and collecting values for mainChoice and subChoice.
The really hard part remains though. That will be writing the function takeQuiz().
You will need to figure out how you want to do it.
Also, yes you can put the function definitions in a header file - but a few new details apply.

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
// quizShow.cpp

#include <iostream>
using namespace std;

// function prototypes
int mainMenu(void);// display main menu and get topic choice as a selection #
int subMenu(int menuNumber);// use a switch statement to call GeometryMenu(), etc.
int takeQuiz(int mainChoice, int subChoice );// administers quiz and returns the score

int GeometryMenu(void);
int HistoryMenu(void);
int GeographyMenu(void);

//** main function **
int main()
{	
	int mainChoice = mainMenu();
	int subChoice  = subMenu( mainChoice );
	int quizScore = takeQuiz( mainChoice, subChoice );// this would branch out to other functions

	cout << "mainChoice = " << mainChoice << endl;// just to verify that the correct values were taken
	cout << "subChoice = " << subChoice << endl;
	
	cout << "You scored " << quizScore << " points." << endl;
	return 0;
}// end of main()

// function definitions
int mainMenu(void)
{
	int choice = 0;

	cout << "    *** Main Menu ***" << endl << endl;
	cout << "Please choose a topic below" << endl;
	cout << "---------------------------" << endl;
	cout << "      1. Geometry" << endl;
	cout << "      2. History" << endl;
	cout << "      3. Geography" << endl;
	cout << "     --------------" << endl;
	cout << "Your choice? (1-3): ";
	cin >> choice;// NOTE: No input validation here yet. 1st version = simple!

	return choice;
}// end of mainMenu()

int subMenu(int menuNumber)
{
	int choice = 0;

	cout << "\n\n\n\n";
	switch( menuNumber )
	{
	case 1:
		choice = GeometryMenu();
		break;
	case 2:
		choice = HistoryMenu();
		break;
	case 3:
		choice = GeographyMenu();
		break;
	default:
		break;
	}

	return choice;
}// end of subMenu()

int GeometryMenu(void)
{
	int choice = 0;

	cout << "*** Geometry Menu ***" << endl << endl;
	cout << "Please choose a topic below" << endl;
	cout << "---------------------------" << endl;
	cout << "      1. Circles" << endl;
	cout << "      2. Squares" << endl;
	cout << "      3. Lines" << endl;
	cout << "      4. Planes" << endl;
	cout << "     --------------" << endl;
	cout << "Your choice? (1-4): ";
	cin >> choice;

	return choice;
}// end of GeometryMenu()

int HistoryMenu(void)
{
	int choice = 0;

	cout << "   *** History Menu ***" << endl << endl;
	cout << "Please choose a topic below" << endl;
	cout << "---------------------------" << endl;
	cout << "      1. Ancient" << endl;
	cout << "      2. 18th century" << endl;
	cout << "      3. 19th century" << endl;
	cout << "      4. United States" << endl;
	cout << "     --------------" << endl;
	cout << "Your choice? (1-4): ";
	cin >> choice;

	return choice;
}// end of HistoryMenu()

int GeographyMenu(void)
{
	int choice = 0;

	cout << " *** Geography Menu ***" << endl << endl;
	cout << "Please choose a topic below" << endl;
	cout << "---------------------------" << endl;
	cout << "      1. Europe" << endl;
	cout << "      2. Africa" << endl;
	cout << "      3. Asia" << endl;
	cout << "     --------------" << endl;
	cout << "Your choice? (1-3): ";
	cin >> choice;

	return choice;
}// end of GeographyMenu()

// Here's the hard part. Writing this function!!
int takeQuiz(int mainChoice, int subChoice )
{
	int score = 0;
	score = mainChoice + subChoice;// temp for testing
	// TODO: open file for specific quiz. Fill arrays with questions and answers, etc...
	return score;
}
I see. I basically had this:
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
#include "stdafx.h"
//I have <iostream>, <string>, and <sstream> in that header
using namespace std;

int mainMenu (void);
int subMenu (int menuNumber)
{
	int choice = 0;

	cout << "\n\n\n\n";
	switch( menuNumber )
	{
	case 1:
		choice = FrenchMenu();
		break;
	case 2:
		choice = ChemistryMenu();
		break;
	case 3:
		choice = BeliefSystemsMenu();
		break;
        case 4:
                choice = EnglishMenu();
                break;
        case 5:
                choice = GeometryMenu();
                break;
        case 6:
                choice = GlobalMenu();
                break;
	default:
		break;
	}

	return choice;
}
int takeQuiz (int mainChoice, int subChoice );

int FrenchMenu (void);
int ChemistryMenu (void);
int BeliefSystemsMenu (void);
int EnglishMenu (void);
int GeometryMenu (void);
int GlobalMenu (void);

int main ()
{
	int mainChoice = mainMenu();
	int subChoice = subMenu ( mainChoice );
	int quizScore = takeQuiz ( mainChoice, subChoice );

	cout << "You scored " << quizScore << " points." << endl;
	return 0;
}
	
int mainMenu (void)
{
	int choice = 0;

	cout << "	*** Main Menu ***" << endl << endl;
	cout << "Please choose a topic below" << endl;
	cout << "---------------------------" << endl;
	cout << "	1. French" << endl;
	cout << "	2. Chemistry" << endl;
	cout << "	3. Belief Systems" << endl;
	cout << "	4. English" << endl;
	cout << "	5. Geometry" << endl;
	cout << "	6. Global" << endl;
	cout << "	------------------" << endl;
	cout << " Your choice? (1-6): ";
	cin >> choice;

	return choice;


	if ( 1 )

		int FrenchMenu (void);
			int choice = 0;
	
			cout << "	*** French Menu ***" << endl << endl;
			cout << "Please choose a topic below" << endl;
			cout << "-----------------------------" << endl;
			cout << "	1.1. Topic 1: Salade- Le Proces" << endl;
			cout << "	1.2. Topic 2: Les Trains, L'avion, Le futur" << endl;
			cout << "	-----------------------------------------" << endl;
			cout << "	Your choice? (1.1-2.1)" << endl;
			cin >> choice;

			return choice;

				if ( 1.1 )


	if ( 2 )

		int ChemistryMenu (void);
			int choice = 0;

			cout << "	*** Chemistry Menu ***" << endl << endl;
			cout << "	Please choose a topic below" << endl;
			cout << "	---------------------------" << endl;
			cout << "	" << endl;
			cout << "	Your choice? ( - )" << endl;
			cin >> choice;

			return choice;

	if ( 3 )
		int BeliefSystemsMenu (void);
			int choice = 0;

			cout << "*** Belief Systems Menu ***" << endl << endl;
			cout << "Please choose a topic below" << endl;
			cout << "---------------------------" << endl;
			cout << "	1.1. Ancient Egyptian" << endl;
			cout << "	2.1. Zorostrianism" << endl;
			cout << "	3.1. Greek" << endl;
			cout << "	4.1. Norse" << endl;
			cout << "	5.1. Hinduism" << endl;
			cout << "	6.1. Buddhism" << endl;
			cout << "	7.1. Chinese Philosophies" << endl;
			cout << "	8.1. Judaism" << endl;
			cout << "	-----------------------" << endl;
			cout << "	Your choice? (1.1-8.1)" << endl;
			cin >> choice;

			return choice;
	
	if ( 4 )
		int EnglishMenu (void);
			int choice = 0;

			cout << "	*** English Menu***" << endl << endl;
			cout << "Please choose a topic below" << endl;
			cout << "---------------------------" << endl;
			cout << "	1.1. To Kill a Mockingbird" << endl;
			cout << "	2.1. Lord of the Flies" << endl;
			cout << "	------------------------" << endl;
			cout << "Your choice? (1.1-2.1)" << endl;
			cin >> choice;

			return choice;

	if ( 5 )
		int GeometryMenu (void);
			int choice = 0;

			cout << "	*** Geometry Menu***" << endl << endl;
			cout << "Please choose a topic below" << endl;
			cout << "---------------------------" << endl;
			cout << "	1.1. Chapter 1" << endl;
			cout << "	2.1. Chapter 2" << endl;
			cout << "	3.1. Chapter 3" << endl;
			cout << "	4.1. Chapter 4" << endl;
			cout << "	------------" << endl;
			cout << "Your choice? (1.1-4.1)" << endl;
			cin >> choice;

			return choice;

	if ( 6 )
		int GlobalMenu (void);
			int choice = 0;

			cout << "	*** Global Menu***" << endl << endl;
			cout << "Please choose a topic below" << endl;
			cout << "---------------------------" << endl;
			cout << "	1.1. Periodization 1450" << endl;
			cout << "	2.1. 1450-1750 (Enlightenment)" << endl;
			cout << "	3.1. French Revolution" << endl;
			cout << "	4.1. Latin American Revolutions" << endl;
			cout << "	-----------------------------" << endl;
			cout << "Your choice? (1.1-4.1)" << endl;
			cin >> choice;

			return choice;

	return 0;
}


I used if instead of switch/case. Where you see if ( 1.1 ) is where I would have put the questions. But I guess you need to use case.

Where you put
// TODO: open file for specific quiz. Fill arrays with questions and answers, etc...
, that's where I should put the questions using #include right? And I should indicate which one to open using:
1
2
if ( 2 ) //Geometry>Circles
    #include "Geometry>Circles.h" 

right?

When this works, I think I'll take a break from my C++ adventure and try VBASIC instead, and then come back.

Again, thanks for the immense help that you've given me. I'm sure you're tired of correcting my errors. And somehow, I will put a credits page on here at the end, and you'll definitely be on it.

[COMMENTARY]
If you're wondering why I had Chemistry blank, its because I left my notebook and folder at school, and did not remember what we had been tested on. :P

If the question numbers don't look right, it's because I was editing, then I left, and then I saw your post.
Last edited on
closed account (D80DSL3A)
You put a lot of effort into your code there. Have you tried compiling it?

I think some formal study would be best for you at this point. Sheer force of will won't make a program work. You must understand what you are doing.
Find a good book on c++ programming for beginners and study through it as you do with your college textbooks.
Start at chapter 1 and study each chapter in sequence, working through all examples and exercises as you go. You will get through some parts fast because you already know the material but don't skip anything. Programming is technical. You will learn it very well this way.

I'm currently studying from Beginning Visual C++ 2008 by Ivor Horton. I have no problem myself with studying a "beginners" book. There's a LOT in it that I don't know. I chose this book because I like the authors style and it is written specifically for the IDE I am using.
You could also try the tutorials at this site. There are some pretty good ones here too: http://www.cprogramming.com/

If you decide to press forward with your project then try creating it in pieces. Don't try to write the whole thing in one shot. eg: Get a single menu working then add a bit more to your working program and so on, building up the functionality as you go.
The complete code I posted last time will compile and run. Experiment around with it. You could change one piece at a time to see what happens. Let me know when you get a basic version running!
I've tested it and it works! Thanks again for all of your help, and for future reference, I'm actually in high school xD

I'll definitely look into buying a book the next time I hit Barnes & Nobles (the single bookstore nearby, since the other one at a nearby mall had to close about 6 months ago >.<) and if I don't find one there, I'll probably go online.
closed account (D80DSL3A)
You're welcome for the help. Was that your version that worked or did you just verify that mine works?

High school huh? Nice early start for you then! I'm a little older. My son is a H.S. senior this year.

I wanted to post back once more because I finally have my newest version of this working fully. I have takeQuiz() now reading questions and answers from text files (one file for each subMenu item). This program will give a multi-question quiz and score it. Missed answers are followed by the correct answer during the quiz.
The program is simpler than the one I posted earlier in some ways. Note how all info for the menus is in one place, the INITmenus() function.
The use of structures, vectors, strings and all the file I/O makes it more complex though.
It is still a single source file program (with nothing in extra header files). The program needs to have correctly named and placed text files for it to work though. More about that following the program 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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
using namespace std;

struct menuData
{
	unsigned int Nchoices;
	string title;
	vector<string> topics;
	vector<string> fileName;
};


// function prototypes
void INITmenus( menuData& Mdata, vector<menuData>& SubData );
void LISTfileNames(const menuData& Mdata, const vector<menuData>& SubData );
unsigned int Menu(const menuData& Mdata );// display any Menu and get topic choice as a selection #
unsigned int takeQuiz(unsigned int mChoice, unsigned int sChoice, const menuData& Mdata, const vector<menuData>& SubData );
unsigned int askQuestion( const string& Q, const string& A );

//** main function **
int main()
{
	menuData mainData;
	vector<menuData> subData;
	INITmenus( mainData, subData );
//	LISTfileNames( mainData, subData );// uncomment for a list of all required file names
		
	// Geometry\Circles.txt
	unsigned int mainChoice = Menu( mainData );
	unsigned int subChoice  = Menu( subData[mainChoice-1] );

	unsigned int quizScore = takeQuiz( mainChoice, subChoice, mainData, subData );
	
	cout << "You scored " << quizScore << " points." << endl;

	return 0;
}// end of main()

// function definitions

unsigned int askQuestion( const string& Q, const string& A )
{
	string Auser;
	cout << Q << " ";
	cin >> Auser;

	if( Auser == A )
	{
		cout << "Correct!" << endl << endl;
		return 1;
	}
	else
	{
		cout << "Wrong! The correct answer is: " << A << endl << endl;
		return 0;
	}
}// end of askQuestion()

unsigned int takeQuiz(unsigned int mChoice, unsigned int sChoice, const menuData& Mdata, const vector<menuData>& SubData )
{
	string fName( Mdata.fileName[mChoice-1] );
	fName.append( SubData[mChoice-1].fileName[sChoice-1] );	

	//** input file **
	vector<string> question;
	vector<string> answer;
	string q_temp, a_temp;	
	ifstream inFile( fName.c_str() );	
	if( inFile )
	{
		cout << fName << " was opened." << endl;
		
		while( !inFile.eof() )
		{			
			getline( inFile, q_temp, '\n' );
			question.push_back( q_temp );
			getline( inFile, a_temp, '\n' );
			answer.push_back( a_temp );
		}
					
		cout << "Nquestions = " << question.size() << endl;
			
		// Done! close-up
		inFile.close();
	}
	else
		cout << fName << " was NOT opened." << endl;

	// ready for quiz
	unsigned int score = 0;	
	if( question.size() > 0 )
	{
		cout << "\n\n\n " << Mdata.topics[mChoice-1] << " " << SubData[mChoice-1].topics[sChoice-1] << " Quiz." << endl;
		cout << "----------------------------------------" << endl;
		for(unsigned int q=0; q<question.size(); q++)
			score += askQuestion( question[q], answer[q] );	
	}
	
	return score;
}// end of takeQuiz()

void INITmenus( menuData& Mdata, vector<menuData>& SubData )
{
	menuData MD_temp;
	Mdata.title = "Main";

	Mdata.topics.push_back("Geometry");     Mdata.fileName.push_back("quizData\\Geometry\\");	
	MD_temp.topics.push_back("Circles");    MD_temp.fileName.push_back("Circles.txt" );
	MD_temp.topics.push_back("Rectangles"); MD_temp.fileName.push_back("Rectangles.txt" );
	MD_temp.topics.push_back("Triangles");  MD_temp.fileName.push_back("Triangles.txt" );
	MD_temp.title = Mdata.topics[ Mdata.topics.size() - 1 ];
	MD_temp.Nchoices = MD_temp.topics.size();
	SubData.push_back(MD_temp);
	
	MD_temp.topics.clear();
	MD_temp.fileName.clear();
	Mdata.topics.push_back("History");         Mdata.fileName.push_back("quizData\\History\\");	
	MD_temp.topics.push_back("Ancient");       MD_temp.fileName.push_back("Ancient.txt" );
	MD_temp.topics.push_back("18th century");  MD_temp.fileName.push_back("century18.txt" );
	MD_temp.topics.push_back("19th century");  MD_temp.fileName.push_back("century19.txt" );
	MD_temp.topics.push_back("United States"); MD_temp.fileName.push_back("USA.txt" );
	MD_temp.title = Mdata.topics[ Mdata.topics.size() - 1 ];
	MD_temp.Nchoices = MD_temp.topics.size();
	SubData.push_back(MD_temp);

	MD_temp.topics.clear();
	MD_temp.fileName.clear();
	Mdata.topics.push_back("Geography");       Mdata.fileName.push_back("quizData\\Geography\\");
	MD_temp.topics.push_back("Asia");          MD_temp.fileName.push_back("Asia.txt" );
	MD_temp.topics.push_back("United States"); MD_temp.fileName.push_back("USA.txt" );
	MD_temp.title = Mdata.topics[ Mdata.topics.size() - 1 ];
	MD_temp.Nchoices = MD_temp.topics.size();
	SubData.push_back(MD_temp);

	Mdata.Nchoices = Mdata.topics.size();
	return;
}// end of INITmenus()

void LISTfileNames(const menuData& Mdata, const vector<menuData>& SubData )
{
	string fName;

	cout << "Listing all fileNames" << endl;
	for(unsigned int m=0; m<Mdata.fileName.size(); m++)
		for(unsigned int s = 0; s < SubData[m].fileName.size(); s++)
		{
			fName = Mdata.fileName[m];
			fName.append( SubData[m].fileName[s] );
			cout << fName << endl;
		}

	return;
}// end of LISTfileNames()

unsigned int Menu(const menuData& Mdata )// this replaces all of the previous individual xyzMenu()'s
{
	unsigned int choice = 0;

	cout << "\n\n\n\n";
	do// get choice
	{
		cout << "    *** " << Mdata.title << " Menu ***" << endl << endl;
		cout << "Please choose a topic below" << endl;
		cout << "---------------------------" << endl;

		for(unsigned int j=1; j<=Mdata.Nchoices; j++)	
			cout << "      " << j << ". " << Mdata.topics[j-1] << endl;	
	
		cout << "     --------------" << endl;
		cout << "Your choice? (1-" << Mdata.Nchoices << "): ";
		cin >> choice;

		// check choice
		if(choice > Mdata.Nchoices)
			cout << endl << "That choice was too high. Try again." << endl;
		if(choice < 1)
			cout << endl << "That choice was too low. Try again." << endl;

	}while( (choice > Mdata.Nchoices)||(choice < 1) );	

	return choice;
}// end of Menu() 


About the text files containing the questions and answers:
Add a folder titled quizData inside the same folder as the cpp file for this program.
In the quizData folder add 3 folders: Geometry, History and Geography
In the Geometry folder place a file named Circles.txt with the following content exactly.
What is 5 + 2 ?
7
12 + 9 =
21
2 x 3 =
6
The square root of 144 =
12
Pi to four places =
3.142

You can now take a 5 question quiz on Geometry > Circles ( I know they are actually arithmatic questions). Change the Q's and A's to suit but the fomat MUST be:
question1
answer1
question2
answer2

And so on... Hope it works for you. Try tinkering with that!!
There is a line of code in main() which calls LISTfiles(). If you uncomment that line the program will list all of the files needed to make all of the quizzes work.
o.o That's a lot of code...
I think the current form is enough. I can simply put the answer after it says that the answer is wrong.

I was talking about the code that you posted worked. I tried to correct mine so many times, but it wouldn't work.

I've rethought trying to make the questions random. Instead I thought that I might have a possible (READ: useful) use for goto.
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
cout << "Question" << endl;
cin >> iNumber 
b = iNumber==12 ? 1 : 0;
if ( b )
cout << "Correct!"; << endl;
else
cout << "Wrong! The answer was 12"; << endl;
cout << "Pick a number (2-5)" << endl;
cin >> iNumber;

iNumber==2 ? 2 : 0;//This is all just number verification
iNumber==3 ? 3 : 0;
iNumber==4 ? 4 : 0;
iNumber==5 ? 5 : 0;

if ( 2 )
goto Question 2;

if ( 3 )
goto Question 3;

if ( 4 )
goto Question 4;

if ( 5 )
goto Question 5;


Question 2:
cout << "Question" << endl;
cin >> iNumber
c = iNumber==12 ? 1 : 0;
if ( c )
cout << "Correct!" << endl;
else
cout << "Wrong! The answer was 12" << endl;

cout << "Pick a number (3-6)" << endl;
cin >> iNumber;

And the rest would be similar.

I could also put goto at the end and use it to restart the entire quiz using a similar method

[EDIT]
Forget the last paragraph, it won't work since the start and end are at different blocks
Last edited on
Pages: 12