PLEASE HELP ME MAKE MY CODES SHORTER USING FUNCTIONS

Hi Guys. I'm just new here. I just want to seek help. We have a project in Visual c++.

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
#include<iostream>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>

using namespace std;

void gotoxy(int xpos, int ypos)	
{	
COORD scrn;	
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);	
scrn.X = xpos; scrn.Y = ypos;	
SetConsoleCursorPosition(hOutput,scrn);	
}


void intro(void)
{
   		int length;
		char *intr_text[2] = {{"NO KANGAROOS WERE HARMED IN THE MAKING OF THIS GAME"},
                            	      {"H A N G A R O O"}};
        system("cls");
        length = strlen(intr_text[0]);

        gotoxy((80 - length)/2, 13);
        system("color 05");
        cout<<intr_text[0];
        Sleep(1000);

        gotoxy((80 - length)/2, 13);
        system("color 0d");
        cout<<intr_text[0];
        Sleep(1000);

        gotoxy((80 - length)/2, 13);
        system("color 07");
        cout<<intr_text[0];
        Sleep(1000);

        length = strlen(intr_text[1]);
        system("cls");
        gotoxy((80 - length)/2, 13);
        system("color 0f");
        cout<<intr_text[1];
        Sleep(1000);

        system("cls");
}

int main()
{

	intro();

	char YOUR_ANSWER[50];

	gotoxy(18,4); cout<<"= CATEGORY: Health Diseases and Conditions =\n";
	gotoxy(35,6); cout<<"[ oearlhc ]\n"; 
	gotoxy(18,9); cout<<"YOU: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "cholera" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	gotoxy(18,4); cout<<"= CATEGORY: World's 100 Tallest Building =\n";
	gotoxy(35,6); cout<<"[ mrtssmeeu ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "messeturm" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	gotoxy(18,4); cout<<"= CATEGORY: Not so secret Herbs and Spices =\n";
	gotoxy(35,6); cout<<"[ ieoetltsm ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "mistletoe" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	
	gotoxy(18,4); cout<<"= CATEGORY: World's 50 Largest Lakes =\n";
	gotoxy(35,6); cout<<"[ mntbaoia ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "manitoba" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	
	gotoxy(18,4); cout<<"= CATEGORY: Birds species and names =\n";
	gotoxy(35,6); cout<<"[ aoutcns ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "toucans" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	
	gotoxy(18,4); cout<<"= CATEGORY: Periodic Table of Elements =\n";
	gotoxy(35,6); cout<<"[ mmlhoiu ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "holmium" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	
	gotoxy(18,4); cout<<"= CATEGORY: Health Diseases and Conditions =\n";
	gotoxy(35,6); cout<<"[ aieidphthr ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "diphtheria" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	
	gotoxy(18,4); cout<<"= CATEGORY: Creatures from way back when =\n";
	gotoxy(35,6); cout<<"[ uuaoahdrsrs ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "hadrosaurus" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	
	gotoxy(18,4); cout<<"= CATEGORY: Respiratory Problems =\n";
	gotoxy(35,6); cout<<"[ ueuoisslcrbt ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "tuberculosis" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");

	
	gotoxy(18,4); cout<<"= CATEGORY: News and Talk ShowsAnimals and Plants =\n";
	gotoxy(35,6); cout<<"[ aeienltd ]\n";
	gotoxy(18,9); cout<<"YOUR ANSWER: ";
	cin>>YOUR_ANSWER;
	if ( strcmp ( YOUR_ANSWER, "dateline" ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
    else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");


return 0;
}


And there you go, that's my code. Actually, its not yet done. My problem is, I want to make my program shorter. How can I do that? They told me to use functions but I have no idea How will I do that.

I saw a program..

1
2
3
4
5
6
7
8

    char *game_data[ASKED_ITEMS][2] = {{"phrase"   , "nobody is perfect!"},
                                                                   {"movie"    , "lord of the rings"},
                                                                   {"country"  , "philippines" },
                                                                   {"movie"    , "final destination"},
                                                                   {"inventor" , "hidetsugu yagi and shintaro uda"},
                                                                   {"scientist", "einstein"}
                                                                 };


I want to make my program just like that.
Last edited on
Hello,
you can do something like 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

struct Question
{
	Question(){}
	Question( char* _category, char* _item2, char* _you, char* _answer ) : category(_category), item2(_item2), you(_you), answer(_answer){}
	char* category;
	char* item2;
	char* you;
	char* answer;
};

int main()
{

	intro();
	
	Question questions[10] = {
					{"= CATEGORY: Health Diseases and Conditions =\n", "[ oearlhc ]\n", "YOU: ", "cholera" },
					{"= CATEGORY: World's 100 Tallest Building =\n", "[ mrtssmeeu ]\n", "YOUR ANSWER: ", "messeturm" },
					{"= CATEGORY: Not so secret Herbs and Spices =\n", "[ ieoetltsm ]\n", "YOUR ANSWER: ", "mistletoe" },
					{"= CATEGORY: World's 50 Largest Lakes =\n", "[ mntbaoia ]\n", "YOUR ANSWER: ", "manitoba" },
					{"= CATEGORY: Birds species and names =\n", "[ aoutcns ]\n", "YOUR ANSWER: ", "toucans" },
					{"= CATEGORY: Periodic Table of Elements =\n", "[ mmlhoiu ]\n", "YOUR ANSWER: ", "holmium" },
					{"= CATEGORY: Health Diseases and Conditions =\n", "[ aieidphthr ]\n", "YOUR ANSWER: ", "diphtheria" },
					{"= CATEGORY: Creatures from way back when =\n", "[ uuaoahdrsrs ]\n", "YOUR ANSWER: ", "hadrosaurus" },
					{"= CATEGORY: Respiratory Problems =\n", "[ ueuoisslcrbt ]\n", "YOUR ANSWER: ", "tuberculosis" },
					{"= CATEGORY: News and Talk ShowsAnimals and Plants =\n", "[ aeienltd ]\n", "YOUR ANSWER: ", "dateline" }
				};

	char YOUR_ANSWER[50];
	
	for( unsigned int i = 0; i < 10; ++i )
	{
		gotoxy(18,4); cout << questions[i].category;
		gotoxy(35,6); cout << questions[i].item2; 
		gotoxy(18,9); cout << questions[i].you;
		cin>>YOUR_ANSWER;
		
		if ( strcmp ( YOUR_ANSWER, questions[i].answer ) == 0 ) // Equal strings
		{	
			gotoxy(18,11); 
			cout<<"Your answer is correct!\n";
			Sleep(1000);
		}
		else                                    // Not equal
		{
			gotoxy(18,11); 
			cout<<"Oooops! Sorry. You're Wrong!\n";
			Sleep(1000);
		}
		system("cls");
	}
	
return 0;
}


But this use object not functions.
Last edited on
And if you really want function calls, you could do something like that :

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

struct Question
{
	Question(){}
	Question( char* _category, char* _item2, char* _you, char* _answer ) : category(_category), item2(_item2), you(_you), answer(_answer){}
	char* category;
	char* item2;
	char* you;
	char* answer;
};

void AskQuestion( Question (&q)[10], int index )
{
	char YOUR_ANSWER[50];
	
	gotoxy(18,4); cout << q[index].category;
	gotoxy(35,6); cout << q[index].item2; 
	gotoxy(18,9); cout << q[index].you;
	cin>>YOUR_ANSWER;
		
	if ( strcmp ( YOUR_ANSWER, q[index].answer ) == 0 ) // Equal strings
	{	
		gotoxy(18,11); 
		cout<<"Your answer is correct!\n";
		Sleep(1000);
	}
	else                                    // Not equal
	{
		gotoxy(18,11); 
		cout<<"Oooops! Sorry. You're Wrong!\n";
		Sleep(1000);
	}
	system("cls");
}

int main()
{

	intro();
	
	Question questions[10] = {
					{"= CATEGORY: Health Diseases and Conditions =\n", "[ oearlhc ]\n", "YOU: ", "cholera" },
					{"= CATEGORY: World's 100 Tallest Building =\n", "[ mrtssmeeu ]\n", "YOUR ANSWER: ", "messeturm" },
					{"= CATEGORY: Not so secret Herbs and Spices =\n", "[ ieoetltsm ]\n", "YOUR ANSWER: ", "mistletoe" },
					{"= CATEGORY: World's 50 Largest Lakes =\n", "[ mntbaoia ]\n", "YOUR ANSWER: ", "manitoba" },
					{"= CATEGORY: Birds species and names =\n", "[ aoutcns ]\n", "YOUR ANSWER: ", "toucans" },
					{"= CATEGORY: Periodic Table of Elements =\n", "[ mmlhoiu ]\n", "YOUR ANSWER: ", "holmium" },
					{"= CATEGORY: Health Diseases and Conditions =\n", "[ aieidphthr ]\n", "YOUR ANSWER: ", "diphtheria" },
					{"= CATEGORY: Creatures from way back when =\n", "[ uuaoahdrsrs ]\n", "YOUR ANSWER: ", "hadrosaurus" },
					{"= CATEGORY: Respiratory Problems =\n", "[ ueuoisslcrbt ]\n", "YOUR ANSWER: ", "tuberculosis" },
					{"= CATEGORY: News and Talk ShowsAnimals and Plants =\n", "[ aeienltd ]\n", "YOUR ANSWER: ", "dateline" }
				};

	for( unsigned int i = 0; i < 10; ++i )
	{
		AskQuestion( questions, i );
	}
	
return 0;
}


As your code is C++, you should consider std::string instead of char* and std::vector instead of arrays.
Thank you so much for answering me I really appreciate it!
I tried to run the program so I can see the result but it has an error.

1
2
3

error C2552: 'questions' : non-aggregates cannot be initialized with initializer list


I don't know how can I fixed it.

Try with
1
2
3
4
5
6
7
8
9
10
11
12

Question questions[10];
questions[0] = Question("= CATEGORY: Health Diseases and Conditions =\n", "[ oearlhc ]\n", "YOU: ", "cholera");
questions[1] = Question("= CATEGORY: World's 100 Tallest Building =\n", "[ mrtssmeeu ]\n", "YOUR ANSWER: ", "messeturm");
questions[2] = Question("= CATEGORY: Not so secret Herbs and Spices =\n", "[ ieoetltsm ]\n", "YOUR ANSWER: ", "mistletoe");
questions[3] = Question("= CATEGORY: World's 50 Largest Lakes =\n", "[ mntbaoia ]\n", "YOUR ANSWER: ", "manitoba");
questions[4] = Question("= CATEGORY: Birds species and names =\n", "[ aoutcns ]\n", "YOUR ANSWER: ", "toucans");
questions[5] = Question("= CATEGORY: Periodic Table of Elements =\n", "[ mmlhoiu ]\n", "YOUR ANSWER: ", "holmium");
questions[6] = Question("= CATEGORY: Health Diseases and Conditions =\n", "[ aieidphthr ]\n", "YOUR ANSWER: ", "diphtheria");
questions[7] = Question("= CATEGORY: Creatures from way back when =\n", "[ uuaoahdrsrs ]\n", "YOUR ANSWER: ", "hadrosaurus");
questions[8] = Question("= CATEGORY: Respiratory Problems =\n", "[ ueuoisslcrbt ]\n", "YOUR ANSWER: ", "tuberculosis");
questions[9] = Question("= CATEGORY: News and Talk ShowsAnimals and Plants =\n", "[ aeienltd ]\n", "YOUR ANSWER: ", "dateline");


instead of

1
2
3
4
5
6
7
8
9
10
11
12
Question questions[10] = {
			{"= CATEGORY: Health Diseases and Conditions =\n", "[ oearlhc ]\n", "YOU: ", "cholera" },
			{"= CATEGORY: World's 100 Tallest Building =\n", "[ mrtssmeeu ]\n", "YOUR ANSWER: ", "messeturm" },
			{"= CATEGORY: Not so secret Herbs and Spices =\n", "[ ieoetltsm ]\n", "YOUR ANSWER: ", "mistletoe" },
			{"= CATEGORY: World's 50 Largest Lakes =\n", "[ mntbaoia ]\n", "YOUR ANSWER: ", "manitoba" },
			{"= CATEGORY: Birds species and names =\n", "[ aoutcns ]\n", "YOUR ANSWER: ", "toucans" },
			{"= CATEGORY: Periodic Table of Elements =\n", "[ mmlhoiu ]\n", "YOUR ANSWER: ", "holmium" },
			{"= CATEGORY: Health Diseases and Conditions =\n", "[ aieidphthr ]\n", "YOUR ANSWER: ", "diphtheria" },
			{"= CATEGORY: Creatures from way back when =\n", "[ uuaoahdrsrs ]\n", "YOUR ANSWER: ", "hadrosaurus" },
			{"= CATEGORY: Respiratory Problems =\n", "[ ueuoisslcrbt ]\n", "YOUR ANSWER: ", "tuberculosis" },
			{"= CATEGORY: News and Talk ShowsAnimals and Plants =\n", "[ aeienltd ]\n", "YOUR ANSWER: ", "dateline" }
			};


I used to compile with the C++0x flag so the initializer lists were activated...

Oh and as "= CATEGORY: " does repeat in each row, you could remove them from the array values and change
gotoxy(18,4); cout << q[index].category; to gotoxy(18,4); cout << "= CATEGORY: " << q[index].category;

And same with the [] on the hints (item2). This way if later you decide to change [] by {} you have to change it once and not in all rows of the array. Functions must help you to do less work as possible ;)
Last edited on
Topic archived. No new replies allowed.