473,805 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_rac e (Yacht* y, int pl, string ti)

{

if (nEntries<MAX_E NTRIES)

{

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_e ntries (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 2005
"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
3005
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 updated proposal is at: www.python.org/peps/pep-0322.html In a nutshell, it proposes a builtin function that greatly simplifies reverse iteration. The core concept is that clarity comes from specifying a sequence in a forward direction and...
8
2092
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 search the matched string and return this string. For example, this vector has (string1, string2, ...stringn). When the formatted txt file is read, I get string j which could be a member of the above vector. This vector is a private member of...
2
2045
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" facility. I am to get rid of them and replace them with exceptions. I need to create a "linkedListException" class that's declared and implemented in my "linkedListType" class. This class needs to inherit from the base "exception" class and return...
20
2896
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
8698
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
2905
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" /////////////////////////////// source code /////////////////////////////// #include<iostream> #include <string> #include<vector> #include<sstream>
2
1197
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 Avariable = MyFunction() 2. In vb express is it possible to create an application that will allow a
9
1507
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 'Key Field
1
2572
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 classes in this code. Link_Structure, which is composed of Node classes (it's a linked list data structure), and of course the Node class. In class Node: function setActive( $i ) { $this->_data = $i; } This function is designed to set the array...
3
1517
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 simple "database" class. The interface of this database class supports adding items, removing items, obtaining information about how many items are stored etc. In the assignment the database is supposed to support only storing items of a...
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10607
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.