C String Comparison

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kidko
    New Member
    • Apr 2007
    • 17

    C String Comparison

    I'm trying to write a function that reads keystrokes using the NCURSES library's "getstr(str )" function (where "str" is a C-style string, and it modifies the value of it directly). However, my code does not ever cause the program to exit, and even when typing exactly the word "quit," no backspace or anything, it gives me the "Unknown command" message. I'm writing in C++ using the G++ compiler on Kubuntu Linux 7.04.
    [code=cpp]display::clear_ messages(); // Clear the top line of the screen
    echo(); // Show the characters being typed
    cbreak(); // Wait for <enter>
    char text[20];
    getstr(text); // changes text[20] directly
    noecho(); // Don't show characters
    raw(); // Give me the keystrokes directly
    if (text == "quit") {
    exit(0);
    } else {
    display::messag e(display::blan k_line);
    move(0,0); // Move to top line
    printw("Unknown command '%s'",text); // works like printf()
    }[/code]

    Thanks in advance!
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    text == "quit"
    Will always return false.

    C strings are not first class citizens. Unlike say two ints, you cannot compare two C strings and have that translated to comparing the values of the strings. Referring to a string is the same as referring to an array of type char, which translates to you referring to a char pointer. Do you follow?

    Hence, == compares char pointers. Not what you are looking for. You probably want to use strcpy (or better yet, strncpy).

    Comment

    • keerthiramanarayan
      New Member
      • Nov 2007
      • 13

      #3
      Originally posted by kidko
      [code=cpp]
      if (text == "quit") {
      [/code]
      should be replaced by
      [code=cpp]
      if(strncmp(text ,"quit",4) == 0){
      [/code]

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If you stop using the C string library and start using the C++ string object you will have fewer problems.

        Comment

        • kidko
          New Member
          • Apr 2007
          • 17

          #5
          Originally posted by oler1s
          C strings are not first class citizens. Unlike say two ints, you cannot compare two C strings and have that translated to comparing the values of the strings. Referring to a string is the same as referring to an array of type char, which translates to you referring to a char pointer. Do you follow?

          Hence, == compares char pointers. Not what you are looking for. You probably want to use strcpy (or better yet, strncpy).
          So it was comparing memory addresses? Definitely not what I'm looking for. But why use str(n)cpy? Doesn't that copy the string (or n letters of it), while what I want is a comparison?

          Originally posted by keerthiramanara yan
          should be replaced by
          [code=cpp]
          if(strncmp(text ,"quit",4) == 0){
          [/code]
          Aha. Exactly what I needed.

          Thanks, everybody, for the feedback/help.

          Comment

          Working...