首页 文章

代码块退出(0)错误? [关闭]

提问于
浏览
-5

我正在学习c并且正在使用Visual Studios但我今天切换到代码块并且遇到了麻烦 . 我的编译器是MinGW,如果这很重要的话 . 无论如何,我做了一个看起来像这样的练习程序:

#include <iostream>

using namespace std;

Int main()
{
Int x;
cin >> x;
If(x == 1)
Exit(0)
Return 0;
}

当我构建它时,我在包含Exit(0)的行上出现错误 . 为什么是这样?另外,如何让底部的构建/调试工具栏重新出现?

2 回答

  • 3
    #include <iostream>
    #include <stdlib.h>   
    
    int main(){
       using namespace std;
       int x;
       cin >> x;
       if(x == 1) {exit(0);}
       return 0; 
     }
    
  • 5

    C和C区分大小写 . 这会导致代码出现许多问题(不仅仅是 Exitexit ) .

    除此之外,作为C的简单规则,忘记 exit 无论如何都存在 . 它还没有足够地研究C来知道RAII是什么,只要相信我这是一件非常糟糕的事情 .

    #include <iostream>
    #include <cstdlib>
    
    int main() {
        int x;
        std::cin >> x;
        if(x == 1)
            return EXIT_FAILURE; // return something different from the normal exit
        return 0;
    }
    

    如果您需要退出main,只需使用 return ,因为我已经更改了上面的代码 . 如果由于真正严峻的紧急情况需要退出其他地方(例如,你've detected such a massive problem that even trying to shut down cleanly is likely to destroy the user'的数据),你可能想要使用 abort() . 否则,您可能希望抛出一个异常,它将传播回 main ,并从那里退出(在正确执行析构函数之后) .

相关问题