|
I explained this a day or two ago. Search back a bit and you should find the information.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
In the code below, I'm curious about the "!" in front of the strcmp function. I'm confused on this. In my understanding, the !strcmp is saying that IF str and Info do NOT compare, then cout the number. What am I missing hear? Thanks
#include "iostream.h"<br />
#include "string.h"<br />
<br />
int main()<br />
{<br />
int x;<br />
int i;<br />
char str[80];<br />
bool isfinished=false;<br />
char Info [] [300] = <br />
{<br />
"3030-02-000-5287" , "Here is the information for this number." ,<br />
"3030-03-000-0029" , "Here is the information for this number." ,<br />
};<br />
while(isfinished==false)<br />
{<br />
cout<<"\nPlease enter a number:\n";<br />
cin>>str;<br />
for(i=0; i < 4; i += 2)<br />
if(!strcmp(str, Info[i]))<br />
{<br />
cout<<"\nInformation:"<<Info[i+1]<<"\n";<br />
break;<br />
}<br />
if(i == 4)<br />
{<br />
cout<<"Number not found.\n";<br />
}<br />
<br />
cout<<"\nWould you like to check another?\n";<br />
<br />
{<br />
cout<<"Enter 1 for yes, 0 for no.\n";<br />
(cin>>x).get(); <br />
<br />
}<br />
if (x==0) isfinished=true;<br />
}<br />
<br />
return 0;<br />
}<br />
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
if(!something)
is equal to
if(something == 0)
our strcmp is executed, its return value is negated and if solution is not zero (false) than gets into if())
t!
|
|
|
|
|
The strcmp function returns 0 when string1 is identical to string2. The application writes the info to screen when string1 is identical to string2.
|
|
|
|
|
So would strcmp (without the "!") return a 1(TRUE) if the two variables are equal?
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
Oh no, sorry. The function returns 0 when the strings are equal. That would make:
if (!0)
So only when the strings are not equal, the code in the if statement gets executed, and the next in the info array is printed to screen (Info[i+1]).
I'm sorry, it's very early in the morning (in Holland) , and I hadn’t finished my coffee.
|
|
|
|
|
|
Again I'm sorry (I must be sleeping).
When you compare 2 strings with strcmp(), this function returns 0, so strcmp("codeproject","codeproject") returns 0. That will result in if (!0). NOT 0 is 1, so when the 2 strings are identical, the result is displayed on screen with cout.
Check by watching the result var in the debugger:
<br />
int result = strcmp(str, Info[i]);<br />
<br />
if(!result)<br />
{<br />
cout<<"\nInformation:"<<Info[i+1]<<"\n";<br />
break;<br />
}
If result == 0 then the information is displayed.
Maybe it's better to rewrite the code to:
<br />
<br />
if (strcmp(str, Info[i])==0)<br />
{<br />
cout<<"\nInformation:"<<Info[i+1]<<"\n";<br />
break;<br />
}<br />
<br />
so it's perfectly clear that the if statement checks for identical strings (for less experienced programmers like me ).
|
|
|
|
|
strcmp() returns 0 when the two strings are identical. !strcmp() will invert that (! is the not operator) so it will return 1 when the strings are equal, and 0 when they are not. In this case, whatever is in the if statement will be executed when the two strings are identical.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Bad programming practise IMHO - you should only use ! on a boolean expression - not on something that returns an int. I would always write code like this as "if (strcmp(psz1, psz2) == 0" or "if (strcmp(psz1, psz2) != 0".
Just my 2c.
When I am king, you will be first against the wall.
|
|
|
|
|
Robert Edward Caldecott wrote:
Bad programming practise IMHO
Agreed.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Thank you all, I get it now. I was thinking that the strcmp function would return a 1 for a match, not a 0. Is there a particular reason for this? Thanks again, Dave
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
strcmp returns a negative or positive number if the first string is less than or greater than the second string (check which way round they actually are in MSDN), which is why it returns 0 for equality.
--
Ian Darling
|
|
|
|
|
I am trying to get the mouse coodinate on picture box/control while mouse click accour on it in order to draw some circle based on that coordinate. But so far I can get it. I can get the mouse coordinate only on form itseft not on picture box.
Any one can help me?I really appreciate it, thank you.
|
|
|
|
|
Try GetCursorPos() function, you just pass it a pointer to a POINT structure.
-Nick Parker
|
|
|
|
|
If you're using MFC, use MapWindowPoints() , if you're not using MFC, using ClientToScreen() , to convert from the form to the screen coordinates, and ScreenToClient() to convert from the screen to the control coordinates.
Hope this helps,
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
how can i use toolbar control in my activex control.
thanks! every one
|
|
|
|
|
see:
http://www.codeproject.com/script/comments/forums.asp?msg=550923&forumid=1647#xx550923xx
and
http://www.codeproject.com/script/comments/forums.asp?msg=549039&forumid=1647#xx549039xx
now my test result is :
the clean app works, but the clean DLL does not.
a good man in china
|
|
|
|
|
what is the different between the the framewrok of the win32 DLL and that of the win32 appliacation in vc++ 6.0 ??I encountered a problem ,and i have to know that,where can i found the information.??
a good man in china
|
|
|
|
|
Your question really does not make sense. As I said two days ago when you asked a similar question, a DLL is a part of an application. However this knowledge will in no way help you solve a problem with your DLL or your application.
John
|
|
|
|
|
gdzfy wrote:
I encountered a problem
Could you explain your problem? Maybe we can help.
John
|
|
|
|
|
yes,two days ago i intented to ask this question ,but i wrote the wrong sentence and ask the thing i had know.
well,here is my problem: I am making the SDK programing----writing a dll,and I have to open a driver(impeg32.dll) firstin order to use the API.THAT is I have to use the
Opendriver() In my dll,but it fails.
Maybe you can see the thread i had askde three days ago:
http://www.codeproject.com/script/comments/forums.asp?msg=548929&forumid=1647#xx548929xx
thank you very much!
a good man in china
|
|
|
|
|
Your question makes sense now. Thanks. You are asking what is different about a win32 dll from a win32 application that could cause the dll not to be able to open a device driver using OpenDriver() while the application can. I'm sorry I too have never used the OpenDriver() command.
John
|
|
|
|
|
well,that is all right.
a good man in china
|
|
|
|
|