I.Q test game help

Hello everyone! Recently, I tried in some experimentations based on an I.Q Test game. Let me know if there is something I need to improve since this is the first time I made such codes. Basically, this is all about testing one's intelligent from different subjects such as English, Math, and Science.
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
	using namespace std;
	char dataRecord (char rec);
	int main()
{
	char diff;
	char easy, medium, hard;
	char ans;
	char res;
	int totalScore;
	cout << "Welcome to the IQ Test!! How far can you go?";
	cout << "Please select your difficulty from Easy to Extreme,";
	cin >> diff;
	if (diff == easy)
	{
		cout << "You choose  easy!" << endl;
		cout << "There are five set of easy questions in this difficulty." << endl;
		cout << "Question no. 1!" << endl;
			cout << "Which number should come next in the series? 1 - 1 - 2 - 3 - 5 - 8 - 13" << endl;
			cout << "A. 8                     B. 21" << endl;
			cout << "C. 26                    D. 13";
			cin >> ans;
			switch (ans)
			{
			case 'A' :
				cout << " Wrong answer!';
				break;
			case 'B' :
			    cout << " Wrong answer!';
					break;
			case 'C' :
				cout << " Wrong answer!';
					break;
			default:
				cout <<" You got the correct answer!";
			}
			cout << "Question no. 2!" << endl;
			cout << "Of these four elements, which one is the appropriate element of Sodium?";
			cout << "A. Sb                     B. Si" << endl;
			cout << "C. Na                     D. Sm";
			cin >> ans;
			switch (ans)
			{
			case 'A' :
				cout << " Wrong answer!';
				break;
			case 'B' :
			    cout << " Wrong answer!';
				break;
			case 'D' :
				cout << " Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
			}
			cout << "Question no. 3!" << endl;
			cout << "Choose the appropriate options to complete the sentences." << endl;
			cout << "Most of the patient visits ---- to physician assistants in the recent years all around he world";
			cout << "A. have been made                    B. will have been made" << endl;
			cout << "C. make                              D. have made";
			cin >> ans;
			switch (ans)
			{
			case 'B' :
				cout << "Wrong answer!';
				break;
			case 'C' :
			    cout << "Wrong answer!';
				break;
			case 'D' :
				cout << "Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
			}
			cout << "Question 4!" << endl;
			cout << "K is the chemical symbol for which element?" << endl;
			cout << "A. Magnesium                       B. Potassium"; << endl;
			cout << "C. Mercury                         D. Oxygen";
			cin >> ans;
			switch (ans)
			{
			case 'A' :
				cout << " Wrong answer!';
				break;
			case 'C' :
			    cout << " Wrong answer!';
				break;
			case 'D' :
				cout << " Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
			}
			cout << "Question 5!" << endl;
			cout << "Which of the following is used in pencils?" << endl;
			cout << "A. Graphite                        B. Silicon" << endl;
			cout << "C. Charcoal                        D. Phosporous";
			cin >> ans;
			switch (ans)
			{
			case 'B' :
				cout << " Wrong answer!';
				break;
			case 'C' :
			    cout << " Wrong answer!';
				break;
			case 'D' :
				cout << " Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
			}
	}
		if (diff == medium)
		{
			cout << "You chose medium!"
			cout << "There are ten set of questions in this difficulty." << endl;
		    cout << "Question no. 1!" << endl;
			cout << "Solar Eclipse will take place when"; << endl;
			cout << "A.	the sun is between the moon and the earth       B.	the earth is between the moon and the sun" << endl;
            cout << "C.	the moon is between the sun and the earth       D.	the moon does not lie on the line joining the sun and the earth";
			cin >> ans;
			switch (ans)
			{
			case 'B' :
				cout << "Wrong answer!';
				break;
			case 'A' :
			    cout << "Wrong answer!';
				break;
			case 'D' :
				cout << "Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
		}
		cout << "Question no. 2!" << endl;
		cout << "One watt-hour is equivalent to:" << endl;
		cout << "A. 6.3 x 10^3 J                    B.	6.3 x 10^-7 J" << endl;
        cout <<" C.	3.6 x 10^3 J                    D.	3.6 x 10^-3 J";
		cin >> ans;
		switch (ans)
			{
			case 'B' :
				cout << "Wrong answer!';
				break;
			case 'A' :
			    cout << "Wrong answer!';
				break;
			case 'D' :
				cout << "Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
		}
		cout << "Question 3!" << endl;
		cout << "Identify the error in this sentence.";
		cout << "Learning a new language as an adult is more difficult than to learn one as a child." << endl;
		cout << "A. to learn           B. learning           C. No change";
		cin >> ans;
		switch (ans)
			{
			case 'B' :
				cout << "Wrong answer!';
				break;
			case 'C' :
			    cout << "Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
		}
		cout << "Question 4! << endl;
			cout << "Which of the following is the 10th term of the arithmetic sequence where the first term is 5 and the 4th term is 17." << endl;
		    cout << "A. 42                                      B.29";
			cout << "C. 21                                      D.41";
			cin >> ans;
			switch (ans)
			{
			case 'B' :
				cout << "Wrong answer!';
				break;
			case 'C' :
			    cout << "Wrong answer!';
				break;
			case 'A' :
				cout << "Wrong answer!';
				break;
			default:
				cout <<" You got the correct answer!";
		}
			cout << "Question 5!" << endl;
			cout << "Identify the correct phrase in this sentence." << endl;
			cout << "There are two ways to get a promotion: working hard or make friends in high places." << endl;
			cout << "A. to work hard                                  B. to working hard";
			cout << "C. work hard                                     D. no changes";
	
			system ("pause >0");
	        return 0;
} 


P.S, this is a very long code and I can't post the upcoming ones so I have to cut it here.
I would recommend breaking this down with functions which are called in your main program.

Example:

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

using namespace std;

string choice;
string CHOOSE1();

int a = 1, b = 2, c = 3, d = 4;

int main()
{
		cout << "Enter a letter a, b, c, or d: \n";

		cout << CHOOSE1();
}

string CHOOSE1()
{
	for (int i = 1; i < 1000; i++)
	{

		cin >> choice;

		if (choice == "a")
		{
			cout << "Input = ";
			cout << " 1 *\n";
		}
		else if (choice == "b")
		{
			cout << "Input = ";
			cout << " 2 **\n";
		}
		else if (choice == "c")
		{
			cout << "Input = ";
			cout << " 3 ***\n";
		}
		else if (choice == "d")
		{
			cout << "Input = ";
			cout << " 4 ****\n";
		}
	}
	return 0;
}


Once it gets the the string of cout << CHOOSE1(); it takes you to the function. Then brings you back once it has ended. Ignore my loop as this was used to help someone else.

By doing it this way, your code will be a lot more neat and organized. Hope this helps.
Last edited on
So, how would this look like if I were to place it on the following questions? I have 5 for easy, 10 for medium, and 10 for extreme.
One function that would shorten the code is a checkAnswer() function:
1
2
3
4
5
6
7
8
void checkAnswer(char correctAnswer, char userAnswer)
{
    if (correctAnswer == userAnswer) {
        cout << " You got the correct answer!\n";
    } else {
        cout << "Wrong Answer!\n";
    }
}

Then lines 25-38 become checkAnswer(ans, 'D');. Lines 44-57 become checkAnswer(ans,'C'); etc.

By the way, you don't initialize the characters easy, medium and hard, nor do you tell the user what characters they should enter for each of these.

Once you get this working, consider changing it so that the questions and correct answers are stored in a text file instead of the code. The file might be formatted like this:
- each question consists of:
- a line that gives the difficulty: Easy, Medium or Hard.
- a line that contains one character that is the correct answer.
- N lines that are the question
- a blank line. This signals the end of the question.

The main part of hte program then becomes something like this:
1
2
3
4
5
6
7
while (cin >> question) {
  if (question.difficulty == requestedDifficulty) {
      cout << question.text;
      cin >> ans;
      checkAnswer(question.answer, ans);
  }
}
Last edited on
Actually, I was suppose to set the difficulties first before triggering sets of questions, how am I gonna do that anyway?
Last edited on
I might not understand something. It looks like the user selects a difficulty level (diff). Then, depending on what level they selected, the program presents them with different questions. Is that right? To implement this with questions in a file, I'm saying that you mark each question with a difficulty level. If it matches the diff then the program asks that question.
Topic archived. No new replies allowed.