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

This is a discussion on Using the if() and || (or) statements properly. within the C++ Programming forums, part of the General Programming Boards category; Please do excuse my lack of experience, but I have a (simple?) question that I cannot seem to find laying ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    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.

    Problem:
    - 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.

    Code:
    if ((answ == "yes") || (answ == "Yes") || (answ == "YES"))
    Alternatives attempted:
    Code:
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Code:
    #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, 01: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

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