how do I do "this or that" with strcmpi

This is a discussion on how do I do "this or that" with strcmpi within the C++ Programming forums, part of the General Programming Boards category; How can I combine these to save space in the code? Code: if (!strcmpi("exit", command)) { return 0; } if ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    how do I do "this or that" with strcmpi

    How can I combine these to save space in the code?

    Code:
              if (!strcmpi("exit", command))
    					{
    						return 0;
    					}
    
                if (!strcmpi("x", command))
    					{
    						return 0;
    					}

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: how do I do "this or that" with strcmpi

    Code:
              if (!strcmpi("exit", command) || !strcmpi("x", command))
    					{
    						return 0;
    					}
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    if (!strcmpi("exit", command)&&(!strcmpi("x", command)))
    {
    return 0;
    }

    btw isnt it strcmp...?

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Code:
    if (!strcmpi("exit", command) && !strcmpi("x", command) )
    {
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    sorry i also meant || ..

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    easah

    Code:
    if (!strcmpi("exit", command) || !strcmpi("x", command))
         return 0;
    EDIT: wow a lot of people beat me
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    No, it should be &&, you had it right the first time. If it is || then it will ALWAYS return zero since the only way that statement is false is if the string is equal to both "x" and "exit" at the same time.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    pj, rethink this one. look at his original code. In your example, both must be true;
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    You're starting to scare me, PJ
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    he didnt meant that u must check both x and exit did he?
    if not he should use ||

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Wait, what exactly is he trying to accomplish? Does he want the function to continue unless the user inputs something other than "exit" or "x" or does he want it to return no matter what the user prints? Because if you use the || sign, then no matter what command is, whether it be "dog","exit" or "x" it will return. If thats the case, then why even have the if statement?

    I guess I'm assuming that he means to check to see if the string isn't either "x" or "exit" which would need the &&.

    [edit]
    Maybe he didn't mean to use the ! operator?
    [/edit]
    Last edited by PJYelton; 02-04-2003 at 03:58 PM.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    only he can know

  13. #13
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    [code]
    if (!strcmpi("exit", command) || !strcmpi("x", command))
    {
    return 0;
    }
    [code]


    thats what i wanted

  14. #14
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by PJYelton
    Wait, what exactly is he trying to accomplish? Does he want the function to continue unless the user inputs something other than "exit" or "x" or does he want it to return no matter what the user prints? Because if you use the || sign, then no matter what command is, whether it be "dog","exit" or "x" it will return. If thats the case, then why even have the if statement?

    I guess I'm assuming that he means to check to see if the string isn't either "x" or "exit" which would need the &&.

    [edit]
    Maybe he didn't mean to use the ! operator?
    [/edit]
    Say it outloud; If string equals exit and string equals x... doesn't make sense.
    If string equals exit or string equals x... makes sense.

  15. #15
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I know, but aren't you forgetting about the ! operator?

    if (!(x==y) || !(x==z)) will ALWAYS return true.
    if (!(x==y) && !(x==z)) will return false if x is neither y or z.

    [edit] Oops, the first statement I guess can return false, but only if y==z [/edit]
    Last edited by PJYelton; 02-04-2003 at 04:14 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alternative to strcmpi
    By ehj3000 in forum C Programming
    Replies: 6
    Last Post: 12-12-2005, 07:09 PM
  2. HELP with strlen and strcmpi
    By unejam2005 in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2005, 03:01 AM
  3. basic strcmpi question
    By Noobie in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2003, 03:50 PM
  4. strcmpi()
    By Sicilian in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 02:19 PM

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