!strcmp
-
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" #include "string.h" int main() { int x; int i; char str[80]; bool isfinished=false; char Info [] [300] = { "3030-02-000-5287" , "Here is the information for this number." , "3030-03-000-0029" , "Here is the information for this number." , }; while(isfinished==false) { cout<<"\nPlease enter a number:\n"; cin>>str; for(i=0; i < 4; i += 2) if(!strcmp(str, Info[i])) { cout<<"\nInformation:"<>x).get(); } if (x==0) isfinished=true; } return 0; }
:-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
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" #include "string.h" int main() { int x; int i; char str[80]; bool isfinished=false; char Info [] [300] = { "3030-02-000-5287" , "Here is the information for this number." , "3030-03-000-0029" , "Here is the information for this number." , }; while(isfinished==false) { cout<<"\nPlease enter a number:\n"; cin>>str; for(i=0; i < 4; i += 2) if(!strcmp(str, Info[i])) { cout<<"\nInformation:"<>x).get(); } if (x==0) isfinished=true; } return 0; }
:-D"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!
-
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" #include "string.h" int main() { int x; int i; char str[80]; bool isfinished=false; char Info [] [300] = { "3030-02-000-5287" , "Here is the information for this number." , "3030-03-000-0029" , "Here is the information for this number." , }; while(isfinished==false) { cout<<"\nPlease enter a number:\n"; cin>>str; for(i=0; i < 4; i += 2) if(!strcmp(str, Info[i])) { cout<<"\nInformation:"<>x).get(); } if (x==0) isfinished=true; } return 0; }
:-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
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? :confused: :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
:eek: 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):zzz:, and I hadn’t finished my coffee. :~ :laugh:
-
:eek: 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):zzz:, and I hadn’t finished my coffee. :~ :laugh:
Xander80 Mentioned 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 still confused:eek: If !strcmp returns 0(from a non compare condition), why would I want to cout anything? I would think that you want to cout when you have found a match. Hmmmmm. :confused: Thanks :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
Xander80 Mentioned 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 still confused:eek: If !strcmp returns 0(from a non compare condition), why would I want to cout anything? I would think that you want to cout when you have found a match. Hmmmmm. :confused: Thanks :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
Again I'm sorry :zzz::zzz::zzz: (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:
int result = strcmp(str, Info[i]); if(!result) { cout<<"\nInformation:"< If result == 0 then the information is displayed. Maybe it's better to rewrite the code to: `if (strcmp(str, Info[i])==0) { cout<<"\nInformation:"< so it's perfectly clear that the if statement checks for identical strings (for less experienced programmers like me :~ ). :wtf::wtf:`
-
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" #include "string.h" int main() { int x; int i; char str[80]; bool isfinished=false; char Info [] [300] = { "3030-02-000-5287" , "Here is the information for this number." , "3030-03-000-0029" , "Here is the information for this number." , }; while(isfinished==false) { cout<<"\nPlease enter a number:\n"; cin>>str; for(i=0; i < 4; i += 2) if(!strcmp(str, Info[i])) { cout<<"\nInformation:"<>x).get(); } if (x==0) isfinished=true; } return 0; }
:-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
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"
-
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" #include "string.h" int main() { int x; int i; char str[80]; bool isfinished=false; char Info [] [300] = { "3030-02-000-5287" , "Here is the information for this number." , "3030-03-000-0029" , "Here is the information for this number." , }; while(isfinished==false) { cout<<"\nPlease enter a number:\n"; cin>>str; for(i=0; i < 4; i += 2) if(!strcmp(str, Info[i])) { cout<<"\nInformation:"<>x).get(); } if (x==0) isfinished=true; } return 0; }
:-D"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
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.
-
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"
-
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.
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 :-D
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
-
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 :-D
"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