Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. !strcmp

!strcmp

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveE9th
    wrote on last edited by
    #1

    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

    T X R L 4 Replies Last reply
    0
    • D DaveE9th

      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

      T Offline
      T Offline
      Tibor Blazko
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0
      • D DaveE9th

        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

        X Offline
        X Offline
        Xander80
        wrote on last edited by
        #3

        The strcmp function returns 0 when string1 is identical to string2. The application writes the info to screen when string1 is identical to string2.

        D 1 Reply Last reply
        0
        • X Xander80

          The strcmp function returns 0 when string1 is identical to string2. The application writes the info to screen when string1 is identical to string2.

          D Offline
          D Offline
          DaveE9th
          wrote on last edited by
          #4

          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

          X 1 Reply Last reply
          0
          • D DaveE9th

            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

            X Offline
            X Offline
            Xander80
            wrote on last edited by
            #5

            :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:

            D 1 Reply Last reply
            0
            • X Xander80

              :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:

              D Offline
              D Offline
              DaveE9th
              wrote on last edited by
              #6

              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

              X 1 Reply Last reply
              0
              • D DaveE9th

                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

                X Offline
                X Offline
                Xander80
                wrote on last edited by
                #7

                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:`

                1 Reply Last reply
                0
                • D DaveE9th

                  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

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #8

                  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"

                  1 Reply Last reply
                  0
                  • D DaveE9th

                    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

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    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.

                    R D 2 Replies Last reply
                    0
                    • L Lost User

                      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.

                      R Offline
                      R Offline
                      Ryan Binns
                      wrote on last edited by
                      #10

                      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"

                      1 Reply Last reply
                      0
                      • L Lost User

                        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.

                        D Offline
                        D Offline
                        DaveE9th
                        wrote on last edited by
                        #11

                        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

                        I 1 Reply Last reply
                        0
                        • D DaveE9th

                          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

                          I Offline
                          I Offline
                          Ian Darling
                          wrote on last edited by
                          #12

                          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

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups