I have a problem that seems like it should be easy but so far has been fairly difficult to find documentation on... I want to be able to pass parameters to a program in the command line in the same manner as the built in functions (ex. the "/r" in C:\Windows\chkdsk /r). I want my program to be able to accept a parameter, such as "<My Book>", by adding it right after the program name. Could I add multiple parameters? My hope is to produce an index of my bank record books and access it by simply typing something similar to "C:\MyBooks Book_Number_1 Page_Number_31"

I have tried this with no success:


#include <iostream>
using namespace std;

int main(int choice1, int choice2)

      //my program here using choice1 and choice2

     return 0;


10 Years
Discussion Span
Last Post by Prabakar

Here is how to test command-line arguments. Run this program from a cmd.exe command prompt to see how to get the arguments into the program

#include <iostream>
using std::cout;

int main(int argc, char* argv[])
   for(int i = 0; i < argc; i++)
       cout << argv[i] << "\n";

One last question: how can I convert between argv[x] and a regular int? I have tried

int * temp;
temp = argv[x];

but the compiler keeps telling me I cannot convert from char* to int*. I know there is a way but why can I not convert from a pointer to another type of pointer?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.