Thread: code to test odd/even

  1. #1
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33

    code to test odd/even

    This should be real easy but I'm not getting it.
    User is prompted for an integer. I test the integer for being equal to zero or int % 1 !=0. Below I test against mod 2 for even/odd.

    First it thinks everything is an odd integer. Second it thinks everything is odd. I thought % just gives you the remainder of the one figure divided by the other. Am I missing something?
    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    int main()
    {
        int num = 0;
        bool isOdd;
        
        
        cout << "Enter an integer (whole number) and I will tell you" << endl;
        cout << "if the number is odd or even:  " << endl;
        
        cin >> num;
        if(num=0 || num % 1 != 0)
        {
                    cout << "Not an integer.  Shutting down." << endl;
                    }
    
        if(num%2!=0)
        {
                    isOdd = true;
                     }
        if(isOdd=true)
        {
                      cout << "Number is Odd." << endl;
                      }
        if(isOdd=false)
        {
                      cout << "Number is even." << endl;
                      }
                     
                      system ("pause");
                      }
    fyi: the only reason for the bool is because I tried the test with just the % and got the same result. Any help?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the difference between == and = ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You made a typographical error: num=0 should be num==0. But since 0 is an integer, your output in that case does not make sense. Also, unless I missed something, num % 1 != 0 is always false.

    A similiar problem with = versus == is repeated later in your program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33

    thank you

    will apply changes. However, I'm still confused if a decimal/fraction is divided by one it is still the decimal, thus producing a remainder. This doesn't apply for %? Please clarify. I apreciate it.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
        cin >> num;
        if(num=0 || num % 1 != 0)
        {
             cout << "Not an integer.  Shutting down." << endl;
        }
    I thought 0 was an integer. Besides 0, num, being an integer variable, can't store anything that isn't an integer. If you want to check that the user indeed typed an integer, you'll need to test the result of the input statement:

    Code:
        if (!(cin >> num) || num == 0)
        {
             cout << "Not an integer.  Shutting down." << endl;
        }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  2. test code
    By egomaster69 in forum C Programming
    Replies: 3
    Last Post: 12-20-2004, 07:04 PM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM