为什么with和条件的语句无法按预期工作?

问题描述 投票:0回答:1

类似的问题不是我要问的。

这里,

#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);
}

如果输入数字以外的数字,它将再次循环运行。如果我输入的数字甚至是0或负数,它将直接结束循环并打印else语句,如果if会发生其他情况?

c++
1个回答
0
投票

我认为您想使用or代替and中的else if (inputX <= 0 && inputX > 18)

它将检查else if,您的代码是否像这样:

else if (inputX <= 0 || inputX > 18)
© www.soinside.com 2019 - 2024. All rights reserved.