Horse Racing Program Help

Hello, I am writing a simple horse racing code for my Programming 1 class. I have everything done but cannot get the winner to be displayed at the end correctly. That is all I need and I just cant seem to get it right.

Code:
#include<iostream>
#include<string>
#include<time.h> // two libraries needed to generate random numbers
#include<stdlib.h>

using namespace std;

int position(int pos, char horse)
{
cout << horse << ": ";
pos = pos + rand() % 2 + 1; // this code will generate a random number between 1 and 2
for(int b=0;b<=pos;b++)
cout << " ";
cout << "~n-n^";
cout << endl << endl;




return pos;
}

int main()
{
int posA=0, posB=0, posC=0, posD=0;


char horse;

srand(time(NULL)); // this line of code ensures that our program will generate a new random number each time we ask for it


for(int a=0;a<=25;a++)
{
system("cls");

horse='A';
posA=position(posA,horse);



horse='B';
posB=position(posB,horse);



horse='C';
posC=position(posC,horse);



horse='D';
posD=position(posD,horse);



for(int timer=0;timer<=100000000;timer++);
}

int highest=0;
int winner[] = {posA, posB, posC, posD};
for(int p=0;p<=3;p++)
{
cout << winner[p] << endl;
if(winner[p]>winner[highest])
highest=p;
}
cout << "Winning Horse: " << highest << endl; //<------ Problem Area---









system("pause");
return 0;
}

Can someone shed some light on this problem. It is probably something easy to fix but I cant seem to get it. Thanks!
I believe I can help...
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<string>
#include<time.h>///two libraries needed to generate random numbers
#include<stdlib.h>
using namespace std;

int position(int pos, char horse)
{
cout << horse << ": ";
pos = pos + rand() % 2 + 1; /// this code will generate a random number between 1 and 2
for(int b=0;b<=pos;b++)
cout << " ";
cout << "~n-n^";
cout << endl << endl;
return pos;
}

int main()
{
int posA=0, posB=0, posC=0, posD=0;
char horse,horsename,bethorse;
double betcash=0;
cout<<"Who do you think s going to win?\nPlace your Bet and find out!\n> $";
cin>>betcash;
cout<<"On what horse? A, B, C, or D?\n> ";
cin>>bethorse;
if(bethorse=='a'){bethorse='A';}
if(bethorse=='b'){bethorse='B';}
if(bethorse=='c'){bethorse='C';}
if(bethorse=='d'){bethorse='D';}

srand(time(NULL)); /// this line of code ensures that our program will generate a new random number each time we ask for it

for(int a=0;a<=25;a++)
{
system("cls");

horse='A';
posA=position(posA,horse);
horse='B';
posB=position(posB,horse);
horse='C';
posC=position(posC,horse);
horse='D';
posD=position(posD,horse);
for(int timer=0;timer<=100000000;timer++);
}

int highest=0;
int winner[] = {posA, posB, posC, posD};


for(int p=0;p<=3;p++)
{
cout<<winner[p] << endl;
if(winner[p]>winner[highest]) ///Your Problem is here all answers are 2.
highest=p;
}
switch(highest){
case 1:
horsename='A';
break;
case 2:
horsename='B';
break;
case 3:
horsename='C';
break;
case 4:
horsename='D';
break;
}
cout<<"Winning Horse: Horse "<<horsename<<highest<<endl;
if(horsename==bethorse){cout<<"You Won! You Get triple times the money you paid.";}
else cout<<"You Lost! All the money that you just bet is now mine!";

system("pause");
return 0;
}
Last edited on
Ok so I modified my code and added your suggestions. Now it is saying that the winning horse is A all the time.
I know that is because the problem is where I said it is, I didn't change it, I modified it so it would be easier to find.
Last edited on
Problem solved! You are very welcome!
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
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
#include<iostream>
#include<string>
#include<ctime>///two libraries needed to generate random numbers This is uneeded, you could just use srand(rand());
#include<cstdlib>
#include<cctype>
using namespace std;

int position(int pos, char horse)
{
cout << horse << ": ";
pos = pos + rand() % 2 + 1; /// this code will generate a random number between 1 and 2
for(int b=0;b<=pos;b++)
cout << " ";
cout << "~n-n^";
cout << endl << endl;
return pos;
}

int main()
{
int posA=0, posB=0, posC=0, posD=0;
char horse,horsename,bethorse;
double betcash=0;
cout<<"Who do you think is going to win?\nPlace your Bet and find out!\n> $";
cin>>betcash;
cout<<"On what horse? A, B, C, or D?\n> ";
cin>>bethorse;
if(bethorse=='a'){bethorse='A';}
if(bethorse=='b'){bethorse='B';}
if(bethorse=='c'){bethorse='C';}
if(bethorse=='d'){bethorse='D';}

srand(time(NULL)); /// this line of code ensures that our program will generate a new random number each time we ask for it

for(int a=0;a<=25;a++)
{
system("cls");

horse='A';
posA=position(posA,horse);
horse='B';
posB=position(posB,horse);
horse='C';
posC=position(posC,horse);
horse='D';
posD=position(posD,horse);
for(int timer=0;timer<=100000000;timer++);
}

int highest=0;
int winner[] = {posA, posB, posC, posD};


for(int p=0;p<=3;p++)
{
cout<<winner[p] << endl;
if(winner[p]>winner[highest]) ///Your Problem is here all answers are 2.
highest=p;
}
switch(highest){
case 0:
horsename='A';
break;
case 1:
horsename='B';
break;
case 2:
horsename='C';
break;
case 3:
horsename='D';
break;
}
cout<<"Winning Horse: Horse "<<horsename<<endl;
if(horsename==bethorse){cout<<"You Won! You Get triple times the money you paid."<<endl;}
else cout<<"You Lost! Now your $"<<betcash<<" that you just bet is now mine!"<<endl;

system("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.