Thread: Or Questions

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Or Questions

    Instead of doing
    Code:
    if ( strCookies=="good" || strCookies=="better" )
    can you do something like
    Code:
    if ( strCookies==("good" || "better") )
    also, is there any way to put multiple items on a case line:

    e.g.
    Code:
    case apples||oranges:
    {
    cout<<"either apples or oranges were in the box"<<endl;
    break;
    }
    Last edited by TenderKitten; 10-16-2011 at 11:39 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > can you do something like
    > if ( strCookies==("good" || "better") )
    You can (syntactically), but it doesn't mean what you want it to mean (namely comparing strCookies with 2 strings).

    > also, is there any way to put multiple items on a case line:
    Yes, you just do
    Code:
    case apples:
    case oranges:
    {
        cout<<"either apples or oranges were in the box"<<endl;
        break;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by TenderKitten View Post
    Instead of doing
    Code:
    if ( strCookies=="good" || strCookies=="better" )
    can you do something like
    Code:
    if ( strCookies==("good" || "better") )
    Nope, sorry. You'd have to make a utility function for that.
    Example:
    Code:
    template<typename T1, typename T2, typename T2>
    bool equal(const T1& val1, const T2& val2, const T3& val3)
    {
    	if (val1 == val2) return true;
    	return (val1 == val3);
    }
    
    if (equal(strCookies, "good", "better"))
    	// Do something
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. About asking questions
    By Niara in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-04-2008, 12:58 PM
  3. Some questions
    By Overlord in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2006, 10:41 AM
  4. Two Questions.
    By Ranedhel in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2003, 07:49 AM
  5. questions
    By kuwait in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2002, 01:35 PM