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

== evaluation problem

P: n/a
Hi

I ran my code in gdb after it was giving me un-expected results, to
find out why and I found where the problem is but I don't understand
why it happened.

if( username == us && password == pa ) {
cout << "connected." << endl;
on_off = true;
} else {
cout << "wrong username or password" << endl;
}

************************************************** **************
(gdb) p username
$6 = 0x805114c "sam"
(gdb) p us
$7 = 0x8051235 "sam"
(gdb) p username == us
$8 = false
(gdb) p password
$9 = 0x8051150 "jesse"
(gdb) p pa
$10 = 0x8051239 "jesse"
(gdb) p password == pa
$11 = false
(gdb) p username == us && password == pa
$12 = false
(gdb)
************************************************** **************
I was expecting "True" instead of false above.

thanks
Nov 12 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* Gary Wessle:
>
I ran my code in gdb after it was giving me un-expected results, to
find out why and I found where the problem is but I don't understand
why it happened.

if( username == us && password == pa ) {
cout << "connected." << endl;
on_off = true;
} else {
cout << "wrong username or password" << endl;
}

************************************************** **************
(gdb) p username
$6 = 0x805114c "sam"
(gdb) p us
$7 = 0x8051235 "sam"
(gdb) p username == us
$8 = false
(gdb) p password
$9 = 0x8051150 "jesse"
(gdb) p pa
$10 = 0x8051239 "jesse"
(gdb) p password == pa
$11 = false
(gdb) p username == us && password == pa
$12 = false
(gdb)
************************************************** **************
I was expecting "True" instead of false above.
It seems you're comparing pointers instead of strings.

A good solution is to use std::string.

A bad solution is to keep the raw arrays and use std::strcmp instead of ==.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 12 '06 #2

P: n/a

"Gary Wessle" <ph****@yahoo.comwrote in message
news:m3************@localhost.localdomain...
Hi

I ran my code in gdb after it was giving me un-expected results, to
find out why and I found where the problem is but I don't understand
why it happened.

if( username == us && password == pa ) {
cout << "connected." << endl;
on_off = true;
} else {
cout << "wrong username or password" << endl;
}

************************************************** **************
(gdb) p username
$6 = 0x805114c "sam"
0x805114C looks like a pointer, so I'm guessing username is a char* (char
array).

You can't comapre character arrays using ==, using == on 2 character arrays
will state if the pointers are the same, which they usually won't be. If
you must use character arrays, you need to use strcmp (string compare) whice
will evaluate to 0 if the strings are 0. So:

char username[] = "John Smith";
char us[] = "John Smith";
if ( strcmp( username, us ) == 0 && strcmp( password, pa ) == 0 )
// yada yada

There is also a strncmp to cmpare n characters.

It is much better to use std::string so you don't have to do this type of
thing. Then you can use ==

std::string username = "John Smith";
std::string us "John Smith";

if ( username == us )
// yada yada
(gdb) p us
$7 = 0x8051235 "sam"
(gdb) p username == us
$8 = false
(gdb) p password
$9 = 0x8051150 "jesse"
(gdb) p pa
$10 = 0x8051239 "jesse"
(gdb) p password == pa
$11 = false
(gdb) p username == us && password == pa
$12 = false
(gdb)
************************************************** **************
I was expecting "True" instead of false above.

thanks

Nov 12 '06 #3

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrites:
"Gary Wessle" <ph****@yahoo.comwrote in message
news:m3************@localhost.localdomain...
Hi

I ran my code in gdb after it was giving me un-expected results, to
find out why and I found where the problem is but I don't understand
why it happened.

if( username == us && password == pa ) {
cout << "connected." << endl;
on_off = true;
} else {
cout << "wrong username or password" << endl;
}

************************************************** **************
(gdb) p username
$6 = 0x805114c "sam"

0x805114C looks like a pointer, so I'm guessing username is a char* (char
array).

You can't comapre character arrays using ==, using == on 2 character arrays
will state if the pointers are the same, which they usually won't be. If
you must use character arrays, you need to use strcmp (string compare) whice
will evaluate to 0 if the strings are 0. So:

char username[] = "John Smith";
char us[] = "John Smith";
if ( strcmp( username, us ) == 0 && strcmp( password, pa ) == 0 )
// yada yada

There is also a strncmp to cmpare n characters.

It is much better to use std::string so you don't have to do this type of
thing. Then you can use ==
I am limited because the library provided "by Big Bucks Inc." uses
const char* username and same for password. so I have to supply the
same data type in-order to use their methods.

Nov 13 '06 #4

P: n/a

Gary Wessle wrote:
"Jim Langston" <ta*******@rocketmail.comwrites:
It is much better to use std::string so you don't have to do this type of
thing. Then you can use ==

I am limited because the library provided "by Big Bucks Inc." uses
const char* username and same for password. so I have to supply the
same data type in-order to use their methods.
You might not be as limited as you think. If the library you are using
ever gives you a const char* you can immediately put it in a
std::string. Whenever you need to give a const char* to the library,
use the c_str() member function of std::string. You can use std::string
throughout your own code. Use of C style strings need not propogate
outside the interface to the library.

Gavin Deane

Nov 13 '06 #5

P: n/a

Gary Wessle wrote in message ...
>"Jim Langston" <ta*******@rocketmail.comwrites:
>You can't comapre character arrays using ==, using == on 2 character
arrays
>will state if the pointers are the same, which they usually won't be. If
you must use character arrays, you need to use strcmp (string compare)
whice
>will evaluate to 0 if the strings are 0. So:

char username[] = "John Smith";
char us[] = "John Smith";
if ( strcmp( username, us ) == 0 && strcmp( password, pa ) == 0 )
// yada yada

There is also a strncmp to cmpare n characters.

It is much better to use std::string so you don't have to do this type of
thing. Then you can use ==

I am limited because the library provided "by Big Bucks Inc." uses
const char* username and same for password. so I have to supply the
same data type in-order to use their methods.
{
char bust[] = "Issimple";
char const *busted = bust;
std::string Name( bust );
std::string Name2 = busted;
cout <<"\n string Name ="<<Name<< std::endl;
cout <<" string Name2 ="<<Name2<< std::endl;
}
// out: string Name =Issimple
// out: string Name2 =Issimple

// void BigBucks( char const *dude);

BigBucks( Name.c_str() );
Now, what were you saying? <G>

--
Bob R
POVrookie
Nov 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.