【问题标题】:Why does my program stop with an error message?为什么我的程序停止并显示错误消息?
【发布时间】:2017-05-05 11:56:56
【问题描述】:

我在 Visual Studio (VS) 中编写了一些 C++ 代码,其中使用了一些命令行参数; (argc, argv).

但是,当我从 VS 内部运行它时,它会显示此错误消息并停止工作。

该项目是一个 Win32 控制台应用程序。

另外,我不知道在 VS 中运行时如何为程序提供参数。

代码:

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]) {
    if (argc != 3)
        cerr << "Please input valid file name";
    int year;
    ifstream in(argv[1]);
    ofstream out;
    out.open(argv[2], ofstream::app);
    if (out) {
        //cout << "Input year:" << endl;
        while (in >> year)
            if ((year % 4) == 0 && (year % 100) != 0 || (year % 400) == 0)
                out << year << " is a leap year" << endl;
            else   out << year << " is not a leap year" << endl;
    }
    else
        cerr << "can not open! Try again";
    return 0;
}

【问题讨论】:

  • 您的代码有错误。
  • 如果我们没有看到您的代码,您究竟希望我们如何知道导致编译错误的原因?我们不介意读者。
  • 1.如果程序无法编译,调试器 start 是如何工作的? 2.不要链接代码图片!您正在使用文本输入表单,只需粘贴代码。 3.您的问题似乎是您的所有代码都被注释掉了。或者编译器对您选择的字体感到不安,这是可以理解的。
  • sorry,这是我第一次在这里提问,我会给出代码。
  • @StephenLiao 请阅读How to Ask,以及如何提供minimal reproducible example,然后edit您的问题并请求重新打开它。

标签: c++ visual-studio


【解决方案1】:

正如 cmets 中所述并由 OP 解决:在检查参数数量时,如果检查失败,您可能无法继续使用参数。要从 VS 中向程序传递参数,请参阅 this question

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 2021-03-04
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多