Finding the lead horse.

When I the code comes up to decide if the current horse is in the lead, it will choose that horse regardless of whether its distance > lead. Any tips would be greatly appreciated.

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
  // Week 4 Assignment-3
// Description: Horserace
//----------------------------------

//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <array> // provides access to std::array
#include <time.h> // provides access to time()
#include <string> // provides access to getline()
#include <iomanip> 
//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------

//**begin global constants**********
// struct for horse (name, distance, eventOffset, ID)
struct Horse
{
	string name;
	int distance;
	int eventOffset;
	int ID;
};
// enum for horse names
static string ChristmasNames[] = {"Dasher", "Dancer", "Prancer", "Comet", "Cupid", "Blitzen"};
static string WarcraftNames[] = {"Arthas", "Thrall", "Illidan", "Sargeras", "Deathwing", "Garrosh"};
static string FriendsNames[] ={"Chandler", "Ross", "Rachel", "Joey", "Pheobe", "Monica"};
static string RaceNames [] ={"GonnaLose", "NeverWon", "BadLuck", "Failure", "FatChance", "YeahRight"};
// number of horses (6)
const int horseCount = 6;
// length of track (100)
const int trackLength = 100;
//--end of global constants---------
//----------------------------------

//**begin main program**************
int main()
{
	// seed random number generator
	srand(time(NULL));
	// create and initialize variables
	// two 1-6 dice
	int die1 = 0, die2 = 0;
	// sum of dice roll
	int sum;
	// number of winning horse
	int winner;
	// distance of leading horse
	int lead = 0;
	// bet
	int bet =  0;
	// cash on hand
	int cash = 100;
	// flag for ending race
	bool racing = false;
	// flag for ending game
	bool playing = true;
	// horse number bet on
	int hNum = 0;
	// user's input on horse names
	int userSet = 0;
	// user's input on starting the game
	int getPlaying = 0;
	//Begin's main loop
	while (playing)
	{
		cout << "***********************************\n"
			 << "* WELCOME TO THE HORSE RACE DERBY *\n"
			 << "***********************************\n";
		cout << "You have $" << cash << endl;
		cout << "\nAre you ready to play?\n"
			 << "[1]Yes\t[2]No\n";
		cin  >> getPlaying;
		if (getPlaying == 1)
		{
			 playing = true;
		}
		else
		{
			 return 0;
		}
		// create horses
		array <Horse, horseCount> horses;
		cout << "\nPick a set of horses:" << endl // have user name horses (range-based for loop)
			 << "[1]Christmas Theme\t[2]World of Warcraft Enemies\n"
			 << "[3]Friendly horses\t[4]Extreme Racers\n";
		cin >> userSet;
		if (userSet == 1)
		{
			for(int i = 0; i < horseCount; i++) 
			{
				horses[i].name = ChristmasNames[i];
			}
		}
		else if (userSet == 2)
		{
			for(int i = 0; i < horseCount; i++) 
			{
				horses[i].name = WarcraftNames[i];
			}
		}
		else if (userSet == 3)
		{
			for(int i = 0; i < horseCount; i++)
			{
				horses[i].name = FriendsNames[i];
			}
		}
		else if (userSet == 4)
		{
			for(int i = 0; i < horseCount; i++)
			{
				horses[i].name = RaceNames[i];
			}
		}
		for(int x = 0; x < horseCount; x++) 
		{
			horses[x].ID = x + 1;
			cout << "Horse " << horses[x].ID << " name is " << horses[x].name << "\n";
		}
						
		for(int i = 0; i < horseCount; i++) 
		{
			horses[i].distance = 0;
			horses[i].eventOffset = 0;
		}
		// get the number of the horse being bet on
		cout << "\nWhich horse do you want to bet on?\n";
		cin >> hNum;
		while (hNum < 0 && hNum > 6)
		{
				cout << "\nYou have entered an invalid number. Please select a horse 1-6\n";
				cin >> hNum;
		}
		cout << "\nYour horse is " << horses[hNum - 1].name << endl;
		// ask for a bet amount
		cout << "\nPlease place your bet\n$";
		cin >> bet;
		// if bet is less than or equal to $0-- quit 
		while (bet <= 0  || bet > cash)
		{
			cout << "\nYou entered an invalid number. Did you want to try again?"
				 << "\n[1]Yes\t[2]No\n";
			cin >> getPlaying;
			if (getPlaying == 1)
			{
				playing = true;
				cout << "\nPlease place your bet\n$";
				cin >> bet;
			}
			else
			{
				return 0;
			}
		}
		cash = cash - bet;
		cout << "You have $" << cash << " left.\n";
		cout << "Press enter to begin the race!";
		cin.ignore();
		cin.get();
		racing = true;
			// start of race loop
		while (racing)
		{
				// for each horse--use range-based for
			for(int i = 0; i < horseCount; i++)
			{							  // roll one 16 sided die to check on event 
				switch (int x = 1 + rand()%16)// use switch statement to handle event
				{
					case 0:
					case 1:
						cout << horses[i].name << " breaks stride.\n";
						horses[i].eventOffset = horses[i].eventOffset - rand()%2 + 1;
						break;
					case 2:
					case 3:
						cout << horses[i].name << " finds stride\n";
						horses[i].eventOffset = horses[i].eventOffset + rand()%2 + 1;
						break;
					case 4:
						cout << horses[i].name << " stumbles.\n";
						horses[i].eventOffset = horses[i].eventOffset - rand()%3  + 4;
						break;
					case 5:
						cout << horses[i].name << " gets a burst of energy.\n";
						horses[i].eventOffset = horses[i].eventOffset + rand()%3 + 4;
						break;
					default:
						cout << horses[i].name << " keeps the pace.\n";
						break;
				}
				// roll dice and, using offset, adjust horse’s distance
				die1 = 1 + rand()%6;
				die2 = 1 + rand()%6;
				sum = die1 + die2;
				horses[i].distance = sum + horses[i].eventOffset;
				// check for race over
				if (horses[i].distance >= trackLength)
				{
					winner = horses[i].ID;
					racing = false;
				}
				// report horse position
				else 
				{
					cout << horses[i].name << " is at " << horses[i].distance << endl;
				}
				// check if this is leading horse and change lead and winner values if it is
				if (horses[i].distance > lead)
				{
					horses[i].distance = lead;
					cout << horses[i].name  << " has the lead." << endl;
				}
			//continue race loop until race is over
			}
		//end race loop
		}
			// report winner and settle bet
		cout << "The winner is " << winner << " " << horses[winner - 1].name << endl;
		if (winner == hNum)
		{
			cash = cash + bet*6;
			cout << "You won! Your cash is now $" << cash << endl;
		}
		else
		{
			cout << "Better luck next time.\n";
		}
			// if player has cash, get new bet, new horse, and start race
		if (cash <= 0)
		{
			cout << "You're broke!\nGoodbye!\n";
			cin.get();
			return 0;
		}
		//	Wait for user input to close program when debugging.
	}
	cin.get();
	cin.get();
	return 0;
}
//--end of main program-------------
//----------------------------------
Line 203: You've determined the winner and turned the racing flag off, but you continue with your for loop. i.e. You will fall through and execute the if at line 211, then continue with the next iteration of the for loop (if not the last horse). At line 203, you want to break out of the for loop.
Topic archived. No new replies allowed.