-7

I have written some C++ code in Visual Studio (VS), which uses some command line parameters; (argc, argv).

However, when I run it from inside VS, it shows this error message and stops working.

error message

The project is a Win32 Console application.

Also, I don't know how to provide arguments to the program when running from within VS.

Code:

#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;
}
stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • 1
    You have an error in your code. – Pete Becker May 05 '17 at 11:57
  • 9
    How on earth do you expect us to know what is causing a compile error if we don't see your code? We aren't mind readers. – Cory Kramer May 05 '17 at 11:57
  • 2
    1. How does the debugger _start_ working if the program doesn't compile? 2. Don't link to pictures of code! You're using a text entry form, just paste the code. And 3. your problem appears to be that all your code is commented out. Or the compiler is upset at your choice of typeface, which would be understandable. – Useless May 05 '17 at 11:59
  • 3
    sorry,this is the first time I ask questions here .i will give the code. – Xudong Liao May 05 '17 at 12:00
  • 1
    @StephenLiao Please, PLEASE read [ask], and how to provide a [mcve], then [edit] your question and request for it to be reopened. – gravity May 05 '17 at 12:00
  • No need to apologise if you fix it, but there's a [help center](https://stackoverflow.com/help) and lots of existing questions to help you figure it out. – Useless May 05 '17 at 12:01
  • What does *The mistake is that the debugger stops working* mean? The program starts to work and then just stalls? You get some sort of error message? – NathanOliver May 05 '17 at 12:13
  • I just run it without debugging,then get this error message. – Xudong Liao May 05 '17 at 12:23
  • @StephenLiao What error message? **Copy-paste** it to the question. – Algirdas Preidžius May 05 '17 at 12:45
  • The first mistake that I see in the code is that it continues when the check for the number of arguments fails. It is best to `return -1` after the std::err-message. The VS-debugger is very good, if you used it to step through your code, you would have spotted that already. – stefaanv May 05 '17 at 12:51
  • @stefaanv thank you very much! 谢谢 , i modify my code referring to your advice.It can debug well. But i can not input the cmd parameters, because i get the std::cerr message as long as i just run the cpp file in vs not by using GCC to get a ".exe" file.How can i do to solve it. – Xudong Liao May 05 '17 at 13:06
  • I hope this helps: http://stackoverflow.com/questions/3697299/passing-command-line-arguments-in-visual-studio-2010 – stefaanv May 05 '17 at 13:10
  • By the way: avoid confusion between compiling, running and debugging: http://www.site.uottawa.ca/~lucia/courses/2131-06/labs/Lab1/index.html (not perfect, but it gives an idea) – stefaanv May 05 '17 at 13:12
  • @stefaanv Though the first experience I ask question here is not very perfect, it is satisfied!Thank you very much ! – Xudong Liao May 05 '17 at 13:21
  • @StephenLiao: you still have to accept the fact that you present a question and code to experienced software people who volunteer to answer questions. Your question (especially without the code) was pretty bad. It was getting better and I just updated it to be at least less confusing. The better the question (according to links provided by gravity), the more chance you have on a good answer and thus the better the experience. I hope this doesn't stop you from asking other questions. However, as I already said, stepping through your code with the debugger, would have been more efficient. – stefaanv May 05 '17 at 15:39
  • @StephenLiao: you are welcome to edit the question yourself or even revert – stefaanv May 05 '17 at 15:40

1 Answers1

1

As already stated in the comments and solved by the OP: when checking the number of arguments, you may not continue and use the arguments anyway if the check fails. To pass arguments to the program from within VS, see this question.

Community
  • 1
  • 1
stefaanv
  • 14,072
  • 2
  • 31
  • 53