Thread: Using strcmp/strcmpi in a while loop parameter

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Maryland
    Posts
    2

    Smile Using strcmp/strcmpi in a while loop parameter

    Using only two dictionaries, stdio.h and string.h, I am writing a program in which the user must type "quit" (case insensitve) to quit the program. This is homework but I have searched the internet for something similar to my question but all I could find were if statements comparing strings, which is not my issue.
    Code:
    while((strcmpi(input, "quit") != 0))
    {
        //do stuff
    }
    The code above is what I have now but I'm almost sure that isn't right. I've already learned that strcmp and strcmpi return an integer 0 if the strings you're comparing are equal. But I have not learned how to asses if one string is not equal to another, let alone if you can even compare strings in the while loop parameter. This is not my entire assignment either, it is just a very small part. Everything I'm writing inside of the loop is the main part of the assignment so I am not asking anyone how to do my entire assignment. I would appreciate if someone could just steer me in the right direction. Thank you
    Last edited by kimberly; 11-02-2012 at 08:49 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The test to see if the contents of two strings are unequal is of the form "strcmp(string1, string2) != 0" (i.e. exactly as in the loop you have shown, except for your usage of strcmpi(), which is non-standard, and your unnecessary usage of additional brackets).

    You need to get your language straight. There is no such thing as a "while loop parameter". It is a condition. So
    Code:
    while(strcmp(input, "quit") != 0)
    {
        //do stuff
    }
    will loop until the string input compares equal to "quit" (i.e. the first five characters are 'q', 'u', 'i', 't', and '\0'). The "strcmp(input, "quit") != 0" is the loop condition.

    By definition, if strcmp() returns zero if the strings are equal, it returns non-zero if they are not. (Negative and positive return values from strcmp() have different meanings, but either means "not equal").

    Note that, in C, zero corresponds to false and non-zero to true. So the condition "strcmp(input, "quit") != 0" can be simplified to "strcmp(input, "quit")". The difference between these is stylistic, because the two forms are technically equivalent. Some people advocate clarity, so will advocate the first form. Some people advocate brevity, so advocate the second. I personally advocate clarity over brevity (code that is clearer and easier to understand is easier to get right).
    Last edited by grumpy; 11-02-2012 at 09:01 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Maryland
    Posts
    2
    Thank you grumpy. I do need to get my language straight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-07-2012, 09:56 AM
  2. Simple strcmp parameter problem.....
    By INFERNO2K in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 12:34 PM
  3. alternative to strcmpi
    By ehj3000 in forum C Programming
    Replies: 6
    Last Post: 12-12-2005, 07:09 PM
  4. HELP with strlen and strcmpi
    By unejam2005 in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2005, 03:01 AM
  5. strcmpi()
    By Sicilian in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 02:19 PM