c - Allowing a user to pick an option for how a program runs at the terminal -


So, suppose I have two functions that do the same thing but with a different algorithm. I want to let the user choose one option or another on the terminal.

When the user chooses which options they want, then something like this should be ...

  ./a.out Option 1  

or

 . / A.out option 2  

Before I came here and asked, read more on this, so maybe I could have done some code, but I do not even know that What is said, so I can search for it!

Any help would be greatly appreciated, thanks!

sample

  #include & lt; Stdio.h & gt; #include & lt; String.h & gt; Int main (int argc, char * argv []) {// Eg. ./a.out option1 // argc: 2 / argv [0]: "./a.out" (or it may be a different example, for example, fullput or etc.) // argv [1]: " Option1 "int opt ​​= 0; If (argc> 1) {If (strcmp (argv [1], "option1") == 0) opt = 1; And if (strump (argviz [1], "option 2") == 0) opt = 2; Else opt = -1; } Switch (opt) {Case 0: printf ("The option was not specified. \ N"); break; Case 1: printf ("Option 1 was specified. \ N"); break; Case 2: printf ("Option 2 was specified. \ N"); break; Default: printf ("The options you specify are incorrect. \ N"); Printf ("Usage:% s [option1 | option2] \ n", argv [0]); } Return 0; }  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -