how do you pass arguments to a c/c++ program?

wasssup

Diamond Member
Nov 28, 2000
3,142
0
0
its been a long time since i've really used c/c++, so i don't remember this stuff too well anymore :(

i'm using gcc to compile the program(s), and what i'm trying to do is set it up so i can just do something like "a.out GCD" or "a.out MAX" and depending on the argument (GCD or MAX) do different things from within the program...

I have no idea how to implement this, and google hasn't been helpful to me yet :(
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
int main(int argc, char *argv[])
{
int i;
for(i=0; i<argc; i++)
cout << argv[ i ] << endl;
}

Compile this, and then run it (./a.out foo bar blat "a string").

edit: the argv in cout is supposed to be the element of argv indexed by i. fusetalk thinks I want to italicize stuff ;).
 

wasssup

Diamond Member
Nov 28, 2000
3,142
0
0
thanks for the quick reply...i dunno why, but for some reason all the programs i've been creating complain and say "permission denied" when i try running them on the campus network...

so once i figure out why its doing this i'll give you a reply about whether or not it worked :)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
"a.out" generally won't work, you need to do "./" to run an executable in the current directory. Like "./a.out"