user14400886
user14400886

Reputation:

Cannot execute the else statement it gives out an error

** I wanted to print each integer (n) between b and c such that if n>9 else statement executes and prints out each integers in the form of even or odd.The if statements executes if the following condition of the variables of c and b are true.So there is no problem if i input c and b both <9.
**

#include <iostream>
#include <cstdio>
using namespace std;

int main() {

      int b;
      int c;

      string x[9] = {"one","two","three","four","five","six","seven","eight","nine"};

      cin >> c;
      cin >> b;

      for (int r=0 ; c <= b ; c+1) {

           c=c+1+r;

           if (1<= c <= 9) {
                 cout << x[c-2] << endl;
           } else {
                 if (c%2  != 0) {              
                     cout << "odd" << endl;
                 } else {
                     cout << "even" << endl;
                 }
           }
       c;
    }

    return 0;
}

Upvotes: 1

Views: 49

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

The condition 1<= c <= 9 won't work as you expected.

1<= c <= 9 is treated as (1 <= c) <= 9.

1 <= c here will be treated as 0 or 1, so the condition (1 <= c) <= 9 will always be true.

You should use 1 <= c && c <= 9 instead.

Upvotes: 5

Related Questions