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{
        int Speed;
       int Position;
       int HorseNumber;
       int Advance();
       void SetHorse(int Speed, int Position,int HorseNumber);

	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(){
    return Position;

class RaceTrack{
        int Length;
        int Lanes;
        int i;//counter
        int j;//counter
        Horse* Head;
        Horse* Temp;
        Horse* Temp2;
        Horse HorseArray[7];
        RaceTrack(int Length, int Lanes);
        void StartRace();


 RaceTrack::RaceTrack(int LengthI, int LanesI){
    i = 0;
    j = 0;
    Horse HorsesArray[7];
    for(i=0; i<Lanes;i++){// School's IDE says memory Exception around here.
//Speed is between 1 and 11, start position is zero, and horse number is i;


void RaceTrack::StartRace(){
    /* 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)){
            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";

        srand(SEED);//Will make seed dependent on time when done debugging.
        if(rand()/RAND_MAX > .5 ) {
        Temp = Temp + 1;
            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);

   return 0;

Edited by Casper3912: n/a

7 Years
Discussion Span
Last Post by VernonDozier

Line 70 - You are going to get a seg fault if i >= 7, so confirm that that never occurs. Very easy to do. Stick this after line 69.

assert(i < 7);

Stick this at the top.

#include <cassert>

If the assertion fails, you'll get a run-time error saying so.


That and some other info I got allowed me to get it working on time, THANKS!
I like the cassert library, better then a bunch of print/cin statements.
How do I tag this thread as solved? I don't see a button anywhere for it.

Edited by Casper3912: n/a


>> That and some other info I got allowed me to get it working on time, THANKS!

You're welcome. Glad you got it working.

>> How do I tag this thread as solved? I don't see a button anywhere for it.

It's not a button. It will just be an html link that you click located below the last post in the thread saying "Mark This Thread As Solved", right below "Has This Thread Been Answered" in purple.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.