Help with a horse racing sim?

I am a pretty new C++ developer and I am working on a text-based horse racing sim where the user puts in the acceleration, endurance, and speed of six horses. I want it to be where the one with the best stats is most likely the winner, but it another can still win just like in real sports. Below is my code so far. Keep in mind I am new so it might be a bit messy. Any help is appreciated, especially with the actual deciding of the winner. Thanks to anyone who helps.

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

using namespace std;

int main()
{
int acc;
int end;
int speed;
int horse1acc;
int horse1end;
int horse1speed;
int horse2acc;
int horse2end;
int horse2speed;
int horse3acc;
int horse3end;
int horse3speed;
int horse4acc;
int horse4end;
int horse4speed;
int horse5acc;
int horse5end;
int horse5speed;
int horse6acc;
int horse6end;
int horse6speed;
int winner;

cout << "Enter acceleration of horse 1: ";
cin >> horse1acc;

cout << "Enter endurance of horse 1: ";
cin >> horse1end;

cout << "Enter speed of horse 1: ";
cin >> horse1speed;

cout << "Enter acceleration of horse 2: ";
cin >> horse2acc;

cout << "Enter endurance of horse 2: ";
cin >> horse2end;

cout << "Enter speed of horse 2: ";
cin >> horse2speed;

cout << "Enter acceleration of horse 3: ";
cin >> horse3acc;

cout << "Enter endurance of horse 3: ";
cin >> horse3end;

cout << "Enter speed of horse 3: ";
cin >> horse3speed;

cout << "Enter acceleration of horse 4: ";
cin >> horse4acc;

cout << "Enter endurance of horse 4: ";
cin >> horse4end;

cout << "Enter speed of horse 4: ";
cin >> horse4speed;

cout << "Enter acceleration of horse 5: ";
cin >> horse5acc;

cout << "Enter endurance of horse 5: ";
cin >> horse5end;

cout << "Enter speed of horse 5: ";
cin >> horse5speed;

cout << "Enter acceleration of horse 6: ";
cin >> horse6acc;

cout << "Enter endurance of horse 6: ";
cin >> horse6end;

cout << "Enter speed of horse 6: ";
cin >> horse6speed;


return 0;
}
Last edited on
I don't see a question...
I recomend you create a horse class for storing their stats.
When you have something like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int horse1acc;
int horse1end;
int horse1speed;
int horse2acc;
int horse2end;
int horse2speed;
int horse3acc;
int horse3end;
int horse3speed;
int horse4acc;
int horse4end;
int horse4speed;
int horse5acc;
int horse5end;
int horse5speed;
int horse6acc;
int horse6end;
int horse6speed;

...then you could well use a std::vector and a struct:
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
#include <vector>
#include <iostream>

// define what goes into a new type horse
struct horse
{
    int acc;
    int end;
    int speed;
};

int main()
{
    // an expandable list of elements of type horse
    std::vector<horse> horses;

    // add a new horse:

    horse h; // create a horse called h

    h.acc = 2;
    h.end = 4;
    h.speed = 1;

    // copy that horse into the vector
    horses.push_back(h); // makes vector larger

    h.acc = 3;
    h.end = 2;
    h.speed = 2;

    horses.push_back(h); // makes vector larger

    // display horse speeds:
    for(size_t i = 0; i < horses.size(); ++i)
    {
        std::cout << i << ": " << horses[i].speed << '\n';
    }
}


Structs:
http://cplusplus.com/doc/tutorial/structures/

Vectors
http://cplusplus.com/reference/stl/vector/
Last edited on
I agree with LBEaston, a horse class would be a good idea. If you don't know how to create classes, look here:

http://www.cplusplus.com/doc/tutorial/classes/

If you have any questions, ask :)

So here I have some fine tuned code that I had some help making.

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

// struct - this is a data type, a group of related variables
struct horse_struct
{
   int acc;
   int end;
   int speed;
};

