Getting errors with game code. Need Help.

Hi, I needed help to run the following code which is being used for a game for our project. I was getting errors. Could someone maybe try running it and see what the problem might be as I'm new to programming and this code was done by one of my group members. Thanks

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>
using namespace std;

#include <string>
#include <Windows.h>
​

​
//Background functions
int generateRandomNumber(int ceiling);
void waitForCommand();
void refreshScreen();
void refreshStatsBox();
void refreshCommandBox();
void moveCursorTo(int x, int y);
void resizeWindow(int x, int y);
​
//Background variables
int screenWidth = 79;
int topSegmentHeight = 20;
int bottomSegmentHeight = 11;
​
bool keepGoing = true;
​
//===============================================================////Command functions
void showHelp();
void showStats();
void build();
void hunt();
void chopWood();
void mineGold();
void endDay();
​
//Command Variables
int day = 1;
int villagers = 3;
int food = 100;
int gold = 100;
int wood = 100;
int energyLeft = 5;
​
​
int main()
{
	resizeWindow(1500, 375);
	refreshScreen();
	refreshCommandBox();
	refreshStatsBox();
	​
		moveCursorTo(14, topSegmentHeight + 12);
	​
	while (keepGoing)
		waitForCommand();
	​
		return 0;
}
​
void waitForCommand()
{
	string command;
	​
		cin >> command;
	​
	if (command == "Exit" || command == "exit")
		keepGoing = false;
	else if (command == "Day" || command == "day")
	{
		day++;
		refreshStatsBox();
	}
	​
		refreshCommandBox();
}
​
​
void refreshScreen()
{
	//LOOP FOR TOP STARS//
	for (int i = 0; i < screenWidth; i++)
		cout << "*";
	​
		cout << endl;
	​
		//LOOP FOR BORDERS FOR TOP WINDOW//
	for (int i = 0; i < topSegmentHeight; i++)
	{
		for (int i = 0; i < screenWidth; i++)
		{
			if (i == 0 || i == screenWidth - 1)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
	​
		//LOOP FOR MIDDLE STARS//
	for (int i = 0; i < screenWidth; i++)
		cout << "*";
	​
		cout << endl;
	​
		//LOOP FOR BORDERS FOR BOTTOM WINDOW//
	for (int i = 0; i < bottomSegmentHeight; i++)
	{
		for (int i = 0; i < screenWidth; i++)
		{
			if (i == 0 || i == screenWidth - 1)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
	​
		//LOOP FOR BOTTOM STARS//
	for (int i = 0; i < screenWidth; i++)
		cout << "*";
	​
		cout << endl;
}
​
void refreshCommandBox()
{
	string command;
	​
		moveCursorTo(0, topSegmentHeight + 11);
	​
	for (int i = 0; i < screenWidth; i++)
		cout << "=";
	​
		moveCursorTo(2, topSegmentHeight + 12);
	cout << "Command >>>                                                                 ";
	moveCursorTo(2, topSegmentHeight + 12);
	cout << "Command >>> ";
}
​
void refreshStatsBox()
{
	for (int i = 0; i < bottomSegmentHeight - 2; i++)
	{
		moveCursorTo(15, topSegmentHeight + i + 2);
		cout << "|";
	}
	moveCursorTo(5, 22);
	cout << "STATS";
	​
		moveCursorTo(2, 24);
	cout << "Day = " << day;
	moveCursorTo(2, 25);
	cout << "Energy = 1 ";
	moveCursorTo(2, 26);
	cout << "Health = 5";
	moveCursorTo(2, 27);
	cout << "Food = 100";
	moveCursorTo(2, 28);
	cout << "Wood = 100";
	moveCursorTo(2, 29);
	cout << "Gold = 100";
}
​
void resizeWindow(int x, int y)
{
	int cx = GetSystemMetrics(SM_CXSCREEN);// get screen width
	int cy = GetSystemMetrics(SM_CYSCREEN);// height
	cx /= 2; // get center
	cy /= 2;
	cx -= 680 / 2; //subtract center of window
	cy -= 720 / 2;
	HWND console = GetConsoleWindow();
	MoveWindow(console, cx, cy, x, y, TRUE); //resizes and moves window 
}
​
void moveCursorTo(int x, int y)
{
	COORD p = { x, y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
}
​
//================================================================================================//int generateRandomNumber(int ceiling)
{
	int randomNumber = rand() % ceiling + 1;
	​
		return randomNumber;
}
​
void showHelp()
{
	cout << "Commands====================" << endl;
	cout << "'Hunt' --> Will send out one villager to hunt and gather food" << endl;
	cout << endl;
}
​
void build()
{
	energyLeft--;
	​
		cout << "Building a house..." << endl;
	cout << "Lost: 50 wood, 50 gold, 10 food" << endl;
	cout << "Gained: 5 villagers" << endl;
	cout << endl;
	​
		wood -= 50;
	gold -= 50;
	food -= 10;
	villagers += 5;
}
​
void hunt() //Actually uses RNG, only one
{
	energyLeft--;
	​
		int randomNumber = generateRandomNumber(10);
	​
		cout << "Hunting...." << endl;
	​
	if (randomNumber <= 5)
	{
		cout << "Hunting worked perfectly!" << endl;
		cout << "Lost: nothing" << endl;
		cout << "Gained: 50 food" << endl;
		cout << endl;
		​
			food += 50;
	}
	else if (randomNumber > 5 && randomNumber < 8)
	{
		cout << "Your villager was killed by a bear!" << endl;
		cout << "Lost: 1 villager" << endl;
		cout << "Gained: nothing" << endl;
		cout << endl;
	}
	else
	{
		cout << "The hunter found only a small rabbit and so brought back a small amount of food" << endl;
		cout << "Lost: nothing" << endl;
		cout << "Gained: 10 food" << endl;
		cout << endl;
		​
			food += 10;
	}
	​
}
​
void chopWood()
{
	energyLeft--;
	​
		cout << "Chopping wood...." << endl;
	cout << "Lost: nothing" << endl;
	cout << "Gained: 50 wood" << endl;
	cout << endl;
	​
		wood += 50;
}
​
void mineGold()
{
	energyLeft--;
	​
		cout << "Mining for Gold...." << endl;
	cout << "Lost: nothing" << endl;
	cout << "Gained: 50 gold" << endl;
	cout << endl;
	​
		gold += 50;
}
​
void endDay()
{
	energyLeft = 5;
	day++;
}Put the code you need help with here.
I was getting errors.

...What errors.

Debugger should tell you/your team members more than having to paste your entire program here.
I got a load of stray \342, stray \200, and stray \214 errors. Those are invisible characters in your code. Basically I took out the extra lines and reformatted your lines...
I fixed a few minor errors... forgotten brackets, took variables out of your prototypes and so on... made a few comments.

Now the fun begins.

GetConsoleWinow is out of scope in function MoveCursorTo(int, int)
and a few warnings about converting int to short inside of {}'s

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
#include <iostream>
#include <string>              //These go before namespace
#include <Windows.h>

using namespace std;


//Background functions
int generateRandomNumber(int);   // No parameter names in prototypes
void waitForCommand();
void refreshScreen();
void refreshStatsBox();
void refreshCommandBox();
void moveCursorTo(int,int);  // or here
void resizeWindow(int,int);
void showHelp();
void showStats();
void build();
void hunt();
void chopWood();
void mineGold();
void endDay();

//Background variables             // Global variables are bad...
int screenWidth = 79;
int topSegmentHeight = 20;
int bottomSegmentHeight = 11;
bool keepGoing = true;


//Command Variables
int day = 1;
int villagers = 3;
int food = 100;
int gold = 100;
int wood = 100;
int energyLeft = 5;

int main()
{
	resizeWindow(1500, 375);
	refreshScreen();
	refreshCommandBox();
	refreshStatsBox();
	moveCursorTo(14, topSegmentHeight + 12);

	while (keepGoing)
		waitForCommand();
		return 0;
}

void waitForCommand()
{
	string command;
	cin >> command;
    if (command == "Exit" || command == "exit"){             // you forgot {
		keepGoing = false;}
	else if (command == "Day" || command == "day")
	{
		day++;
		refreshStatsBox();
	}

	refreshCommandBox();
}

void refreshScreen()

{
	//LOOP FOR TOP STARS//
	for (int i = 0; i < screenWidth; i++)
		cout << "*";

		cout << endl;

		//LOOP FOR BORDERS FOR TOP WINDOW//
	for (int i = 0; i < topSegmentHeight; i++)
	{
		for (int i = 0; i < screenWidth; i++)
		{
			if (i == 0 || i == screenWidth - 1)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
		//LOOP FOR MIDDLE STARS//
	for (int i = 0; i < screenWidth; i++)
		cout << "*";
		cout << endl;
		//LOOP FOR BORDERS FOR BOTTOM WINDOW//
	for (int i = 0; i < bottomSegmentHeight; i++)
	{
		for (int i = 0; i < screenWidth; i++)
		{
			if (i == 0 || i == screenWidth - 1)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
		//LOOP FOR BOTTOM STARS//
	for (int i = 0; i < screenWidth; i++)
		cout << "*";
		cout << endl;
}
void refreshCommandBox()
{
	string command;
		moveCursorTo(0, topSegmentHeight + 11);
	for (int i = 0; i < screenWidth; i++)
		cout << "=";
		moveCursorTo(2, topSegmentHeight + 12);
	cout << "Command >>>                                                                 ";
	moveCursorTo(2, topSegmentHeight + 12);
	cout << "Command >>> ";
}
void refreshStatsBox()
{
	for (int i = 0; i < bottomSegmentHeight - 2; i++)
	{
		moveCursorTo(15, topSegmentHeight + i + 2);
		cout << "|";
	}
	moveCursorTo(5, 22);
	cout << "STATS";
	moveCursorTo(2, 24);
	cout << "Day = " << day;
	moveCursorTo(2, 25);
	cout << "Energy = 1 ";
	moveCursorTo(2, 26);
	cout << "Health = 5";
	moveCursorTo(2, 27);
	cout << "Food = 100";
	moveCursorTo(2, 28);
	cout << "Wood = 100";
	moveCursorTo(2, 29);
	cout << "Gold = 100";
}

void resizeWindow(int x, int y)
{
	int cx = GetSystemMetrics(SM_CXSCREEN);// get screen width
	int cy = GetSystemMetrics(SM_CYSCREEN);// height
	cx /= 2; // get center
	cy /= 2;
	cx -= 680 / 2; //subtract center of window
	cy -= 720 / 2;
	HWND console = GetConsoleWindow();
	MoveWindow(console, cx, cy, x, y, TRUE); //resizes and moves window
}
void moveCursorTo(int x, int y)
{
	COORD p = { x, y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
}
//================================================================================================//
int generateRandomNumber(int ceiling)
{
	int randomNumber = rand() % ceiling + 1;
		return randomNumber;
}
void showHelp()
{
	cout << "Commands====================" << endl;
	cout << "'Hunt' --> Will send out one villager to hunt and gather food" << endl;
	cout << endl;
}
void build()
{
	energyLeft--;
	cout << "Building a house..." << endl;
	cout << "Lost: 50 wood, 50 gold, 10 food" << endl;
	cout << "Gained: 5 villagers" << endl;
	cout << endl;
	wood -= 50;
	gold -= 50;
	food -= 10;
	villagers += 5;
}
void hunt() //Actually uses RNG, only one
{
	energyLeft--;
		int randomNumber = generateRandomNumber(10);
		cout << "Hunting...." << endl;
if (randomNumber <= 5)
	{
		cout << "Hunting worked perfectly!" << endl;
		cout << "Lost: nothing" << endl;
		cout << "Gained: 50 food" << endl;
		cout << endl;
			food += 50;
	}
	else if (randomNumber > 5 && randomNumber < 8)
	{
		cout << "Your villager was killed by a bear!" << endl;
		cout << "Lost: 1 villager" << endl;
		cout << "Gained: nothing" << endl;
		cout << endl;
	}
	else
	{
		cout << "The hunter found only a small rabbit and so brought back a small amount of food" << endl;
		cout << "Lost: nothing" << endl;
		cout << "Gained: 10 food" << endl;
		cout << endl;
			food += 10;
	}
}
void chopWood()
{
	energyLeft--;
    cout << "Chopping wood...." << endl;
	cout << "Lost: nothing" << endl;
	cout << "Gained: 50 wood" << endl;
	cout << endl;
		wood += 50;
}
void mineGold()
{
	energyLeft--;
	cout << "Mining for Gold...." << endl;
	cout << "Lost: nothing" << endl;
	cout << "Gained: 50 gold" << endl;
	cout << endl;
    gold += 50;
}
void endDay()
{
	energyLeft = 5;
	day++;
}

Topic archived. No new replies allowed.