By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Trouble with find() template

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.