Menu

Using Command-line arguments in DevC++

Drewsky
2006-05-17
2012-09-26
  • Drewsky

    Drewsky - 2006-05-17

    I have a problem using command-line arguments in DevC++. As a test, I compiled the following program:

    include <iostream>

    int main(int argc, char **argv)
    {
    std::cout << "Received " << argc << " arguments...\n";
    for (int i=0; i<argc; i++)
    std::cout << "argument " << i << ": " << argv[i] << std::endl;
    return 0;
    }

    I then went to the 'Execute' pulldown menu in DevC++ and selected 'Parameters...' There I typed in some 'Parameters to pass to your program' and clicked on the OK button. I then ran the program. The results are as follows:

    Received 1 arguments...
    argument 0: C:\Project1.exe
    Press any key to continue . . .

    The only parameter the program is picking up is the filename. Yet when I run the program from the command line with parameters in a console window the program picks up all the parameters I type in. What am I doing wrong in the IDE?

     
    • Wayne Keen

      Wayne Keen - 2006-05-17

      Which version number of Dev are you using?

      When I run your code (I added a system("pause") so I could see my results), with parameters to pass to your program set to

      1 2 3 4

      The program indicates that it got 5 parameters, the 0th being the program name, and the rest being the numbers.

      I compiled my code, and then hit

      Exectute:Run

      in the IDE to run it. I am running version 4.9.9.2 in Windows XP Pro.

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2006-05-17

      My output looked just like this output from running at the MSYS prompt:

      $ ./testit.exe 1 2 3 4
      Received 5 arguments...
      argument 0: c:\dev-cpp\testit.exe
      argument 1: 1
      argument 2: 2
      argument 3: 3
      argument 4: 4
      Press any key to continue . . .

       
    • Drewsky

      Drewsky - 2006-05-19

      Thanks for the advice Wayne, I'll be cautious!

       
    • Drewsky

      Drewsky - 2006-05-18

      Hi Wayne. I am running DevC++ version 4.9.8.3 in Windows XP Pro. I did it exactly as you described, but the parameters I put in 'Parameters to pass to your program' are not being picked up. I only get parameter 0, the program name.

       
    • Nobody/Anonymous

      I just tried it on mine, compiled as a single file (no project), WinXP Pro, version 4.9.9.2
      I got the same results as Wayne, works as expected.

      You could try opening a command prompt, change to the directory the program is in and run it from there, if that works then it would most likely be some problem with Dev.

      You might want to reinstall with the latest version (follow the uninstall/reinstall directions in the 'read this before posting' thread carefully!

      b_a

       
    • Drewsky

      Drewsky - 2006-05-18

      I also originally tried compiling it as a single file as you did, but that didn't work. Then I tried it as a project because I thought there may be something in the auto-generated makefile that accepts the arguments. I'm going to try the later version of DevC++ (4.9.9.2) because I also tried running it from the command prompt and that worked. Thanks, I'll let you know what happens...

       
    • Wayne Keen

      Wayne Keen - 2006-05-18

      4.9.8.3

      This is an old version of Dev, which will make getting help (and Dev-Packs) harder. I recommend you do a clean uninstall of it (see the "Please Read Before Posting a Question" thread for directions on doing that, and please follow them) and install the current version of Dev, 4.9.9.2

      Wayne

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.