473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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;

}


Jul 22 '05 #1
1 1987
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The...
8
by: ding feng | last post by:
I am a beginner. So this question could be very stupid. Would anyone help me to solve this problem? A formatted txt file is read. Then i need to look into a vector who is a member of a class to...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
2
by: Warex | last post by:
Hello, I have 2 questions maybe someone can answer. 1. On a function class when you return an item is it possible to return more than one and how is that done? Currently I am using for one...
9
by: Peri | last post by:
Dear All, I have written a common array class which has insert, search and other functions. For Example (Code in VB.NET): ' Client Class Public Class Client Public ClientCode As String ...
1
SamKL
by: SamKL | last post by:
Hey, I'm no expert on PHP, and I have somewhat of an understanding of object oriented code. Anyway, getting right to the problem. I'm using PHP4, so base it off of that. Basically I have 2...
3
by: Payne | last post by:
Hello, I'm having trouble figuring out how to best explain my problem but I hope I can make myself clear enough. Anyway, I'm doing an assignment for school and in this one we're supposed to write a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.