Thread: Using the if() and || (or) statements properly.

  1. #1
    Registered User
    Join Date
    Dec 2010

    Using the if() and || (or) statements properly.

    Please do excuse my lack of experience, but I have a (simple?) question that I cannot seem to find laying around.

    - Having issues combining conditions to test for if()
    - I wish to test multiple strings in one if statement, WITHOUT adding each particular thing to test for in its own parenthesis as done in the first example.

    if ((answ == "yes") || (answ == "Yes") || (answ == "YES"))
    Alternatives attempted:
    if (answ == "yes" || "Yes" || "YES")
    if (answ == "yes, "Yes", "YES")
    I'm sure it's something rather simplistic that I'm just missing out on, but no tutorials I've seen explain the process of testing multiple string conditions in an if statement.
    The first code I've included does work, but I presumed there was a better, more ethical way to approach my problem. Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well your first example is the only way to compare with each string variant.

    It is usually simpler to convert answ to lowercase, then have a single comparison with answ == "yes"

    The others are valid syntax, but they don't do what you want them to.
    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
    Registered User
    Join Date
    Dec 2010
    Good point. I should know it would be something so simple and mindblowing.. haha. And yes, I've noticed that the if statement tests positive regardless if I use any but the first.

    In regards to converting a string to lowercase, what syntax do you suggest?

    (Simplest solution please)

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    #include <iterator>
    #include <string>
    #include <algorithm>
    #incldue <cctype>
    std::string s("My string");
    std::string lower;
    std::transform(s.begin(), s.end(), std::back_inserter(lower), &std::tolower);
    Unfortunately, there is no simple function for transforming a string to lower-case. But it's simple if you know your way around the standard library.

    If you learn about iterators (especially back insert iterators), std::transform and std::lower, it should hopefully become obvious.
    Last edited by Elysia; 12-30-2010 at 08:36 AM.
    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. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM