C++ 未在此范围内声明输入(第41行) #包括 使用名称空间std; void menue() { cout

C++ 未在此范围内声明输入(第41行) #包括 使用名称空间std; void menue() { cout,c++,c++11,variables,scope,do-while,C++,C++11,Variables,Scope,Do While,变量input在do while语句的块范围(复合语句)内声明 #include <iostream> using namespace std; void menue() { cout<<"Choose an option"<<endl; cout<<"========================"<<endl; cout<<"1. Open"<<endl; cout&l

变量
input
在do while语句的块范围(复合语句)内声明

#include <iostream>
using namespace std;

void menue()
{

    cout<<"Choose an option"<<endl;
    cout<<"========================"<<endl;
    cout<<"1.  Open"<<endl;
    cout<<"1.  Close"<<endl;
    cout<<"1.  Exit"<<endl;

}

void menueOptions()
{

    do
    {
        int input;
        cout<<"Enter selection here:  "<<flush;
        cin >> input;

        switch(input)
        {

        case 1:
            cout<<"Opening..."<<endl;
            break;
        case 2:
            cout<<"Closing..."<<endl;
            break;
        case 3:
            cout<<"Exiting..."<<endl;
            break;
        default:
            cout<<"Invalid input"<<endl;

        }
    }
while(input < 1 || input > 3); // ERROR HERE (INPUT WAS NOT DECLARED IN THIS // SCOPE)
}

int main()
{

    menue();
    menueOptions();


return 0;
}
do
{
int输入;
//...
}
while(输入<1 | |输入>3);//此处有错误(输入未在此//范围中声明)
}
因此,在do while语句的条件下,不能在范围外使用它

(注意:即使do while语句不使用复合语句,它在do和while之间也有自己的块作用域。来自C++17标准(8.5迭代语句)

2迭代语句中的子语句隐式定义了 块范围(6.3),每次通过 如果迭代语句中的子语句是单个 语句而不是复合语句,就好像它被重写了一样 是包含原始语句的复合语句

--(完)

在do while语句之前声明它

    do
    {
        int input;
        //...
    }
while(input < 1 || input > 3); // ERROR HERE (INPUT WAS NOT DECLARED IN THIS // SCOPE)
}
int输入;
做
{
//...
}
而(输入<1 | |输入>3);//此处出错(未在此//范围中声明输入)
}

谢谢;那是个愚蠢的决定mistake@Coalyboi如果答案有助于你解决问题,你应该接受。
    int input;
    do
    {
        //...
    }
while(input < 1 || input > 3); // ERROR HERE (INPUT WAS NOT DECLARED IN THIS // SCOPE)
}