请看这个:
int f();
int g();
int main()
{
f();
g();
return 0;
}
int f()
{
// In case of fail
return 1;
}
int g()
{
return 0;
}
如果error
中有f()
,我怎样才能立即退出main
而不运行g()
并从0
返回main
}。
这样做的正确方法是什么?
答案 0 :(得分:2)
您可以退出以下应用程序:
int main()
{
if (f())//if statement does not require `}` if it has only one statement. Here it is only `exit(1)` statement.
exit(1);// error exit, you can use return 1; as well. Because here f() returns 1
g();
exit(0); // correct execution exit. You can use return 0; as well.
}
现在您可以检查退出状态,如下所示: -
UNIX / LINUX
echo $?
WINDOWS
echo %ERRORLEVEL%
答案 1 :(得分:0)
int f();
int g();
int main()
{
int value;
if(!f()){
return 0;
}else{
value= g();
}
return value;
}
int f()
{
if(stat=='correct'){
// In case of success
return 1;
}
else{ // In case of fail
return 0;}
}
int g()
{
if(stat=='correct'){
// In case of success
return 1;
}
else{ // In case of fail
return 0;}
}
答案 2 :(得分:-1)
你可以这样做:
int main()
{
if(!f()){
return 0;
}else{
g()
}
}