Answered How do I achieve an OR type statement in VS C++ 2010?

  • Wednesday, July 11, 2012 7:52 AM
     
     

    What I'm trying to do...

    String Answer

    If (Answer =="Yes")

    Do action 1

    Else if (Answer =="No")

    Do action 2

    If (Answer != "Yes" OR Answer != "No")

    Do action 3.

    However, I'm not sure how to do it unfortunately.

    I tried to do it with 

    For (Answer != "Yes", Answer !="No")

    But I'm assuming that is incorrect as well.

    Also, that wouldn't even compile.

    I tried to set it as...

    If Answer == Yes - Do blah blah

    If Answer == No - Do blah blah2

    If Answer != Yes - Do blah blah3

    If Answer != No - Do blah blah3

    However, then if the answer is 'Yes' the 4th command interferes since the answer is NOT No and will do that command as well. 

    Can anyone please explain to me how you can give 2 answers as correct, the rest as negative and achieve 3 different results based on that?

    Thanks.

All Replies

  • Wednesday, July 11, 2012 8:24 AM
     
     Answered
    >How do I achieve an OR type statement
    >If (Answer != "Yes" OR Answer != "No")

    if(Answer != "Yes" || Answer != "No")

    Note however that such a comparison will *always* test true.
    Think about it. If it's "Yes" then it can't be "No" and if
    it's "No" it can't be "Yes". So at least one of the comparisons
    must be true - Answer has to be not equal to one or the other
    (or both). With negations such as != and the same variable
    being tested, a logical "AND" is needed (&&).

    You appear to be completely lacking in the basics of C or C++.
    Do yourself a favour and read a textbook or take a course or a
    tutorial before trying to program anything. It's not possible
    to teach someone how to program in C/C++ (or any other language)
    in forums such as these.

    - Wayne

  • Wednesday, July 11, 2012 9:19 AM
     
     Answered

    Also, keep in mind that when comparing strings for equality, things like case sensitivity can bite you. "Yes", "YES", "yes" and "Yes!" are all different things, and comparing them with "Yes" will have to be more complex than just comparing equality.

    As for learning C++, I don't know which materials you are working with, but you need either a course or a good book to teach you not only the syntax, but also the underlying principles and paradigms that the language is based around.

  • Wednesday, July 11, 2012 9:23 AM
     
     

    How odd, I'm not just a noob at C++ but at sticking to a name too since I had to change it slightly... Anyways.

    Thanks for the answer || fixed my problem. when you said AND, I thought of it in my head the exact same way as OR, you could call it terrible english if you want. It's it's not THIS or THIS... Instead of If it's not THIS and THIS. In my head, the OR just came quicker, AND was what I meant to say and even reading back on it, although AND is far more correct, I'd still expect the same result, with just more stupidity added to it.

    As for me lacking the basics... Oh by far. I started playing with it yesterday. When I read a book or take a course, I lose interest very quickly because I don't feel like I'm doing anything at the time. I'm not expecting to be some grand master any time soon, but going through and finding problems, and when I can't find them myself, asking; just seems to be the quickest and most entertaining way for me to learn. I know that to someone with experience questions like this seem like you're asking someone who's braindead to do physics equations, however, you must understand that sometimes these things are not clearly defined. So when I went to search up an answer (mind you I tried with AND too) I never once saw || as an answer. I know I may have if I were taking a course, but as I said before... It's my very least preferred method of learning something that can have prac' work.

    To explain... The program is very simple, just command line... It asks for your age, your name, then it asks you if the answer is correct. Earlier on, I simply asked the question as such: So you are # years old and your name is X, is this correct? Yes/No.

    However, I wanted to make it a bit more complex, so I removed the Yes/No part, and wanted to make it so that if the user says: yeh or ya, nah or something other than yes or no, the program turns around and says: "what the heck do you mean 'nah'? It's a yes or no question."

    The yes OR no part may have been what was going through my head when I was asking the question in programming terms.

  • Wednesday, July 11, 2012 9:29 AM
     
     

    Also, keep in mind that when comparing strings for equality, things like case sensitivity can bite you. "Yes", "YES", "yes" and "Yes!" are all different things, and comparing them with "Yes" will have to be more complex than just comparing equality.

    As for learning C++, I don't know which materials you are working with, but you need either a course or a good book to teach you not only the syntax, but also the underlying principles and paradigms that the language is based around.

    For the first part, thank you for that. I might just expand the if to cover all possibilities. I know it's most likely the stupidest and longest way to go about it, but still a total noob...

    As for the second part, very very funny that you should mention the underlying principles and paradigms of C++ because the first thing I did before even downloading VS C++ was read up on a few lectures about the paradigms of int, boolean, long, shorts and how they actually work and how they are calculated into 0's and 1's by the computer, so I can understand why I sometimes had to use a short, or a long, and why converting them into one another can be tricky. I can still remember that 00000111 is a 7 in comp language and it all starts from the right most number at 2^0 and other such things that if I was faced with answering and I had a calculator (or a lot of paper), I could figure out. I can't say it made me an expert in any way, nor that it is even useful really, however... It's nice to understand how the computer interprets what you tell it.

  • Wednesday, July 11, 2012 12:56 PM
     
     

    Abusiive- wrote:

    Thanks for the answer || fixed my problem. when you said AND, I  thought of it in my head the exact same way as OR, you could call
    it terrible english if you want. It's it's not THIS or THIS... Instead  of If it's not THIS and THIS.

    Your code says neither. It's more like "it's not THIS, AND it's not  THAT". Here it should be clearer that replacing AND with OR completely  changes the meaning of the condition.

    See also http://en.wikipedia.org/wiki/De_Morgan's_laws


    Igor Tandetnik

  • Wednesday, July 11, 2012 1:00 PM
     
     Answered

    Abusiive wrote:

    String Answer

    If (Answer =="Yes")

    Do action 1

    Else if (Answer =="No")

    Do action 2

    If (Answer != "Yes" OR Answer != "No")
    Do action 3.

    Why not simply

    if (Answer == "Yes") {
        // Answer is "Yes" - do something
    } else if (Answer == "No") {
        // Answer is "No" - do something else
    } else {
        // Answer is neither "Yes" nor "No" - complain
    }

    I tried to do it with

    For (Answer != "Yes", Answer !="No")

    You can't just make syntax up. This line bears no resemblance to C++.


    Igor Tandetnik

  • Thursday, July 12, 2012 3:06 AM
     
     
    Ah, you see... Years and years ago (about 7-8 years), I used VB to make a code for a game called Starcraft, however... Since then, I haven't even touched on the idea of programming (apart from some very simple things in AutoIT), and I did remember if and if else statements, but not 'else' statements. And when I tried to use if else, due to an extra semicolon, I was getting issues of else if without an if, and else didn't accept any new input into it because of how it works. however, after the whole || and making it work, I had a second look at the if, if else and else statements and it makes a lot more sense to use what you last said as opposed to what I used, but hey... Noob... What can I say? Thank you :)