I have an assignment to derive a Runner class from a Period class. A Period consists of hours(int), minutes(int), and seconds(float) and is in the format of hh:mm:ss.s. A Runner is a Period with a name. We've been given this main function and we have to write the two classes to make it work properly.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int main( ){
Runner runners[100];
int count;
ifstream fin;
fin.open("f:\\TOU.txt");
fin >> count;
for (int n=0; n<count; n++)
fin >> runners[n];
Sort (runners, count);
cout << " NAME TIME OFF PACE\n";
for (int n=0; n<count; n++)
cout << runners[n] << runners[0] – runners[n] << endl;
}
|
I have the input and output operators overloaded right, but I can't seem to get that minus operator to subtract the two Runners, but only display the Period. My overloaded output for Runners makes it output the name, then a tab, then the Period.
This is the in-class overloaded minus that I tried.
1 2 3 4 5 6 7
|
Runner& operator-(const Runner&); //in Public of the Runner class
Runner& Runner::operator-(const Runner& r){
this->setTotal(getTotal() - r.getTotal());
this->totalConvert();
return *this;
}
|
And here's the stand alone one that I tried. The if statement is just how I'm going to display the difference as a negative, since I don't want to let a Period be negative on its own. I'll just have the output function put a negative on the front if neg==true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Period operator-(Runner a, Runner b);
Period operator-(Runner a, Runner b){
Period temp;
if(a.getTotal()>b.getTotal()){
temp.setTotal(a.getTotal() - b.getTotal());
}
else if(b.getTotal()>a.getTotal()){
temp.setTotal(b.getTotal() - a.getTotal());
neg=true;
}
else
temp.setTotal(0);
temp.totalConvert();
return temp;
}
|
The getTotal function converts the hours, minutes, and seconds into seconds as one big float. The totalConvert puts it back into hours, minutes, and seconds so I don't have to worry about converting it during the subtraction.
Even just a hint at what I might be doing wrong would be awesome so that you're not just giving me the answer. I do actually want to learn it, but I have no idea why I can't even get passed the syntax error on the subtraction in main().
The file that it reads in is this.
5
Fred Smith 3:21:19
Jane Doe 3:45:58
Sam Black 2:59:14
Dan Green 4:10:21
Bob Jones 3:58:18 |
If you need to see any other parts (or all) of my code, let me know.