int main()
{
   // make an array (effectively a list) of type horse_struct.
   // So, we have six "horses", each having a copy of the
   // variables within horse_struct
   horse_struct horses[6];
   int winner;

   // This is a basic for-loop, it starts at t=0, and each
   // time it loops around it increases t by 1 (the t++ bit)
   // until t<6 is no longer true.  Then it exits the loop.
   // Here, it will look at all 6 horses
   for (int t = 0; t < 6; t++)
   {
      // cout the text name, the << means we can cout an extra
      // piece of data - the "t" giving the horse number
      cout << "Enter acceleration of horse "<< t << ": ";

      // horses[t] means the horse at slot t of the array (e.g.
      // horses[0] gives you the first horse.  the ".acc" lets
      // you set the associated variable in the horse_struct
      cin >> horses[t].acc;

      // As above, but for the other variables
      cout << "Enter endurance of horse " << t << ": ";
      cin >> horses[t].end;

      cout << "Enter speed of horse " << t << ": ";
      cin >> horses[t].speed;
   }
return 0;
}


I really need some coding the randomization and deciding parts so that the horse with the best starting stats is most likely to win, but can still lose.
Last edited on
So basically you want the entire program? Oo wtf man...try some of it yourself now that you have a base.
I'm trying to have a random number function so an underdog can win like in reals sports hpwever I get massive errors with the code I wrote below. I'm sure I made a beginner mistake but I'm not sure what and I'm still working on fixing it.

1
2
3
int performance=horses[t].acc+horses[t].end+horses[t].speed;
int variable=rand()%100+1;;
int winner=performance+variable;
Last edited on
What errors?
Here is all my code so far.


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
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

// struct - this is a data type, a group of related variables
struct horse_struct
{
   int acc;
   int end;
   int speed;
}

int performance=horses[t].acc+horses[t].end+horses[t].speed;
int variable=rand()%100+1;;
int winner=performance+variable;


int main()
{
   // make an array (effectively a list) of type horse_struct.
   // So, we have six "horses", each having a copy of the
   // variables within horse_struct
   horse_struct horses[6];
   int winner;

   // This is a basic for-loop, it starts at t=0, and each
   // time it loops around it increases t by 1 (the t++ bit)
   // until t<6 is no longer true.  Then it exits the loop.
   // Here, it will look at all 6 horses
   for (int t = 0; t < 6; t++)
   {
      // cout the text name, the << means we can cout an extra
      // piece of data - the "t" giving the horse number
      cout << "Enter acceleration of horse "<< t << ": ";

      // horses[t] means the horse at slot t of the array (e.g.
      // horses[0] gives you the first horse.  the ".acc" lets
      // you set the associated variable in the horse_struct
      cin >> horses[t].acc;

      // As above, but for the other variables
      cout << "Enter endurance of horse " << t << ": ";
      cin >> horses[t].end;

      cout << "Enter speed of horse " << t << ": ";
      cin >> horses[t].speed;

      cout << "The winner is:" << winner <<".";
   }
return 0;
}


I now get 7 errors on line 14 and one on line 16.

Here is what my compiler (Code:Blocks) says.

C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|14|error: two or more data types in declaration of 'performance'|
C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|14|error: 'horses' was not declared in this scope|
C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|14|error: 't' was not declared in this scope|
C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|14|error: 'horses' was not declared in this scope|
C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|14|error: 't' was not declared in this scope|
C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|14|error: 'horses' was not declared in this scope|
C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|14|error: 't' was not declared in this scope|
C:\Users\Steven\Documents\Code Blocks\Horse Racing Sim 2\main.cpp|16|error: 'performance' was not declared in this scope|
||=== Build finished: 8 errors, 0 warnings ===|


What am I doing wrong? I'm sure it's a dumb mistake but I don't know what.
Last edited on
First, put a semi-colon after the closing brackets of the horse_struct struct.
Second problem - your variable is a constant. You want a new random variable for each horse.
Third - you need performance to be a function:

int performance (int t,horse_struct* horses)
{return horses[t].acc+horses[t].end+horses[t].speed;}

You could make it a member function of the horse class.
Last edited on
Finally (I think) you need winner to be a function that looks at the performance of each horse and returns the number of the horse with the best number
dont forget the weight of the jockey for even better results!
Besides jockey, the distance of the run, the ground condition, weather is important too. Some horses are built to run short race just look at their physique! Some horses are trained in fine weather and if say rain fell on the race-day the finishing is very often skewed :P
Topic archived. No new replies allowed.