在下面,我希望程序在function2中“condition”小于3时退出,但此时它只是在switch语句后继续执行代码。我如何实现目标?
#include <iostream>
using namespace std;
int main ()
{
string answer;
int selection;
do
{
cin >> selection;
switch(selection)
{
case 1:
function1();
break;
case 2:
function2();
break;
default:
break;
}
cout << "Anything else?" << endl;
cin >> answer;
}while(!(answer == "no"));
return 0;
}
void function1()
{
//do stuff
}
int function2()
{
int condition;
cin >> conditon;
if(condition < 3)
{
cout << "That's an error." << endl;
return 0;
}
return 0;
}
答案 0 :(得分:1)
在标题中添加<stdlib.h>
并将exit(0)
放回{0},而不是返回0,这对我有效:)