收到此错误:(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;
}
答案 0 :(得分:5)
如果您取出do ... while内容,您会看到programend
未在正确的范围内声明:
int main(void) {
do{
...
} while (programend==false);
return 0;
}
应在main
和do
之间声明可用。
答案 1 :(得分:0)
您将programend
声明为布尔值,但检查为字符
bool programend;
将其更改为
char programend;
或为此目的使用两个不同的变量,如下面的
bool programend;
char choice;
cout << "Would you like to end the program? (y for yes, n for no)" << endl;
cin >> choice;
switch(choice) {
case 'y':
programend=true;
break;
答案 2 :(得分:0)
你的programend声明在do内部块中,但是你在终止中测试它:你可以移到外面,例如在顶部。并初始化它:
int main(void) {
bool programend = false;
do {
} while (...)
}
并声明一个用于读取cin的变量:
int ch;
cin >> ch;
switch (ch) {
...
}