c++ If/Else语句切换错误
C++ If/Else statement in switch case error
我使用下面的开关情况,这一节不允许用户能够重新输入他们想要输入的值,并在程序中重复显示相同的代码。
case 5: iMenu = 5;
{
cout << "Are you sure you would like to exit? Type 1 or 2." << endl << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
int iExit;
cin >> iExit;
if (iExit == 1)
{
return 0;
}
if (iExit == 2)
{
goto CalculatorMenu;
}
else
{
cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
cin >> iExit;
}
}
它无休止地重复案件开头的判决。对此的任何帮助将不胜感激,如果需要,我可以显示整个程序的更多代码。非常感谢。
感谢大家的输入到目前为止,不幸的是它还没有设法解决我的问题,所以我认为我搞砸了早期的编码。这是我的整个开关箱。 CalculatorMenu: //name of menu
cout << "Which function of this calculator would you like to do?" << endl;
cout << "1. Add Two Numbers" << endl;
cout << "2. Subtract one number from another" << endl;
cout << "3. Multiply Two Numbers" << endl;
cout << "4. Divide one number by another" << endl;
cout << "5. Exit" << endl;
cin >> iMenu;
switch (iMenu)
{
case 1: iMenu = 1;
cout << "In this section you can add two numbers together. Please insert one number" << endl << endl;
cin >> iMenu;
int iAdd1;
iAdd1 = iMenu;
cout << "Please insert another number" << endl << endl;
cin >> iMenu;
int iAdd2;
iAdd2 = iMenu;
int iAddResult;
iAddResult = iAdd1 + iAdd2;
cout << "The addition of " << iAdd1 << " + " << iAdd2 << " = " << iAddResult << endl << endl;
goto CalculatorMenu;
case 2: iMenu = 2;
cout << "In this section you can subtract one number from another. Please insert one number" << endl << endl;
cin >> iMenu;
int iSub1;
iSub1 = iMenu;
cout << "Please insert another number" << endl << endl;
cin >> iMenu;
int iSub2;
iSub2 = iMenu;
int iSubResult;
iSubResult = iSub1 - iSub2;
cout << "The subtraction of " << iSub1 << " - " << iSub2 << " = " << iSubResult << endl << endl;
goto CalculatorMenu;
case 3: iMenu = 3;
cout << "In this section you can multiply two numbers together. Please enter a number" << endl << endl;
cin >> iMenu;
int iMult1;
iMult1 = iMenu;
cout << "Please enter another number" << endl << endl;
cin >> iMenu;
int iMult2;
iMult2 = iMenu;
int iMultResult;
iMultResult = iMult1 * iMult2;
cout << "The multiplication of " << iMult1 << " * " << iMult2 << " = " << iMultResult << endl << endl;
goto CalculatorMenu;
case 4: iMenu = 4;
cout << "In this section you can divide one number by another number. Please enter a number" << endl << endl;
float fDiv1;
cin >> fDiv1;
if (fDiv1 == 0)
do
{
cout << "Please do not divide by 0, please enter another number" << endl << endl;
cin >> fDiv1;
} while (fDiv1 == 0);
cout << "Please enter another number" << endl << endl;
float fDiv2;
cin >> fDiv2;
if (fDiv2 == 0)
do
{
cout << "Please do not divide by 0, please enter another number" << endl << endl;
cin >> fDiv2;
} while (fDiv2 == 0);
float fDivResult;
fDivResult = fDiv1 / fDiv2;
cout << "The division of " << fDiv1 << " / " << fDiv2 << " = " << fDivResult << endl << endl;
goto CalculatorMenu;
case 5: iMenu = 5;
{
cout << "Are you sure you would like to exit? Type 1 or 2." << endl << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
int iExit;
cin >> iExit;
while (iExit != 1 && iExit != 2)
{
cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
cin >> iExit;
}
if (iExit == 2)
{
goto CalculatorMenu;
}
if (iExit == 1)
{
return 0;
}
}
default:
{
if (iMenu != 1 || 2 || 3 || 4 || 5)
cout << "Please enter a valid number" << endl << endl;
goto CalculatorMenu;
}
system("Pause");
}
}
Edit:我看了你的代码,我发现了几个错误:
- 当你输入数据时,你应该将数据从原始格式转换为int…否则,不包含数字的字符串将挂起您的程序。
- 在默认情况下,你没有正确地比较if… 我不明白你的系统("暂停")功能是什么…也许sleep()可以延迟再次显示菜单?
- 另外,正如我在我最初的回复中所说的,你不应该在c++中使用GOTO,因为它被认为是一种糟糕的编程实践(有很多关于这个主题的帖子,例如:GOTO仍然被认为是有害的?)
下面是更正后的代码:
enum STR2INT_ERROR { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };
STR2INT_ERROR str2int (int &i, std::string &s, int base = 0)
{
char *end;
const char *c= s.c_str();
long l;
errno = 0;
l = strtol(c, &end, base);
if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
return OVERFLOW;
}
if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
return UNDERFLOW;
}
if (*c == ' ' || *end != ' ') {
return INCONVERTIBLE;
}
i = l;
return SUCCESS;
}
int main(){
std::string menuOpt;
int iMenu=-1;
bool done=false;
while(!done){
cout << "Which function of this calculator would you like to do?" << endl;
cout << "1. Add Two Numbers" << endl;
cout << "2. Subtract one number from another" << endl;
cout << "3. Multiply Two Numbers" << endl;
cout << "4. Divide one number by another" << endl;
cout << "5. Exit" << endl;
cin >> menuOpt;
if( str2int(iMenu,menuOpt) != SUCCESS){
cout << "Please enter a valid number" << endl;
continue;
}
switch (iMenu)
{
case 1:
iMenu = 1;
cout << "In this section you can add two numbers together. Please insert one number" << endl << endl;
cin >> menuOpt;
int iAdd1;
iAdd1 = iMenu;
cout << "Please insert another number" << endl << endl;
cin >> iMenu;
int iAdd2;
iAdd2 = iMenu;
int iAddResult;
iAddResult = iAdd1 + iAdd2;
cout << "The addition of " << iAdd1 << " + " << iAdd2 << " = " << iAddResult << endl << endl;
break;
case 2:
iMenu = 2;
cout << "In this section you can subtract one number from another. Please insert one number" << endl << endl;
cin >> iMenu;
int iSub1;
iSub1 = iMenu;
cout << "Please insert another number" << endl << endl;
cin >> iMenu;
int iSub2;
iSub2 = iMenu;
int iSubResult;
iSubResult = iSub1 - iSub2;
cout << "The subtraction of " << iSub1 << " - " << iSub2 << " = " << iSubResult << endl << endl;
break;
case 3:
iMenu = 3;
cout << "In this section you can multiply two numbers together. Please enter a number" << endl << endl;
cin >> iMenu;
int iMult1;
iMult1 = iMenu;
cout << "Please enter another number" << endl << endl;
cin >> iMenu;
int iMult2;
iMult2 = iMenu;
int iMultResult;
iMultResult = iMult1 * iMult2;
cout << "The multiplication of " << iMult1 << " * " << iMult2 << " = " << iMultResult << endl << endl;
break;
case 4: iMenu = 4;
cout << "In this section you can divide one number by another number. Please enter a number" << endl << endl;
float fDiv1;
cin >> fDiv1;
if (fDiv1 == 0)
do
{
cout << "Please do not divide by 0, please enter another number" << endl << endl;
cin >> fDiv1;
} while (fDiv1 == 0);
cout << "Please enter another number" << endl << endl;
float fDiv2;
cin >> fDiv2;
if (fDiv2 == 0)
do
{
cout << "Please do not divide by 0, please enter another number" << endl << endl;
cin >> fDiv2;
} while (fDiv2 == 0);
float fDivResult;
fDivResult = fDiv1 / fDiv2;
cout << "The division of " << fDiv1 << " / " << fDiv2 << " = " << fDivResult << endl << endl;
break;
case 5: iMenu = 5;
{
cout << "Are you sure you would like to exit? Type 1 or 2." << endl << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
int iExit;
cin >> iExit;
while (iExit != 1 && iExit != 2)
{
cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
cin >> iExit;
}
if (iExit == 1)
{
done=true;
}
break;
}
default:
{
if (iMenu != 1 && iMenu !=2 && iMenu !=3 && iMenu !=4 && iMenu !=5)
cout << "Please enter a valid number" << endl << endl;
}
break;
//system("Pause");
}
}
}
注意:我使用了从这里获得的str2int()方法的修改版本:
另外,请注意,我只将第一个输入转换为int,您需要在每次向用户请求数字输入时都这样做…
如果要分别检查多个条件,则需要在' If '后面加上'else-if'。
int iExit;
cin >> iExit;
if (iExit == 1)
{
return 0;
}
else if (iExit == 2)
{
goto CalculatorMenu;
}
else
{
cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
cin >> iExit;
}
也许EOF输入可以解决你的问题:
cout << "Are you sure you would like to exit? Type 1 or 2." << endl << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
int iExit;
while(cin>>iExit){
if(iExit == 1){
return 0;
}else if(iExit == 2){
//goto ...
}
else{
cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
continue;
}
}
您没有发布整个开关块,因此您可能会忘记使用break
终止case块。
相关文章:
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 语句错误:"","标记之前有"预期的')'
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- 为什么切换 if else 语句的顺序会导致错误?
- 错误:调用 .. at return 语句时没有匹配函数
- 为什么 if 语句在 Java 中"if(1)"给出错误,但在C++中成立?
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 如果语句错误地读取字符串,则始终在第一个条件返回
- 表单显示对话框函数错误,并且不执行下面的语句
- 我收到一个断言错误,但是当我编写 cout 语句时,它会消失
- 我可以使用 try catch 语句来捕获任何错误而不是具体错误吗?
- 我在c++中遇到了一个奇怪的错误,其中一个计算2个小整数加法的语句溢出到一个长值中
- Qt SQL LIKE语句返回错误
- 错误:语句无法解析重载函数C++的地址
- 当我通过删除临时节点来释放内存时,我会得到读取访问违规行为.但是只有当我返回错误语句时
- C++显示错误的 constexpr 错误语句
- C++错误:语句无法解析重载函数的地址
- C++:在CodeBlocks上编译和运行的程序可以工作,但从终端它给出错误语句