Using strcmp/strcmpi in a while loop parameter

This is a discussion on Using strcmp/strcmpi in a while loop parameter within the C Programming forums, part of the General Programming Boards category; Using only two dictionaries, stdio.h and string.h, I am writing a program in which the user must type "quit" (case ...

  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,205
    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%.

  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, 11:34 AM
  3. alternative to strcmpi
    By ehj3000 in forum C Programming
    Replies: 6
    Last Post: 12-12-2005, 06:09 PM
  4. HELP with strlen and strcmpi
    By unejam2005 in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2005, 02:01 AM
  5. strcmpi()
    By Sicilian in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 01:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21