Positive and Negative, Even and Odd

So I'm writing a program that asks to input an integer and the program tells if it's odd or even, negative or positive. For some reason, it'll get the pos/neg part right, but if it's negative, it outputs even and if it's positive it outputs odd. I can't figure out what is making it do this, and I've tried several while/ if-else statements. This is what I have right now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

using namespace std;


int main()
{
 
 int integer;
 int positive;
 int negative;
 int odd;
 int even;   
 char ynAns;
       
    //ask user for an integer
 cout << "Please enter an integer." << endl;
 cin >> integer;
 cout << "The integer you entered is " << endl;    

    if (integer < 0){
    integer = negative;
    cout << "negative " << endl;
    }        
    if (integer > 0){
    integer = positive;
    cout << "positive " << endl;
    }             
    if (integer == 0){
    integer = 0;
    cout << "zero." << endl;        
    }
    
    if ((integer % 2== 0) && (integer != 0)){
        integer = even;
        cout << "and even." << endl;
        }   
    else if ((integer !=0) && (integer != even)) {
        integer = odd;
        cout << "and odd." << endl;    
              }
     
 return 0;
}    


Any input will be appreciated. (I'm putting a "would you like to continue" in there, in case anyone wonders about the 'char ynAns')
Last edited on
1
2
int integer;
integer = even;


positive, negative, Even and Odd do not work for INT.

Remove all the lines where you put integer =
some suggestions for a shorter program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
    std::cout << "Enter number: ";
    int num{};
    std::cin >> num;//other input validation checks http://www.cplusplus.com/forum/beginner/206234/
    if (num != 0)
    {
        if (num % 2 == 0)
        {
            std::cout << ((num > 0) ? "Even and positive \n" : "Even and negative \n");
        }
        else
        {
            std::cout << ((num > 0) ? "Odd and positive \n" : "Odd and negative \n");
        }
    }
    else
    {
        std::cout << "The number is zero \n";
    }
}

positive, negative, Even and Odd do not work for INT.

Remove all the lines where you put integer =


That was my problem. Now it works seamlessly. Thank you!
Topic archived. No new replies allowed.