How can I pass arguments to a console program? Something like: progname -a
Printable View
How can I pass arguments to a console program? Something like: progname -a
Ok, that helped a lot. I wrote a little program using it, but it crashes.
If i type "<progname> -a" it prints ok, but it still crashes.Code:#include <iostream.h>
#include <string.h>
int main(int argc, char* argv[])
{
for (int i = 1; i <= argc; i++)
{
if (strcmp(argv[i], "-a") == 0)
cout<<"ok"<<endl;
}
return 0;
}
How does it crash? Does it ever get to the return 0?
Dont know if this is the problem, but change your for loop to this:
Z.Code:for(int i = 0; i < argc; ++i) {.. }
USe
Code:#include <iostream.h>
#include <string.h>
int main(int argc, char* argv[])
{
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-a") == 0)
cout<<"ok"<<endl;
}
return 0;
}
I win =P.
Z.
Also remember that argv[argc] is defined (in the standard) to be NULL :)