2

I have a program that calculates the factorial of multiple numbers. These nubers are passed as parameters in cmd as such :

factorial.exe 3 4 5

This will calculate the factorials of 3, 4 and 5 respectively. An earlier version of the program had a percentage that showed the fullness of the stack. I want to bring that back now, but I to also pass the wait time as a parameter in cmd as such:

factorial.exe 3 4 5 wait_time1

or

factorial.exe 3 4 5 1000

To read the numbers I use this args parser :

static string the_time;
    public static void Main(string[] args)
    {

        foreach (string s in args)
        {

            extra = int.Parse(s);

            Calculate(extra);
        }
     }

How can I separate the arguments? Thanks in advance.

6 Answers 6

4

You could add the waittime arg like a switch /t:100 so only when you see /t you know it is a waittime.

If you know your args will always have the waittime, then waittime is

waittime = arg[args.Length-1]

Probably worth a look as well

In case you don't want to reinvent the wheel

Sign up to request clarification or add additional context in comments.

1 Comment

And here we have a complete answer =) +1
1

I think, what you want is something like getopt on UNIX-like machines to parse arguments in a sensible way. Have a look here.

Comments

0

Each element at the array will be a string from the command line, space-divided. This way, for

factorial.exe 3 4 5

will you have

args[0] //3
args[1] //4
args[2] //5

EDIT

Thanks to DD59, now I understand your question. You could have a convention that the last parameter will always be the wait time, or you could use a syntax, such as -time (or /t:, as he said).

factorial.exe 3 4 5 -1000

Regards

2 Comments

I think the OPs question is how does he differ between the number arguments and the wait time.
@Hinek Inded, I've edited the answer now that I see it clearly.
0

You are already separating the arguments in foreach loop. The string s is what you get as argument. I suppose you want to pinpoint the wait_time value and don't want to calculate it of course. You have to fix the position like - last argument is wait_time or first arugment is. Without fixing the location it will be hard to determine.

Comments

0

Is wait_time a mandatory argument? Does it always stand at the last place? Then you could do it like this:

the_time = args[args.Length - 1];
for (int i=0; i<args.length - 1; i++){ 
    extra = int.Parse(args[i]); 
    Calculate(extra); 
} 

If wait_time is optional, you should go with DD59's answer.

2 Comments

it is mandatory and for some reason this doesn't work... If I place the time as the first parameter it works faster than it should if it's last it works slower... need to take a look at this
The code above checks for you wait_time in the last argument. So putting it first will result in the last of your numbers being interpreted as the wait_time ...
0

Parsing the command line is a quite complex part. Maybe you should take a look at a available library (some examples):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.