Quiz Game

Hi guys !
I am new to C++.I wanted to make a C++ quiz game.The game will have 20 questions or more.In the code are only 2 questions as examples.
I have some questions:
1.Is there a possibility to ask the questions randomly?I mean that the order of the questions should not be the same.
2.It is possible to randomize the answers ? For instance , first time A. will be Berlin,but the next time it will be Berna or something.
3.Can I make this with functions,because I have just learned them online and I would like to apply them somehow.
4.I want to make at the second menu point a different game mode , where you can choose a category,like Geography,History etc. It is possible to make an array of strings?
I don't want to write my program , I want some help.I forgot to translate some things so :
alegere = choice
Alege ce vrei sa faci = Choose what do you want to do.
Incepe jocul = Start the game
Informatii = Info
Alegerea ta este = Your choice is
Raspunsul este corect = Your answer is correct
Raspunsul este gresit = Your answer is wrong
Pas = Pass
Sorry from my broken English.It's not my natal language.

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
#include <iostream>
using namespace std;
long p;
int main ()
{
	int alegere = 0;
	cout << "				***MARELE CUNOSCATOR***"<<endl<<endl<<endl;
	cout << "Alege ce vrei sa faci:" << endl << endl;
	cout << "1.Incepe jocul." << endl ;
	cout << "2.Alege un domeniu separat de joc." << endl;
	cout << "3.Informatii. " << endl << endl;
	cout << "Alegerea ta este : ";cin >> alegere;
	cout << string(50,'\n');
	char right [] = "Raspunsul este corect.";
	char wrong [] = "Raspunsul este gresit.Raspunsul corect este ";
	char pass [] = "Raspunsul corect este ";
	switch (alegere)
	{case 1:cout<<"Intrebarea 1:"<<endl;
	cin.get ();
	string answer1;
	char question1[] = "Care este capitala Germaniei?";
	cout<<question1<<endl;
	cout<< endl <<"A.Bruxelles."<< endl <<"B.München."<< endl<<"C.Liverpool."<<endl<<"D.Berlin."<<endl<<endl<<"X.Pas."<<endl<<endl;
	char choiceA [] =  "Alegerea ta este : ";
	cout << choiceA;
	getline(cin,answer1);
	if (answer1=="D")
		{p++;
		 cout<< right <<endl<<endl;
		}
	else if (answer1=="X") cout << pass << "Berlin." << endl << endl;
	else {p--;
		  cout << wrong << "Berlin." << endl << endl;
	     }
	cout << "Intrebarea 2:" << endl << endl;
	string answer2;
	char question2[] = " Care din urmatorii lideri a fost conducatorul Imperiului Persan ? ";
	cout << question2 << endl;
	cout << endl << "A.Mehmed II" << endl << "B.Tokugawa" << endl << "C.Darius I"<< endl << "D.Mansa Musa" << endl << endl << "X.Pas" <<endl <<endl;
	cout << choiceA;
	getline (cin,answer2);
	if (answer2=="C")
		{p++;
		 cout<< right <<endl<<endl;
		}
	else if (answer1=="X") cout << pass << "Darius I." << endl << endl;
	else {p--;
		  cout << wrong << "Darius I." << endl << endl;
	     }  
	if (p>=10)cout<<"Felicitari ! Ai obtinut "<<p<<" din 20 de puncte."<<endl<<"Esti bine pregatit";
	if (p<0)p=0;
	if (p<10 && p>=0)cout<<"Ai obtinut "<<p<<" din 20 de puncte."<<endl<<"Incearca sa iti aprofundezi informatiile.Nu esti foarte bine pregatit.";
	}
	return 0;
}
1.Is there a possibility to ask the questions randomly?

Yes, but you have to write the code to do that. Put the questions in a vector, select them from the vector randomly. You probably want to mark the questions as used so that you don't repeat the same question if you happen to get the same random number more than once.

2.It is possible to randomize the answers ?

Yes, again, you have to write the code to do that. You're going to have to keep track of the correct answer. You might consider something like the following:
1
2
3
4
5
6
7
8
9
  struct q_and_a
  {  string question; 
      string correct_ans;
      string alt_ans1;
      string alt_ans2;
      bool answered_correctly;
  };
// You can then make a vector of q_and_a
  std::vector<q_and_a>  qa;

You might also consider loading your questions and answers from a file. That way you can use the same program and change the questions and answers by having multiple data files.

3.Can I make this with functions

Yes. Strongly recommended.

4. It is possible to make an array of strings?

Yes. Use a std::vector.
Last edited on
I will try , but probably I'll have more questions because I have never worked with functions or arrays of strings.
Thanks !
Can you explain me please what alt_ans1/2 represent and how to load the questions & answers from a file ? (with <fstream>)?
Last edited on
Can someone help me ?
I have never worked with functions or arrays of strings.

You have some learning to do.

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/reference/vector/vector/

Perhaps you should try something not quite so ambitious until you've learned functions and arrays/vectors.

Can you explain me please what alt_ans1/2 represent

alt_ans1 and alt_ans2 are alternate (incorrect) answers.

Ignoring switching around the order of the answers, you would display:
A. <correct answer>
B. <alt-answer1>
C. <alt-answer2>
Then let the user select A, B or C.

how to load the questions & answers from a file

Assume the order of the file is as follows:
Question
Correct answer
alt-answer1
alt-answer2

