By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

Class search function

P: n/a
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[i]);

}

}

//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[i]);

}

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[i]);

}

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;

}


Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"John J" <...@...> wrote...
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.

[...]
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[i]);

}

else

{

cout << "There was no winner in this race" << endl;

}

}

}
[...]


Let me format it for you...

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[i]);
}
else
{
cout << "There was no winner in this race" << endl;
}
}
}

(it is quite possible that you have it formatted similarly, but then
you really need to think of using a better news posting service, one
that allows you to retain formatting of your message)

Now, let's analyse the function. The very first statement is

Entry* e = e.place;

What's that supposed to mean? What are you trying to achieve by doing
that? As soon as you can answer this question, you're going to be
closer to finding a solution.

Now, next is the 'else' part of the first 'if'. You have a loop in
it that goes from 0 to nEntries. In it you have

if (e->getPlace() == 1)
...

What's "e" here? If you want to iterate through _all_ entries,
shouldn't "e" change on every iteration?

Now, the last thing: if you found the winner (the one with place == 1),
shouldn't you be stopping the iterations right after printing it out?
I would add

return;

after

out << "No entries" << endl;

(and, BTW, do you really want it to be "out" and not "cout"? If so,
do you really want to output "no winner" to "cout" and not "out"?)

And only print "no winner" at the end of the function, after the for
loop closes.

I think it's time for you to pose some questions to the person who
wrote it for you (if that's you yourself, start asking yourself some
questions). Try to figure out what EVERY statement does and what it
is for. If you can't figure that out, remove the statement, it's
probably something you don't need.

Victor
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.