C++ C++;不要在循环不工作时执行此操作

C++ C++;不要在循环不工作时执行此操作,c++,loops,do-while,C++,Loops,Do While,我正在努力使此代码正常工作: #include <iostream> using namespace std; int main() { int i; do { cout << ("please enter a number between 1 and 10"); cin >> i; } while(i > 10 && i < 1) cout << "the square of the num

我正在努力使此代码正常工作:

#include <iostream>

using namespace std;

int main()
{

int i;
do
{
    cout << ("please enter a number between 1 and 10");
    cin >> i;

} while(i > 10 && i < 1)
  cout << "the square of the number you have entered is " << i*i;
}
#包括
使用名称空间std;
int main()
{
int i;
做
{
cout>i;
}而(i>10&&i<1)
您是否有:

while (i > 10 && i < 1)
while(i>10&&i<1)
你想要:

while (i > 10 || i < 1)
while(i>10 | | i<1)

您应该使用或
|
,带有
&
的条件永远不会为真。

如其他人所述,您的条件是错误的。
while (i > 10 && i < 1)
一个数字不能同时小于1和大于10,因此while循环在do语句之后立即退出

#include <iostream>

using namespace std;

int main()
{

    int i;
    do
    {
        cout << ("please enter a number between 1 and 10");
        cin >> i;

    } while (i < 1 || i > 10)

    cout << "the square of the number you have entered is " << i*i;
}
#包括
使用名称空间std;
int main()
{
int i;
做
{
cout>i;
}而(i<1 | | i>10)

cout循环条件错误,将永远不会循环,因为
i
不能同时小于1
&
大于10。您应该使用逻辑OR(
|
)运算符。此外,do while语句后面必须有一个分号。您可能希望在提示符后面放一个分号和行尾。此外,您不想养成污染全局命名空间的坏习惯,即使使用了
std
。因此:

#include <iostream>

int main()
{
    int i;
    do {
        std::cout << "please enter a number between 1 and 10\n";
        std::cin >> i;
    } while (i > 10 || i < 1);

    std::cout << "the square of the number you have entered is " << i*i << std::endl;
}
#包括
int main()
{
int i;
做{
std::cout>i;
}而(i>10 | | i<1);

std::无法将条件从“and”更改为“or”。请仔细考虑您的代码。
i
如何同时大于10和小于1?我想您的意思是:
,而(i>1&&i<10)
用您的方式为大家欢呼(高性能分数)使其更具意义考虑到为OP添加说明
#include <iostream>

int main()
{
    int i;
    do {
        std::cout << "please enter a number between 1 and 10\n";
        std::cin >> i;
    } while (i > 10 || i < 1);

    std::cout << "the square of the number you have entered is " << i*i << std::endl;
}