Open the file, read in the question, the correct answer and the alternate answers.
Put these strings in an instance of a q_and_a struct as I showed you above.
Push the local instance of the q_and_a struct onto the vector.
Continue doing this until you reach end of file.
Last edited on
Ok...Actually I played a little with the functions and i learned about them while I was waiting for an answer.
I will try.I will insert alt-answer3 too.
If I have more questions , I will ask.
closed account (zNASE3v7)
here is a link that might help you with this also,

http://www.cplusplus.com/doc/tutorial/structures/

and it looks like you know what you want, but the idea about structs is that it combines various data types into one group and you can use these same declarations over again. I will also give you a quick example of something that is similar to a struct and that is the string class which also uses the "." operator and so does the struct. In using a struct you can create your own data type and call each of the members (variables from the struct) using the dot operator.
example using the string class: cin.get, or cin.getline().
myStruct.alt_answer1();
(you can place the struct above main() and below your function prototypes...)
struct MyStruct{
string question1 = "Guess my name?";
string question2 = "Guess what city I from?"
string ans_1;
string ans2;
}; //note that the struct has a semi-colon on the end

here is an example of the code
cout << MyStruct.question1;
cin >> MyStruct.ans_1;
and you can also use rand() to add a lot of variety to the choices of which question gets asked...
http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand

Oh and one last note: don't forget to add #include<string>, and #include <stdio.h> , and #include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
These are the header files (classes) or libraries that have all the prewritten code for their functions such as ctime is a class that has been used and made into a template...it is the most optimal or perhaps the easiest method of performing the function and you just add it and all of it's functionality with the #include<ctime> preprocessor directive.

I hope this is not too confusing, but the idea of the game you have written out so far sounds and looks fun!!
@krisolafson - You have some misinformation in your post and you're misusing the struct.

In using a struct you can create your own data type and call each of the members (variables from the struct) using the dot operator.

You can reference member variables, but you can not call them.

example using the string class: cin.get, or cin.getline().
myStruct.alt_answer1();

If you're assuming alt_answer1 is a string, you can't call a string like that.

the idea about structs is that it combines various data types into one group

The example q_and_a struct I posted earlier creates a type that contains exactly one question, one correct answer and several alternate answers. Each occurrence of the struct is
a discrete question with it related answers.

The example you give violates the the principle of the struct containing exactly one question, one correct answer and several alternate answers.
1
2
3
4
5
6
struct MyStruct{
string question1 = "Guess my name?";
string question2 = "Guess what city I from?"
string ans_1;
string ans2;
};

Putting a a series of questions and the users' response in a single struct does not accomplish what the OP is asking.




Ok...I was thinking that I was not going ok with the functions ,so I stopped to ask you if it's ok..I personally don't think so..
I have to do only the hard parts...takeQuiz(),StartGeography() and StartHistory().
It is correct what I did with the multiple ifstream files?(f1,f2,f3)
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <ctime>
using namespace std;
ifstream f1("Overall");
ifstream f2("Geography");
ifstream f3("History");
int mainMenu(void);
int virtualMenu (int menuNumber);
int takeQuiz ();
int subvirtualMenu (int submenuNumber);
int Info (void);
int StartGeography();
int StartHistory();
int main()
{
    int mainChoice = mainMenu();
    int vChoice = virtualMenu (mainChoice);
    int subChoice = subMenu();
    int svChoice = subvirtualMenu(subChoice);
    int quizScore = takeQuiz( mainChoice, subChoice );
    return 0;
}
int mainMenu(void)
{
	int mainChoice = 0;

	cout << "    *** MARELE CUNOSCATOR ***" << endl << endl;
	cout << "Alege ce vrei sa faci." << endl;
	cout << "---------------------------" << endl;
	cout << "      1. Incepe jocul" << endl;
	cout << "      2. Alege un domeniu separat" << endl;
	cout << "      3. Informatii" << endl;
	cout << "     --------------" << endl;
	cout << "Alegerea ta? (1-3): ";
	cin >> mainChoice;

	return mainChoice;
}
int virtualMenu(int menuNumber)
{
	int vChoice = 0;

	cout << "\n\n\n\n";
	switch( menuNumber )
	{
	case 1:
		vChoice = Start();
		break;
	case 2:
		vChoice = SubMenu();
		break;
	case 3:
		vChoice = Info();
		break;
	default:
		break;
	}
	return vChoice;
}
int subMenu ()
{
    int subChoice = 0;
    cout << "Alege un domeniu :" << endl << endl;
    cout << "   1.GEOGRAFIE" << endl << endl;
    cout << "   2.ISTORIE" << endl << endl ;
    cout << "Alegerea ta este :";
    cin >> subChoice;
    return subChoice;
}
int subvirtualMenu(int submenuNumber)
{
    int subChoice = 0;

	cout << "\n\n\n\n";
	switch( submenuNumber )
	{
	case 1:
		subChoice = StartGeography();
		break;
	case 2:
		subChoice = StartHistory();
		break;
	default:
		break;
	}
	return subChoice;
}
void Info (void)
{
    cout << "In acest joc va trebui sa raspunzi la 100 de intrebari,pe baza carora vei primi puncte.";
    cout << endl << "Succes !" << endl << endl << "MADE BY STEFAN";
}
int StartGeography()

Thanks for help !
Last edited on
It is ok until now ?
Lines 7-9: Your definition of the three ifstreams is fine, although you might want to make those .txt files.

A couple of suggestions:
1) Instead of making the ifstream definitions global, make them local to the appropriate quiz function. i.e Put f3 inside StartHistory. Globals should be avoided.
2) Use more meaningful names than f1, f2, f3.




Topic archived. No new replies allowed.