473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

== evaluation problem

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
5 1471
* 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

"Gary Wessle" <ph****@yahoo.c omwrote 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
"Jim Langston" <ta*******@rock etmail.comwrite s:
"Gary Wessle" <ph****@yahoo.c omwrote 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

Gary Wessle wrote:
"Jim Langston" <ta*******@rock etmail.comwrite s:
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

Gary Wessle wrote in message ...
>"Jim Langston" <ta*******@rock etmail.comwrite s:
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
4967
by: Daniel Déchelotte | last post by:
Hi, Once I was happy with my new deezign, I noticed how bad it breaks in IE (all versions I tried). Since IE7 didn't give acceptable results, I wrote a simplified style that I would fetch to IE. How to do it is the point of my post. I am using for now conditional comment (clean) to give IE its CSS, and a IE parsing bug (not clean) to hide the real CSS from IE:
16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
3
3396
by: marco_segurini | last post by:
Hi, when the following code is executed bool Test1(); bool Test2(); .... if (Test1() || Test2()) { ... } ....
15
2948
by: Jens.Toerring | last post by:
Hi, I have a possibly rather stupid question about the order of evaluation in a statement like this: result = foo( x ) - bar( y ); How can I make 100% sure that foo(x) is evaluated before bar(y), where foo() and bar() can be either functions or macros? I got into problems with this when writing some hardware-related stuff where foo() and bar()
4
2580
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues. According to the standard, most operators evaluate their operands in an unspecified order. This means that in code like this: f() + g()
77
4763
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other ended up compiling it as a = c; b = c. (Both with no optimizations enabled). How did we notice you may ask? Well, in our case 'b' was a memory mapped register that only has a select number of writable bits. He claims it has been a 'C...
9
2511
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand uses errno, modified by right operand): (log(x) - mu) * (log(x) - mu) Code has unspecified behavior. Order of evaluation of function parameters or subexpressions is not defined, so if a value is used and
9
3516
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow us to get rid of all the temporary arrays produced by NumPy. For example, consider the...
54
3947
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of parentheses. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
39
5081
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
0
9519
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
10438
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
10164
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
6780
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
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.