Class search function

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;

}
 
V

Victor Bazarov

John J said:
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);

}

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);
}
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Associated classes 3
Unexpected Result 8
Search function 3
Search function 0
Calling a function in another class 2
not null 36
Need help in debugging tic tac toe (Beginner) 0
Character operations in C++ 2

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top