473,406 Members | 2,549 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,406 software developers and data experts.

Trouble with find() template

The files I'm referring to can be found here:
http://www.moxybox.com/programming

After going through this in debugger numerous times, I can't figure out
why this code continues to return false:

template<class T>
bool Database<T>::find(const T& d) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
database.close();
return true;
}
}
database.close();
return false;
}

In short, it should be comparing the social security number of both tmp
and d. In debugger, the correct data is read into both but it
continuously returns false.

I attempted to change the line

if (tmp == d) {
to
if (tmp.SSN == d.SSN) {

just to ensure that my thoughts were correct. When running this in
debugger, it shows that the correct SSN is being pulled from tmp and
compared to the entered SSN d; however, the tmp.SSN variable has what I
call "garbage" at the end and I believe this is keeping it from
comparing correctly.

Please let me know if I am completely off on my thoughts here, or if
I'm on target, how I can go about removing the "garbage" at the end of
tmp.SSN to make it compare correctly.

Jan 6 '07 #1
5 1454
On 6 Jan 2007 13:52:15 -0800, "zaie" <sa*******@gmail.comwrote:
>The files I'm referring to can be found here:
http://www.moxybox.com/programming

After going through this in debugger numerous times, I can't figure out
why this code continues to return false:

template<class T>
bool Database<T>::find(const T& d) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);
if (tmp == d) { // overloaded ==
database.close();
return true;
}
}
database.close();
return false;
}

In short, it should be comparing the social security number of both tmp
and d. In debugger, the correct data is read into both but it
continuously returns false.

I attempted to change the line

