How would i create a function so that i can have a command line argument switch in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How would i create a function so that i can have a command line argument switch in c++?

For example, i want to make switch where if a person types in -s as a switch, it counts how many times the s letter shows up in a string, and then reports it to the screen.

13th Nov 2021, 9:38 PM
Kareem Idris
1 Answer
+ 2
When you define main(), define its input arguments like this: int main(int argc, char* argv[]) The first argument, argc, will receive the count of space-separated words (strings) from the command line. The second argument, argv[], will be a string pointer array that holds pointers to the individual command line words. The strings are stored in the order they are found left-to-right. They are null-terminated. The first string in argv (that is, argv[0]) will be the program name. The second string, argv[1], would hold the switch "-s" in your case, and so on for remaining command line argument values.
14th Nov 2021, 4:00 AM
Brian
Brian - avatar