这个C++代码有什么问题?(未声明的标识符)

1 c++ visual-c++

得到此错误: (68): error C2065: 'programend' : undeclared identifier

(偏离主题的说明:我知道使用命名空间std是不好的做法,但我不想在所有内容中输入std ::然而,如果那是导致错误的原因,我会这样做.)

这是代码:

#include <iostream>

using namespace std;

int main(void) {
    do{
    system("title Mini-Calc");
    cout << "Hello World!  Welcome to Dustin's C++ Calculator!" << endl;
    cout << "To get started, enter a number:" << endl;

    int operation;
    double num1, num2, answer;

    cin >> num1;
    cout << "Now enter another number:" << endl;
    cin >> num2;

    cout << "Please type the corrresponding number for the operation desired, and press enter." << endl;
    cout << "1 = +, 2 = -, 3 = x, 4 = /" << endl;

    cin >> operation;

    switch(operation) {
        case 1:
            answer=num1+num2;
        break;

        case 2:
            answer=num1-num2;
        break;

        case 3:
            answer=num1*num2;
        break;

        case 4:
            answer=num1/num2;
        break;

    }

    cout << "The answer is: " << endl;
    cout << answer << endl;

    bool programend;

    cout << "Would you like to end the program? (y for yes, n for no)" << endl;

    cin >> programend;

    switch(programend) {
        case 'y':
            programend=true;
        break;

        case 'n':
            programend=false;
        break;

        case 'Y':
            programend=true;
        break;

        case 'N':
            programend=false;
        break;
        }
    } while (programend==false);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

qdi*_*dii 5

如果您取出do ... while内容,您将看到programend未在正确的范围内声明:

int main(void) {
    do{
        ...
    } while (programend==false);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它应该在main和之间声明do.