Class search function



 DEVELOP > c-Plus-Plus > Class search function

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "John J"
Date: 08 May 2004 08:06:58 AM
Object: Class search function
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;
}
.

User: "Victor Bazarov"

Title: Re: Class search function 08 May 2004 09:25:52 AM
"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
.


  Page 1 of 1

1

 


Related Articles
Newbie Question: Function of a class keeps "forgetting" data thats declared inside class
What will happen if a virtual function is declared to be virtual again in a derived class?
address-of a template class template member function
calling virtual function results in calling function of base class...
use class member function as a parameter
template class member function specialization
passing class to a function in c++ - performance question
Class template parameter that is a function
Re: base class destructors and unary_function
Class template and static member function
How can I call class member function by a globle function pointer?
How to pass a class-member function to pthread_create(....)?
Problem with member function of a templated class returning a pointer to an object of the same class
Cast from void* to base class virtual function problem
function vs. class partial specialization
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER