-
command line arguments
Heya fellow programmers
I just ran into these two little variables that are used by convention for some time. argc and argv. How does one get to see the effects of this when running windows Me. For instance look at this program i pasted here below.
#include <iostream.h>
int main(int argc, char *argv[])
{
if(argc != 2) {
cout << "you forgot to type your name";
return 1;
}
cout << "Hello " << argv[1] << '\n';
return 0;
}
when i execute this tricky one all i get is the if statement output. Can someone just enlighten me a little please.
Have a nice day.
zbap:confused:
-
Make sure you're entering the proper arguments:
c:\cppTest Julienne Walker
will bomb, but
c:\cppTest Julienne
works just fine.
If you intend to have the program spit out both a first and last name then you want to test argc for 3 and print argv[1] and argv[2]. Spaces in between the arguments on the command line will cause them to be treated as completely different strings as opposed to one string with internal whitespace.
-Prelude