J
John J
I'm currently writing 3 classes that will accept race entries for yachts and
perform various functions on them. Below is the code from 2 classes
(Race.cpp and Entry.cpp). I've mostly finished these classes; however, there
is a Race::winner() function that is intended to search for entry objects
and display the winner of a race. I'd appreciate some advice on why my
Race::winner() function is not working correctly.
Thanks for any help.
//Race.cpp
#include <string>
#include "Race.h"
#include "Entry.h"
#include "Yacht.h"
using namespace std;
//Construct a race
Race::Race (int n, string d)
{
number = n;
date = d;
nEntries = 0;
for (int j=0; j<MAX_ENTRIES; j++)
entries[j] = NULL;
}
//Enter a race
void Race::enter_race (Yacht* y, int pl, string ti)
{
if (nEntries<MAX_ENTRIES)
{
Entry* e = new Entry (this,y,pl,ti);
entries[nEntries++] = e;
}
else cout << "Too many entries" << endl;
}
//Find an entry object(s)
void Race::display_entries (ostream& out) const
{
if (nEntries == 0)
out << "No entries" << endl;
else
{
cout << "Details of Yachts entered into the requested race:" <<endl << endl;
for (int i=0; i<nEntries; i++)
out << *(entries);
}
}
//Find the winner
void Race::winner (ostream& out) const
{
Entry* e = e.place;
if (nEntries == 0)
{
out << "No entries" << endl;
}
else
{
for (int i=0; i<nEntries; i++)
if (e->getPlace() == 1)
{
out << *(entries);
}
else
{
cout << "There was no winner in this race" << endl;
}
}
}
//Access functions
int Race::getNumber (void) const
{
return number;
}
string Race::getDate (void) const
{
return date;
}
//Output functions
//Print Race info
void Race::Show (ostream& out) const
{
out << "Race Number : " << number << endl
<< "Race Date : " << date << endl << endl;
}
ostream& operator<< (ostream& out, const Race& r)
{
r.Show (out);
if (r.nEntries == 0)
out << "No entries" << endl;
else
{
for (int i=0; i<r.nEntries; i++)
out << *(r.entries);
}
return out;
}
//Entry.cpp
#include <iostream>
#include <string>
#include "Entry.h"
#include "Race.h"
#include "Yacht.h"
using namespace std;
//Construct an Entry
Entry::Entry (Race* r, Yacht* y, int pl, string ti)
{
which = r;
what = y;
place = pl;
time = ti;
}
//Access functions
int Entry::getPlace () const
{
return place;
}
string Entry::getTime () const
{
return time;
}
Race* Entry::getRace () const
{
return which;
}
Yacht* Entry::getYacht () const
{
return what;
}
//Overload of operator<< for output of an Entry
ostream& operator<< (ostream& out, const Entry& e)
{
out << "Yacht : " << *(e.what) << endl
<< "Race : " << Race::Show << endl
<< "Finish Place : " << e.place << endl
<< "Finish Time : " << e.time << endl << endl;
return out;
}
perform various functions on them. Below is the code from 2 classes
(Race.cpp and Entry.cpp). I've mostly finished these classes; however, there
is a Race::winner() function that is intended to search for entry objects
and display the winner of a race. I'd appreciate some advice on why my
Race::winner() function is not working correctly.
Thanks for any help.
//Race.cpp
#include <string>
#include "Race.h"
#include "Entry.h"
#include "Yacht.h"
using namespace std;
//Construct a race
Race::Race (int n, string d)
{
number = n;
date = d;
nEntries = 0;
for (int j=0; j<MAX_ENTRIES; j++)
entries[j] = NULL;
}
//Enter a race
void Race::enter_race (Yacht* y, int pl, string ti)
{
if (nEntries<MAX_ENTRIES)
{
Entry* e = new Entry (this,y,pl,ti);
entries[nEntries++] = e;
}
else cout << "Too many entries" << endl;
}
//Find an entry object(s)
void Race::display_entries (ostream& out) const
{
if (nEntries == 0)
out << "No entries" << endl;
else
{
cout << "Details of Yachts entered into the requested race:" <<endl << endl;
for (int i=0; i<nEntries; i++)
out << *(entries);
}
}
//Find the winner
void Race::winner (ostream& out) const
{
Entry* e = e.place;
if (nEntries == 0)
{
out << "No entries" << endl;
}
else
{
for (int i=0; i<nEntries; i++)
if (e->getPlace() == 1)
{
out << *(entries);
}
else
{
cout << "There was no winner in this race" << endl;
}
}
}
//Access functions
int Race::getNumber (void) const
{
return number;
}
string Race::getDate (void) const
{
return date;
}
//Output functions
//Print Race info
void Race::Show (ostream& out) const
{
out << "Race Number : " << number << endl
<< "Race Date : " << date << endl << endl;
}
ostream& operator<< (ostream& out, const Race& r)
{
r.Show (out);
if (r.nEntries == 0)
out << "No entries" << endl;
else
{
for (int i=0; i<r.nEntries; i++)
out << *(r.entries);
}
return out;
}
//Entry.cpp
#include <iostream>
#include <string>
#include "Entry.h"
#include "Race.h"
#include "Yacht.h"
using namespace std;
//Construct an Entry
Entry::Entry (Race* r, Yacht* y, int pl, string ti)
{
which = r;
what = y;
place = pl;
time = ti;
}
//Access functions
int Entry::getPlace () const
{
return place;
}
string Entry::getTime () const
{
return time;
}
Race* Entry::getRace () const
{
return which;
}
Yacht* Entry::getYacht () const
{
return what;
}
//Overload of operator<< for output of an Entry
ostream& operator<< (ostream& out, const Entry& e)
{
out << "Yacht : " << *(e.what) << endl
<< "Race : " << Race::Show << endl
<< "Finish Place : " << e.place << endl
<< "Finish Time : " << e.time << endl << endl;
return out;
}