I am really struggling to use command line arguments. I have finished the rest of my program but I have to do this to enter the file path to a txt file as well as coordinates (starting point for a maze) in the format:
filepath x y
I understand so far that you must start like this:
1 2 3 4
int main (int argc,char *argv[])
{
}
however I am not sure how to enter the inputs and at the moment I am just trying to use trial and error to work it out however isn't working for me (including any website that I use!) I would really appreciate it if someone can at least point me in the right direction please!
The variables argc and argv are filled in by the operating system when you specify options to your program on the command line. In your example, filepath x y, your variables would be this:
argv[ 0 ] is always the program name (either the full path or just the program name). argc is the number of arguments you passed (2 in your case) plus one (for your program name).
There are plenty of examples that you can find to take from here.
I'm not sure how much you know about using the variables argc and argv so I'll start at the top. Apologies if I cover something you already know, I don't mean to be condescending :)
argc: Gives you the total number of parameters passed to your application. argv: Character string of all the parameters passed.
To access a specific parameter: argv[x]; //where x is the index of the desired parameter
It's important to remember that argcbegins at 0. Also argv[0] will always be the full path and file name of your application.
One other point, really just about arrays in general. Like lnk2019, I don't mean to be condescending - just ignore this if you already know about it.
Remember that you will get runtime errors if you access an element beyond the bounds of an array (e.g. trying to access the fourth element of an array of size three). And the particular relevance to this situation? Don't assume your program gets all the command line arguments it needs. Check 'argc' to be sure 'argv' contains all the element you want before attempting to access them.
Appreciated for all the help guys but I still don't fully understand. I understand that argc is the number of arguments in the command line and i also understand argv. However I don't understand how you input strings on the command lines. To make it clear what I am trying to do I will write it in the way I have been previously been taught in c++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main ()
{
int x;
int y;
cout << "Please enter x and y coordinates: ";
cin >> x >> y;
cout << "x = " << x << endl << "y = " << y << endl;
return 0;
}
I think what my question is how do you input on the command line so you can put it in a variable. Cheers for the help! :)
The above example won't work. You need to tell you application to be ready to except arguments to "main(...)".
You know how you use "ipconfig"? You know how you can pass it command switches such as "/all" and "/flushdns"? Argv[1] would be that command line switch in your application.
Another example would be "ping". To ping google you would type "ping www.google.com" the part after the space (www.google.com) is your first argument to the program, or in other words it is what argv[1] would point to.
Also argv[0] will always be the full path and file name of your application.
This is not true.
argv is just whatever was on the commandline. That means argv[0] is likely whatever command was used to launch your program, which typically will be the program name but it doesn't have to include the full path and often doesn't.