if (tmp == d) {
to
if (tmp.SSN == d.SSN) {

just to ensure that my thoughts were correct. When running this in
debugger, it shows that the correct SSN is being pulled from tmp and
compared to the entered SSN d; however, the tmp.SSN variable has what I
call "garbage" at the end and I believe this is keeping it from
comparing correctly.
Then you probably need to check what is going on inside the method
tmp.readFromFile(database) since that is presumably where the garbage
is getting put into the tmp.SSN field. Are you missing a terminating
'\0' or similar somehwere?
>Please let me know if I am completely off on my thoughts here, or if
I'm on target, how I can go about removing the "garbage" at the end of
tmp.SSN to make it compare correctly.
Better to ensure that it never gets there is the first place, but you
could use the fact that SSN is a fixed number of digits to check that
what you have in the SSN field is the right number of digits and not
garbage.

rossum
Jan 6 '07 #2

rossum wrote:
Then you probably need to check what is going on inside the method
tmp.readFromFile(database) since that is presumably where the garbage
is getting put into the tmp.SSN field. Are you missing a terminating
'\0' or similar somehwere?
Please let me know if I am completely off on my thoughts here, or if
I'm on target, how I can go about removing the "garbage" at the end of
tmp.SSN to make it compare correctly.
Better to ensure that it never gets there is the first place, but you
could use the fact that SSN is a fixed number of digits to check that
what you have in the SSN field is the right number of digits and not
garbage.

rossum
The SSN variable is allocated as char SSN[10], which has something to
do with the garbage on the end of tmp.SSN but I'm not sure exactly
what. I always assumed that the extra space was for the terminating
character /0. Please correct me if I'm wrong.

As for using the fixed number of digits to check that SSN isn't full of
garbage, I can see when I run debugger that the SSN is inherently
correct, just followed by some garbage at the end. I am trying to
figure out a way to pull the 9 digits from tmp and d, assign them to
two seperate variables, and compare them that way.

However, when I attempt to do this I run into the error:
error C2106: '=' : left operand must be l-value

and the only other way I can see of doing it is to convert them to a
string, which seems much more bulky than necessary. But perhaps not.
(I am relatively new at this, please forgive me if my logic is terribly
off.) Any ideas?

Jan 6 '07 #3
On 6 Jan 2007 14:35:28 -0800, "zaie" <sa*******@gmail.comwrote:
>
rossum wrote:
>Then you probably need to check what is going on inside the method
tmp.readFromFile(database) since that is presumably where the garbage
is getting put into the tmp.SSN field. Are you missing a terminating
'\0' or similar somehwere?
>Please let me know if I am completely off on my thoughts here, or if
I'm on target, how I can go about removing the "garbage" at the end of
tmp.SSN to make it compare correctly.
Better to ensure that it never gets there is the first place, but you
could use the fact that SSN is a fixed number of digits to check that
what you have in the SSN field is the right number of digits and not
garbage.

rossum

The SSN variable is allocated as char SSN[10], which has something to
do with the garbage on the end of tmp.SSN but I'm not sure exactly
what. I always assumed that the extra space was for the terminating
character /0. Please correct me if I'm wrong.
Your problem is almost certainly in the readFromFile(database)
function which appears not to by filling the SSN field correctly. Fix
that function so that the field is constructed correctly, if you have
to you can add the terminating null character explicitly:
SSN[9] = '\0';
>
As for using the fixed number of digits to check that SSN isn't full of
garbage, I can see when I run debugger that the SSN is inherently
correct, just followed by some garbage at the end. I am trying to
figure out a way to pull the 9 digits from tmp and d, assign them to
two seperate variables, and compare them that way.
You shouldn't need to do that if both are formed correctly.
>
However, when I attempt to do this I run into the error:
error C2106: '=' : left operand must be l-value

and the only other way I can see of doing it is to convert them to a
string, which seems much more bulky than necessary. But perhaps not.
Strings are much safer, and much easier to use than arrays of
characters. It would probably be worth your while to make the change.

rossum

>(I am relatively new at this, please forgive me if my logic is terribly
off.) Any ideas?
Jan 7 '07 #4
Thank you for your assistance.

I went ahead and cast tmp.SSN and d.SSN to strings and compared them
that way. I also commented out the overloaded operator function ==.

zaie

rossum wrote:
On 6 Jan 2007 14:35:28 -0800, "zaie" <sa*******@gmail.comwrote:

rossum wrote:
Then you probably need to check what is going on inside the method
tmp.readFromFile(database) since that is presumably where the garbage
is getting put into the tmp.SSN field. Are you missing a terminating
'\0' or similar somehwere?

Please let me know if I am completely off on my thoughts here, or if
I'm on target, how I can go about removing the "garbage" at the end of
tmp.SSN to make it compare correctly.
Better to ensure that it never gets there is the first place, but you
could use the fact that SSN is a fixed number of digits to check that
what you have in the SSN field is the right number of digits and not
garbage.

rossum
The SSN variable is allocated as char SSN[10], which has something to
do with the garbage on the end of tmp.SSN but I'm not sure exactly
what. I always assumed that the extra space was for the terminating
character /0. Please correct me if I'm wrong.
Your problem is almost certainly in the readFromFile(database)
function which appears not to by filling the SSN field correctly. Fix
that function so that the field is constructed correctly, if you have
to you can add the terminating null character explicitly:
SSN[9] = '\0';

As for using the fixed number of digits to check that SSN isn't full of
garbage, I can see when I run debugger that the SSN is inherently
correct, just followed by some garbage at the end. I am trying to
figure out a way to pull the 9 digits from tmp and d, assign them to
two seperate variables, and compare them that way.
You shouldn't need to do that if both are formed correctly.

However, when I attempt to do this I run into the error:
error C2106: '=' : left operand must be l-value

and the only other way I can see of doing it is to convert them to a
string, which seems much more bulky than necessary. But perhaps not.
Strings are much safer, and much easier to use than arrays of
characters. It would probably be worth your while to make the change.

rossum

(I am relatively new at this, please forgive me if my logic is terribly
off.) Any ideas?
Jan 7 '07 #5
zaie wrote:
>
template<class T>
bool Database<T>::find(const T& d) {
T tmp;
database.open(fName,ios::in|ios::binary);
while (!database.eof()) {
tmp.readFromFile(database);

When running this in
debugger, it shows that the correct SSN is being pulled from tmp and
compared to the entered SSN d; however, the tmp.SSN variable has what I
call "garbage" at the end and I believe this is keeping it from
comparing correctly.
It sounds like tmp.readFromFile is not reading the stuff correctly,
please post the entire code of that function.

Also, how are you handling errors (eg. if the file ends during
the readFromFile operation)?

Jan 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
0
by: Tor Hovland | last post by:
I'm trying to transform the document element of incoming xml files, however, I'm having trouble with namespace references not appearing correctly. Here's an example input file: <?xml...
0
by: kmunderwood | last post by:
I am having trouble excluding select xml out to HTML using xsl I want to ignore some xml and turn others red I can not find the right way to both: 1. Only show the <tag> that want to, and...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
5
by: Alexander Stippler | last post by:
Hello, I wrote an (templatized) operator==() and don't know why the compiler doesn't consider it. What prevents it from being chosen? class Data {}; template <typename D> class Vector {
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
8
by: Exits Funnel | last post by:
Hello, I've been tasked with porting some C++ code from Windows to Linux. The following excerpt is giving me trouble: //BEGIN CODE #include <string> class TempTestBase_t {
0
by: JackWarner | last post by:
OK, I'm finally getting our organization to upgrade from v6, and the first project I try to recompile I run into hairy problems. We've used this ATL COM collection class for years. It looks like...
12
by: swismiself | last post by:
Hi group, I'm having trouble with a website I'm working on for my boss. Normally I code my own html, but I was having trouble getting the right "look" for this site, so I decided to use a free...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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...

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.