Computing.Net > Forums > Programming > Passing a parameter to C++ program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Passing a parameter to C++ program

Reply to Message Icon

Name: stroke6463
Date: September 14, 2006 at 07:25:41 Pacific
OS: XP Pro
CPU/Ram: idk
Comment:

I want to have my program take a parameter from the command line and output a number. So when I am in the command line and type 'program.exe 4' my program will run its operations using 4 as the input and output the final value. Thanks for the help.



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: September 14, 2006 at 15:42:06 Pacific
Reply:

Look up the arg() function in the C++ documentation.

Stuart


0

Response Number 2
Name: tommycoolman
Date: September 15, 2006 at 01:13:17 Pacific
Reply:

The main() function of your program would have to be modified to accept command line arguments.

#include

int main(int argc, char **argv)
{
printf("%s\n",argv[1]);
return 0;
}

"argc" is the number of command line arguments and argv is an array of strings that holds the arguments. The first argument is always the name of the program, so argv[1] would be the first argument.


0

Response Number 3
Name: stroke6463
Date: September 18, 2006 at 03:52:15 Pacific
Reply:

Thanks that worked perfectly.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Programming in Visual Stu... Good Windows programing r...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Passing a parameter to C++ program

passing parameters to a bat file www.computing.net/answers/programming/passing-parameters-to-a-bat-file-/13094.html

Passing parameter to exe file www.computing.net/answers/programming/passing-parameter-to-exe-file/4717.html

java system call to C++ program www.computing.net/answers/programming/java-system-call-to-c-program/10126.html