I am in a distributed education programming class, our most recent assignment has me stumped with a error that is driving me insane. The IDE I have on my personal computer is missing some of the libraries I need(I believe), and when I remote desktop to my school's computer and use the IDE there the error code I receive is that there is a memory exception at line: 71
The assignment is to stimulate a race track using Object oriented programing, the area I believe I am having problems with is when I attempt to make an array of Horse objects.
Its less then a hundred lines of code once you take out the comments.
#include <iostream>
#include <cstdlib>
#include <iomanip>
#define SEED 50
using namespace std;
/* The following code stimulates a race track,
It first declars a horse class with the properties of horse number, speed, and position.
The methods the horse class has is Advance(), which advances the position dependent on the horses speed;
and SetHorse() which initializes the horses values after construction(this this is because we need a array of Horses latter on)
Next is the RaceTrack class, with Lanes,Length Horses and pointers to horses and counters.
It has a constructor which initializes everything including creating the array previously mentioned and a method called StartRace() which runs the race.
Main sets it all up with Random inputs, will make the seed dependent on time when I'm done.
*/
class Horse{
private:
int Speed;
public:
int Position;
int HorseNumber;
int Advance();
Horse();
void SetHorse(int Speed, int Position,int HorseNumber);
};
Horse::Horse(){
int Speed=0;
int Position=0;
int HorseNumber=0;
}
void Horse::SetHorse(int SpeedI, int PositionI,int I){
Speed = SpeedI;
Position = PositionI;
HorseNumber = I;
}
int Horse::Advance(){
Position+=Speed;
return Position;
}
class RaceTrack{
private:
int Length;
int Lanes;
int i;//counter
int j;//counter
Horse* Head;
Horse* Temp;
Horse* Temp2;
Horse HorseArray[7];
public:
RaceTrack(int Length, int Lanes);
void StartRace();
};
RaceTrack::RaceTrack(int LengthI, int LanesI){
Length=LengthI;
Lanes=LanesI;
i = 0;
j = 0;
Horse HorsesArray[7];
for(i=0; i<Lanes;i++){// School's IDE says memory Exception around here.
HorsesArray[i].SetHorse((rand()%100+1),0,i);
//Speed is between 1 and 11, start position is zero, and horse number is i;
}
Temp2=Temp=Head=HorseArray;
}
void RaceTrack::StartRace(){
cout.fill('.');
/* while current horse is not at the end, increase horse position if random/rand_max is greater then 50%, show output page by page*/
while((Temp->Position < Length) && (i < Lanes)){
if(Lanes%i==0){
for(j=0;j<Lanes;j++){
cout <<"|"<<setw(Temp2->Position-1)<<Temp2->HorseNumber<<setw(Length-Temp2->Position)<<setiosflags(ios::left)<<"| \n"<<setiosflags(ios::right);
Temp2 = Temp2+1;
}
Temp2 = Head;
cout << "Please hit Enter to continue. \n";
cin.ignore(1);
}
srand(SEED);//Will make seed dependent on time when done debugging.
if(rand()/RAND_MAX > .5 ) {
Temp->Advance();
}
Temp = Temp + 1;
i++;
}
for(j=0;j<Lanes;j++){
cout <<"|"<<setw(Temp2->Position-1)<<Temp2->HorseNumber<<setw(Length-Temp2->Position)<<setiosflags(ios::left)<<"| \n"<<setiosflags(ios::right);
Temp2 = Temp2+1;
}
Temp2 = Head;
cout<< "The winning Horse is: Horse Number:... " << Temp->HorseNumber <<"!! \n";
/*Ties are impossible by how Position is evaluated, first horse evaluated to have position greater then Length is the winner.*/
cout.fill(' ');
}
int main() {
srand(SEED);//Will make seed dependent on time when done debugging.
int Length = rand()%10 + 11; //Random number between 11 and 110;
int Lanes = rand()%100/2 + 2; // Random Number between 2 and 7;
RaceTrack TheRace(Length,Lanes);
TheRace.StartRace();
return 0;
}