I'm having an issue in creating my "Horse Race" program. I'm getting error "subscript requires array or pointer type" in my while(winner==false) loop at

if(q==null)
{
    tick[q]++;
} 

and

for(int q=0; q<numhor; q++)
{
    if(tick[q]==10)
    {
    ...
    }
}

in addition to several places in my separate class files, even though I defined the array the beginning of the main.

int main()
{
    srand(time(NULL));
    int numhor=0, lane=0, null=0, tick=0, winlane=0, x=0, name=0;
    int tick[10];
    bool winner=false;
    string name;
    Horse HorseRace[10];
    Track TrackRace[10];
    cout<<"How many horses will you race? ";
    cin>>numhor;
    for(int n=0; n<numhor; n++)
    {
        //cin.ignore();
        system("cls");
        x=0;
        cout<<"Horse #"<<n+1<<endl;
        cout<<"Name the horse: ";
        cin>>name;
        HorseRace[n].nameHorse(name);
        //cout<<"Place this horse in lane: ";
        //cin>>lane;
        system("cls");
        HorseRace[n].lane(lane);
        TrackRace[n].fillTrack(HorseRace[n], lane);
    }
    system("cls");
    while(winner==false)
    {
        system("cls");
        //null=returnout(numhor);
        for(int q=0; q<10; q++) //increases tick number
        {
            if(q==null)
            {
                tick[q]++;
            }
        }
        for(int q=0; q<10; q++) //display tick
        {
            for(int r=0; r<10; r++)
            {
                cout<<" *";
            }
        }
        for(int q=0; q<numhor; q++) //check for winner
        {
            if(tick[q]==10)
            {
                winlane=q;
                winner=true;
                break;
            }
        }
        time();
    }
    for(int w=0; w<10; w++)
    {
        if(HorseRace[w].getlane()==winlane)
        {
            cout<<HorseRace[w].nameHorse(name)<<" wins!"<<endl;
        }
    }
    getch();
}

Header:

#include<string>
using namespace std;
#ifndef _horse_
#define _horse_

class Horse
{
public:
    //constructor
    Horse();
    //destructor
    ~Horse();
    //functions
    void lane(int); //lane of the horse
    int getlane(); //return horse's lane
    int nameHorse(int); //names the horse

private:
    int victory; //number of wins
    bool winlose; //returns whether horse is winner or not
    bool finish[10]; //record of place in last 10 races
    int name; //horse name
    int position; //lane on the track
    int finishrecord;
    int horsename;
};

#endif

#ifndef _track_
#define _track_

class Track
{
public:
    //constructor
    Track();
    Track(int); //default constructor for the track
    //Track(Horse [], int); //overloaded constructor placing
    //destructor
    ~Track();
    //functions
    void fillTrack(Horse, int); //put horses on the track
    int countRacers(Horse); //returns the number of horses in the race
private:
    Horse track[8]; //represents the 'lanes' of the track
    int horseCount; //number of horses in the race
    int laneoccupy;
};

#endif

HorseClass:

#include"HorseRace.h"
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

Horse::~Horse()
{
    getch();
}

Horse::Horse()
{
    //victory=0;
    position=0;
    winlose=false;
}

    void Horse::lane(int x)
    {
        position=x;
    }

    int Horse::getlane()
    {
        return position;
    }

    int Horse::nameHorse(int name)
    {
        return name;
    }

TrackClass:

#include"HorseRace.h"
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

Track::~Track()
{
    getch();
}

Track::Track()
{
    horseCount=0;
    for(int z=0; z<10; z++)
    {
        laneoccupy[z]=0;
    }
}

Track::Track(int run)
{
    horseCount=run;
    for(int z=0; z<10; z++)
    {
        laneoccupy[z]=0;
    }
}

    void Track::fillTrack(Horse HorseRace, int lane)
    {
        laneoccupy[lane]=true;
        horseCount++;
    }

    int Track::countRacers(Horse HorseRace)
    {
        return horseCount;
    }

I somehow managed to fix the error by retyping the main code again, and the class errors were due to not defining the subscript in the header file.

However, after running the code, I realized the code does not work like I wanted it to. I could use some assistance on how to make this "Horse Race" just by simply running a horse by the inputted name and asterisks running across the screen one-by-one until they hit a certain value (I'd be doing 20), in which the code would declare a winner (which should already be programmed).

As it is:

#include"HorseRace.h"
#include<iostream>
#include<conio.h>
#include<ctime>
#include<string>
using namespace std;

int main()
{
    srand(time(NULL));
    int numhor=0, lane=0, runner=0, winlane=0;
    int tick[10];
    bool winner=false;
    string name;
    Horse HorseRace[10];
    Track TrackRace[10];
    cout<<"How many horses will you race? ";
    cin>>numhor;
    numhor=numhor+1;
    for(int n=0; n<numhor; n++)
    {
        system("cls");
        cout<<"Horse #"<<n<<endl;
        cout<<"Name the horse: ";
        getline(cin,name);
        int rename= atoi(name.c_str());
        HorseRace[n].nameHorse(rename);
        system("cls");
        HorseRace[n].lane(lane);
        TrackRace[n].fillTrack(HorseRace[n], lane);
    }
    system("cls");
    while(winner==false)
    {
        //code for running horses
    }
    for(int w=0; w<10; w++)
    {
        if(HorseRace[w].getlane()==winlane)
        {
            cout<<HorseRace[w].getname()<<" wins!"<<endl;
        }
    }
}

In my header file, the Horse class contains:

int name; //horse name
int position;

and Track class contains:

Horse track[10]; //lanes of track
int horseCount; //number of horses
int laneoccupy[10];

and the classes are the same as my earlier posts.

Edited 3 Years Ago by ssh9411

This article has been dead for over six months. Start a new discussion instead.