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;
}

Thanks!

Recommended Answers

All 4 Replies

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";
}

Thanks that will help a great deal!

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?

You can use the Library functions like atoi(), sscanf()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.