如何返回0并从函数终止
How do I return 0 and terminate from a function?
在下面的代码中,如果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;
}
包括<stdlib.h>
在你的头,把exit(0)
而不是返回0,这对我来说是有效的:)
相关文章:
- 为什么我的 while 函数没有终止?
- 在字符串函数中抛出'char const*'实例后调用的终止
- 程序终止时调用函数
- 当加入 C++11 函数的线程仍未终止时,是否可以返回?
- 在使用标准向量函数时引发'std::bad_alloc'实例后调用的终止
- 可变参数模板中递归模板函数的终止问题
- 如何提前终止在另一个文件中定义的函数?
- 如何在执行期间终止(或返回)具有结构类型的自动函数
- 我在执行 levelOrder 函数时无法弄清楚异常终止的原因?
- c 在函数的循环终止时
- 为什么我的析构函数中的异常没有触发 std::终止?
- 如何使用unique_ptr声明调用构造函数并处理程序终止信号
- 程序在函数返回后被终止
- 程序是否可以在调用kill函数之前返回/终止
- Windows资源监视器使用哪些Win32函数来检测已终止进程的磁盘和网络活动
- CMake 解析错误函数缺少结尾")"。而是找到带有文本的未终止字符串")
- 非终止constexpr函数定义明确
- cin函数是否添加了在输入结束时终止的null
- 如果在可变参数模板解包中达到终止函数,则出现异常
- 如何返回0并从函数终止