By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,942 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,942 IT Pros & Developers. It's quick & easy.

Parameters

P: 48
Could anyone please tell me how to make a command where i can add parameters. I've searched on the internet but I dont get it!
Can anyone help?
Thanks

P.S I've just been doing this for a week, and Im a kid so be instructive and sympathetic.

:-)
Aug 14 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 10K+
P: 11,448
Could anyone please tell me how to make a command where i can add parameters. I've searched on the internet but I dont get it!
Can anyone help?
Thanks

P.S I've just been doing this for a week, and Im a kid so be instructive and sympathetic.

:-)
Do you know about argc and argv? They're both the parameters to the main function:

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char* argv[]) {
  2.    ...
  3. }
  4.  
The first parameter equals the number of parameters supplied to you program
plus one. argv[0] equals the name of your program itself. The other argv[i] equal
the parameter i supplied to your program. Do this for starters:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char* argv) {
  4.    int i;
  5.    printf("argc= %d\n", argc);
  6.    for (i= 0; i < argc;i++)
  7.       printf("argv[%d]= %s\n", i, argv[i]);
  8.    return 0;
  9. }
  10.  
kind regards,

Jos
Aug 14 '07 #2

P: 48
thanks a lot for your help. I'm making mathematical programs. I've got loads, but I just wanted to compile them all into 1 program.
I tried simulating parameters using IF and ELSE IF etc.
but it was a nightmare
Aug 14 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
thanks a lot for your help. I'm making mathematical programs. I've got loads, but I just wanted to compile them all into 1 program.
I tried simulating parameters using IF and ELSE IF etc.
but it was a nightmare
If you have multiple functions, I'd recommend either making them methods of a class, or just making them functions in your main (depending on how big your main is and how big your functions are). Then you can call them as desired passing whatever values you want. This is also a good way to abstract them.
Aug 14 '07 #4

Expert 10K+
P: 11,448
thanks a lot for your help. I'm making mathematical programs. I've got loads, but I just wanted to compile them all into 1 program.
I tried simulating parameters using IF and ELSE IF etc.
but it was a nightmare
You bet; if you're programming in C++ you'd better build an abstract class
that can give it's name and the number of arguments it needs. Then populate
a map with names and concrete subclasses of that abstract class.

Your main() function then expects the name of the 'function' followed by its
arguments. Find the name in the map and invoke some method of the class
given the correct number of arguments.

kind regards,

Jos
Aug 14 '07 #5

P: 48
that parameter code snippet you gave me above...it kept coming up with a system.AcessViolationError thing.
It kept closing after printing on the screen
Argc=1
Aug 14 '07 #6

P: 48
whats an abstract class!
Aug 14 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
whats an abstract class!
That sounds like a fantastic question for google, ask, yahoo, etc...

A topic like that is so broad and general that it would be difficult if not impossible to convey to you in this format. Try researching it.
Aug 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.