Thread: help comparing strings

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    help comparing strings

    ok

    Code:
    while(strcmp(scanf("%s",qui),quit) != 0)

    this is within one function
    i have a local variable called char qui[3];

    and then i have a global variable called quit[] = "quit"

    but i my while statement will not work. can someone help

    its within a function and what im trying to do is that when the word quit is scaned i want it to exit the while loop.
    can someone help

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm surprised that compiles - scanf() does not return a pointer to the string you read in, so it will certainly not give the right information for strcmp().

    You will need to separate scanf() from the strcmp() somehow.

    And please enable some higher level of warnings on your compiler, so that when you pass an integer to a function expecting a string, it gives you a warning.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    sorry it dosnt compile!!!

    but i need to compare the string i read in to a global variable called "quit" at any point juring the while statement....

    within my while statement i have calls to other functions. but if the word quit is inputed i want it to exit the while statement.......

    any help



    can u help me ???

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while(scanf("%s",qui) == 1)
    {
       if(strcmp(qui,"quit") == 0)
          break;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, if you want to be "tricky":
    Code:
    while (scanf("%s", qui) == 1 && strcmp(qui, quit) == 0) ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by matsp View Post
    Or, if you want to be "tricky":
    Code:
    while (scanf("%s", qui) == 1 && strcmp(qui, quit) == 0) ...
    --
    Mats
    This is not code, it is art
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xuftugulus View Post
    This is not code, it is art
    I don't think that is "beautiful code", but it does solve the problem in one statement. It also poses a problem: What is the RIGHT thing to do when the scanf() returns a different result than 1 - consider this:
    Code:
       while(scanf("%d", &x)  == 1 && x != 0) ...
    If we enter "a" as the answer to the scanf, then scanf() will return 0. Now we break the loop, although the user just mistyped something.

    Perhaps this more obscure solution would work:
    Code:
       while(scanf("%d", &x), x != 0) ...
    But by this time we have lost half the audience in the osbcurities of the comma operator.

    Edit: Note that for strings it's not really a problem, as strings can only cause scanf() to "fail" with end-of-file result [although scanf() has other problems related to reading strings, in fact, I wouldn't recommend using scanf() for reading strings from a user].

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM