Horse race program is giving error. Need help to run this program. I included all the 3 class files and the main files in it.

closed account (jhbSzwbp)

#include "Horserun.cpp"
#include "Random.cpp"

Horse::Horse(std::string _name)
:name(_name)
,distance_covered(0)
{
}

void Horse::run()
{
// increment the disnatnce randomly between 0-1
distance_covered += Random::randint(Random::MAX_SEED_RUN);
}

int Horse::get_distance_covered()
{
return distance_covered;
}

bool Horse::is_race_won()
{
if (distance_covered >= Random::WINING_DISTANCE)
{
return true;
}
else
{
return false;
}
}

std::string Horse::get_distance_str()
{
std::string retval;
for (uint32_t cnt = 0; cnt < distance_covered; cnt++)
{
retval += "=";
}
retval += " " + name;
if (distance_covered == Random::WINING_DISTANCE)
{
retval += " is the Winner!";
}
return retval;
}







#include <string>

class Horse {
public :
// public method declaration
// c'tor
Horse(std::string _name);

// run method
void run();

// distance covered
int get_distance_covered();

// get distnace string
std::string get_distance_str();

// check if horse has croosed the winnning limit
bool is_race_won();

public :
// public member declaration
std::string name;

private :
int distance_covered;
};









#include <iostream>
#include <cstdlib>
#include <time.h>

class Random
{
public :
static void seed()
{
std::srand(time(0));
}

static int randint(uint32_t max)
{
return std::rand() % (max - 0);
}
static const uint32_t WINING_DISTANCE = 20;
static const uint32_t MAX_SEED_RUN = 2;
static const int HALF_SECOND = 500;
};






#include <iostream>
#include <windows.h>
#include <string>

#include "Random.cpp"
#include "Horserun.cpp"

const std::string BANNER =
"==============\n\
= Horse Race =\n\
==============\n\
";

const int NUM_HORSE_TO_RACE = 5;

int main(int argc, char* arg[])
{
// set title - Change name here
system("Horse Race");

bool noWinners = true;

// call function to generate seed
Random::seed();

// create an array of horse to be raced
Horse * racing_horses[NUM_HORSE_TO_RACE];
for (uint32_t idx = 0; idx < NUM_HORSE_TO_RACE; idx++)
{
if (0 == idx)
{
racing_horses[idx] = new Horse("ShadowFox");
}
else if (1 == idx)
{
racing_horses[idx] = new Horse("Epona");
}
else if (2 == idx)
{
racing_horses[idx] = new Horse("Roach");
}
else if (3 == idx)
{
racing_horses[idx] = new Horse("Pinkie Pie");
}
else if (4 == idx)
{
racing_horses[idx] = new Horse("Wildfire");
}
}

while (noWinners)
{
// print banner
std::cout << BANNER << std::endl;
std::cout << "====================" << std::endl;
for (uint32_t idx = 0; idx < NUM_HORSE_TO_RACE; idx++)
{
racing_horses[idx]->run();
std::cout << racing_horses[idx]->get_distance_str() << std::endl;
noWinners = noWinners && !racing_horses[idx]->is_race_won();
}
std::cout << "====================" << std::endl;
Sleep(Random::HALF_SECOND);
if (noWinners) {
system("cls");
}
}
// delete allocated array
for (uint32_t idx = 0; idx < NUM_HORSE_TO_RACE; idx++)
{
delete racing_horses[idx];
}
return 0;
}

Last edited on
1.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either


2. What do you mean when you say "not compiling?" Are you getting errors? What are those errors?

Simply saying "not compiling" doesn't help us to be able to help you.

3. Do NOT include .cpp files. Create a header for that source file and include that instead.

https://cplusplus.com/forum/general/39618/#msg213812
To rephrase George P's #3:
3. NEVER #include a .cpp file. This can cause multiply defined symbols at link time. Your header files should be .h files.

PLEASE EDIT YOUR POST AND APPLY CODE TAGS.


Whoa, there's an echo in here! :Þ
I didn't think your emphasis was strong enough. :)
Nor did you give an explanation of why not to do it.
Last edited on
Another problem is writing 150 lines in one session, only to discover that it doesn't work.

Start small, compile often (like every 5 lines) and test often.

Like you make sure it works for one horse before copy/pasting your way to 10 of them.
Nor did you give an explanation of why not to do it.

Hmmmmm, then why is there a link?

Read it and there is mention of "multiple definitions".
Your header files should be .h files.


Or .hpp files (or whatever extension is used) when they contain more then just definitions such as templated code.

Note that an include file can have any name. It's the content that's important - not the name. Although there are naming extension conventions (.h, .hpp etc) these are just conventions. Although having a proper include file named .cpp is allowed, it's not convention and will confuse a lot of people and is not good practice.

Also, there is no reason why an include file has to just contain 'header/template etc" code. An include statement can occur anywhere in the code and contain any valid statements that are to be included at that point by the pre-compiler. Although again using include in this manner isn't good convention and would hopefully be frowned upon if found in 'production code'.

Topic archived. No new replies allowed.