2

I want to print the even and odd in a string rather than in binary for true false | 1 / 0; Why the output still print the boolean value?

int main() {
    printf("enter the two numbers\n");
    int a;
    int b;
    cin >> a >> b;

    string arr[9] = {"one","two","three","four","five","six","seven","eight","nine"}; 
    for (int i = a; i <= b; i++)
    {
        if(i <=9){
           cout << arr[i-1] << "\n";
        }else{
           cout << ( i%2 == 0)? "even" : "odd"; // print out 1/0 but did not print out the even and odd
           cout << "\n";
        }
    }

    return 0;
}

output:

enter the two numbers
9 16
nine
1
0
1
0
1
0
1
1

1 Answer 1

4

You are missing parens to enclose the expression. The ternary has lower precedence than <<.

cout << (( i%2 == 0)? "even" : "odd");
Sign up to request clarification or add additional context in comments.

3 Comments

Also, the parentheses around i%2 == 0 aren't needed.
@PeteBecker True, but those of us who have to look at the chart on our wall every time would be tempted to put them in anyway.
@stark -- (I (understand (what (you're talking about)))).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.