Passing command-line arguments to my c++ program t

2019-09-02 02:44发布

I have just started using Visual Studio 2012, which I wish to use to write C++ code.

To get to know how the IDE works I have created a very simple 'add two numbers' program.

#include<iostream>
#include<cstdlib>

int add(int a , int b)
{
  return a+b;
}

int main(int argc, char** argv)
{

  int a = atoi(argv[1]);
  int b = atoi(argv[2]);
  std::cout << "The sum of the arguments is  " << add(a,b)      << std::endl;  
  return 0;
}

If I hard-code the values of 'a' and 'b' (ie no argv's require to be passed), then using F7 to compile and Ctrl F5 to run the executable makes the program run perfectly.

But how do I pass the argv's to the program if I want to specify them at run time?

According to the 2nd answer on this SO thread I have to use Project Tab-> Properties-> Configuration Properties-> Debugging and then enter in the Right Hand Side under (Command Arguments),

But this seems very inconvenient if I want to make several quick runs of the program and test out the executable for different 'argv's.

1条回答
Emotional °昔
2楼-- · 2019-09-02 03:10

You can try CLI Args Made Easy from http://n0n4m3.codingcorner.net/?p=214. There are versions for VS2010 and VS2012.

  1. You may need to rename the downloaded file from .zip to .vsix, eg: CLIArgsMadeEasy2012.vsix
  2. Double-click to install it.
  3. Run Visual Studio.
  4. Show the toolbar by right-clicking the toolbar area, and click CLIArgsMadeEasy
  5. The toolbar appears with one textbox for command line arguments (CLIArgs) and a combobox (Startup Project)
  6. Enter the argument in CLIArgs and remember to press the Enter key, otherwise the arguments will not be saved.
  7. Run your program.
查看更多
登录 后发表回答