gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:26:59 25 4
gpt4 key购买 nike

出现此错误:(68): error C2065: 'programend' : undeclared identifier

(题外话:我知道使用 using namespace 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;
}

最佳答案

如果你取出 do...while 内容,你会看到 programend 没有在正确的范围内声明:

int main(void) {
do{
...
} while (programend==false);
return 0;
}

它应该在 maindo 之间声明为可用。

关于c++ - 这段 C++ 代码有什么问题? (未声明的标识符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com