0

The similar question is not what I asking.

Here,

#include <iostream>

using namespace std;

int main(){
    int base, inputX;
    int aCounter = 0;

    do {
        std::cout << "Please enter a value between 1-10" << endl;
        std::cin >> inputX;

        if (!cin) {
            std::cout << "Error! number only" << endl;
            std::cin.clear();
            std::cin.ignore(100, '\n');
        }else if (inputX <= 0 && inputX > 18) {
            std::cout << "Error! please enter a correct number" << endl;
        }else{
            base = inputX;
            std:;cout << "The number for base is " << base << "." << endl;    
            aCounter++ ;
        }
    }while (aCounter == 0);
}

If I input other than number, it loop again and run well. If I input a number even is 0 or negative, it straight end the loop and print the else statement, what happen to else if?

unDeadHerbs
  • 1,306
  • 1
  • 11
  • 19
nelLuffy
  • 33
  • 6
  • 7
    How can `inputX` be less than or equal to 0 __and at the same time__ greater than 18? – tkausl Nov 30 '19 at 23:40
  • 1
    `std:;cout` should also be be fixed. To avoid this 'silent issue', don't import `std`. – user2864740 Nov 30 '19 at 23:41
  • 1
    See [why you shouldn't use `using namespace std;`](https://stackoverflow.com/q/1452721/9254539). You're already using `std::` before `cin` and `cout` so you can just remove the line after fixing the typo. – eesiraed Dec 01 '19 at 00:05
  • sorry for typo in code, change to || solve all, i got the logic now thank you all for answer. – nelLuffy Dec 01 '19 at 00:44
  • another topic in my code, is the loop method i use is good or still can minimize the code and do the same? – nelLuffy Dec 01 '19 at 00:54

1 Answers1

0

I think you want to to use or instead of and in else if (inputX <= 0 && inputX > 18)

It will check else if if your code like this:

else if (inputX <= 0 || inputX > 18)
Bill Chen
  • 1,699
  • 14
  • 24