-
Horse Race Program help
Hello. I'm a high school student (Jr.) taking C++ (I'm pretty new to it).
I have to do a horse race program that's due tonight at 12am.
I have the horse race portion down pat but I'm very confused with how to show the places of the horses when the race is complete. Any help would be appreciated. Here's the program (the place function is what I made so far to try and display the places of the horses):
Code:
#include<iostream.h>
#include<conio.h>
#include<lvp\random.h>
void drawtrack()
{
clrscr();
cout<<" ------------------------------------------------------------"<<endl;
cout<<"1|------------------------------------------------------------|"<<endl;
cout<<"2|------------------------------------------------------------|"<<endl;
cout<<"3|------------------------------------------------------------|"<<endl;
cout<<"4|------------------------------------------------------------|"<<endl;
cout<<" ------------------------------------------------------------"<<endl;
}
void changehorse(int horsenum, int y)
{
gotoxy(y+2,horsenum+1);
cout<<"-H"<<endl;
}
void movehorse(int horsenum,int &y)
{
if(1+random(100)>50 && y<60) {
changehorse(horsenum,y);
y++;
}
}
void place()
{
int p,y,horse1,horse2,horse3,horse4,horsenum;
if(horse1=60)
cout<<"horse 1: 1st place"<<endl;
else(horse1<60);
if(horse2=60)
cout<<"horse 2: 1st place"<<endl;
else(horse2<60);
if(horse3=60)
cout<<"horse 3: 1st place"<<endl;
else(horse3<60);
if(horse4=60)
cout<<"horse 4: 1st place"<<endl;
else(horse4<60);
}
void main()
{
clrscr();
randomize();
drawtrack();
place();
int horse1=1;
int horse2=1;
int horse3=1;
int horse4=1;
while(horse1<60||horse2<60||horse3<60||horse4<60)
{
movehorse(1,horse1);
movehorse(2,horse2);
movehorse(3,horse3);
movehorse(4,horse4);
for(long pp=1;pp<=10000000;pp++);
}
}
-
In your place function, you have new local variables for all the horses. You should pass the values that you have in main to the place function as arguments and use those instead. Then you will want to use == instead of = to compare. Finally, to find which horse has first place you can compare against 60, but to find which one has second place, you have to find the one that is greater than the others but less than the first place horse. Then for third and fourth you just compare the last two horses. This will probably take a complicated group of ifs and elses.
One last thing, you will want to call place() near the end of the program, after the horses have completed the race.
-
Ewwww.
Code:
#include<iostream.h>
Ewwww.
Code:
#include <iostream>
Ewwww. Use something standard.
-
Code:
for(long pp=1;pp<=10000000;pp++);
not a good way to wait - eats to much CPU
search forum for portable sleep implementations... or use nonpotrable/ if you are using other non-portable things anyway