Intended false statement is seen as true

This is a discussion on Intended false statement is seen as true within the C++ Programming forums, part of the General Programming Boards category; Hey Guys, This is my first post here as I`m learning a programming language for the first time and I`m ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Intended false statement is seen as true

    Hey Guys,

    This is my first post here as I`m learning a programming language for the first time and I`m encountering a nasty problem:

    I intend to use the "if" function to split different types of situations but it seems like i have a fault in my statements because the program doesnt do what I intend to make it do. Here is the code, it will explain my problem:

    Code:
     
    # include <iostream>
    using namespace std;
    
    int main()
    {
    
    int x;
    int y;
    
    cin >> x;
    cin >> y;
    
    if (x<=10 && y==1 || 2 || 3)
    cout <<" x is smaller than 11 and y is 1 or 2 or 3";
    
    else
    cout <<" x is not smaller than 11 or y is not 1 or 2 or 3";
    
    return 0;
    }
    The problem is when I run the program and I enter a value of x bigger than 10 I still get the cout as if the statement was true. Same happens if I enter a value of y which is not 1 or 2 or 3. I still get "x is smaller than 11 and y is 1 or 2 or 3" . What did I do wrong. I want to make the program give "x is smaller than 11 and y is 1 or 2 or 3" when it is true but how do I write this in a statement that will be used in the right way by the compiler?

    I hope that I`m making myself clear because I`m new to programming. It would help me alot if someone could help get rid of my problem.

    Greetings,

    A beginner programmer

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    if (x<=10 && y==1 || 2 || 3)
    As you've noticed, that's not the proper way to do this. You need to specify "y==" in each of the cases, for example:
    Code:
    if (x<=10 && (y==1 || y == 2 || y == 3))
    Notice how I've added parentheses around the whole y-testing expression. If I didn't do this, since && has higher precedence than ||, the compiler would interpret it as
    Code:
    if ((x<=10 && y==1) || y == 2 || y == 3)
    which is probably not what you want.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    wow thanks a lot for the fast reply, I`m going to try to fix this. Is this board the best way to learn by asking beginner questions or do you recommend another way of learning. I still dont have a book and I`m looking on this site cplusplus.com to learn but it doesnt have all the answers to my questions like the one I asked here.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Also is there a better way of comparing an integer with a set of integers? Like you want to see if an integer n compares to 1,5,7,11,20 etc. What is the best way to do so. It would be nice to know how to do this in an efficient way or is it hard to explain.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I didn't learn by asking questions on this board -- I learned on my own from a textbook. (Well, multiple textbooks.) I hardly used the internet at all really. So I'm not the best person to answer that question. [Strange as it may seem, I think one of the ways I learned programming was *answering* questions on CBoard. You'd be surprised how much you can learn that way. ] But I have asked questions on this board and it's a wonderful resource. I think that you need to do a lot of work on your own (reading tutorials perhaps, or textbooks, and certainly writing lots of programs), but that when you're in a bind asking a question here is one of the best ways to get an answer.

    The best way to learn is to practice over and over, write as much code as you possibly can. Textbooks are only useful for a little while, but if you're just starting you may certainly find it useful.

    [edit] In regards to your second question:

    A few ways I can think of for testing whether an integer is in a set of integers:
    • Find a formula to describe the integers. If you're testing for oddness, for example, you can use "if(i % 2 == 1)".
    • Just put the expression into another function. It's amazing how much more readable code can be if it says "if(is_day_of_month(day, month))", no matter how simplistic the function might seem.
    • Create a std::set<>, put the integers you want to test for into the set, and then check if your number is contained in the set. This is quite efficient.

    [/edit]
    Last edited by dwks; 09-17-2010 at 12:32 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    An example of the last case, since it seems the most interesting:
    Code:
    #include <iostream>
    #include <set>
    
    int main() {
        std::set<int> special;
        
        special.insert(1);
        special.insert(5);
        special.insert(7);
        special.insert(11);
        special.insert(20);
        
        std::cout << "Enter a number: ";
        int n;
        std::cin >> n;
        
        if(special.find(n) != special.end()) {
            std::cout << "Congrats, you guessed a special number!\n";
        }
        else {
            std::cout << "That's a boring number.\n";
        }
        
        return 0;
    }
    (Highlighted with codeform.)

    A few example runs:
    Code:
    $ ./isinset 
    Enter a number: 4
    That's a boring number.
    $ ./isinset 
    Enter a number: 5
    Congrats, you guessed a special number!
    $
    For more details, read up on documentation about std::set -- for example: set - C++ Reference
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Thanks again for all the help. Its much information to take in. I have been working on this C++ program for 3 hours facing constant problems so I think I need to take a break and do something else.

    This will hopefully be my last question for now: I dont understand what you mean to express with this code: [code] if(is_day_of_month(day, month)) [\code]. You said that this was a way to solve the problem if you want to see if an integer is part of a set of integers.

    Btw the program I`m working on is an assignment for a C++ programming class I`m following for my 1st year of my Physics Bachelor. You have to do something with birthdates and now I`m working on the part where it shuts down the program when you enter a non existant birthdate. I`m still not sure how to make it shut down if you enter a non existant birthdate. I`m having trouble copy-pasting out of my nedit, I think its a bug maybe having to do with the way my linux is build, otherwise I could post the program.

    Thanks again dwks!

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I probably shouldn't have thrown std::set at you then. Sorry. By is_day_of_month() I meant you could put the expression into a separate function, but if you're not familiar with functions then that's not a very helpful suggestion. For now I'd just write your code right inside the if statement, because that's the simplest and it works.

    Depending on your setup, the clipboard might not work between nedit and your web browser, but the X clipboard probably will. You copy something into this clipboard by simply selecting the text with your mouse cursor or the keyboard. Then you paste somewhere else by middle-clicking (if your mouse has only two buttons, it may recognize clicking the left and right at the same time as a middle-click simulation).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BIG problem. How do I find a bug in so much code?
    By Yarin in forum C++ Programming
    Replies: 44
    Last Post: 01-31-2008, 12:39 PM
  2. the chess project
    By Hussain Hani in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 05-28-2007, 03:33 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM
  5. True or False Quiz (Need help)
    By Twiggy in forum C Programming
    Replies: 9
    Last Post: 10-12-2001, 05:25 AM

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