-1

How can I write a program to check the arguments in the terminal are correct?

For example, if I have a program hello.cpp and I want to call it as:

./hello yes 10

I want the program to make sure that the first argument is yes or no and the second argument is a number between 1-10. So how can I read these arguments into my program to do the checking?

Thanks!

Bob John
  • 3,688
  • 14
  • 43
  • 57

3 Answers3

3

Command line arguments are passed as a count and individual strings in the argc and argv arguments to main().

int main(int argc, char *argv[])
{
   ...
}

Simply check the value in argc and the strings in argv for the appropriate values.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
3

You meant to execute like this, ./hello yes 10

there is an option argc and argv in c where argc is the number of arguments passed and argv with the index shows the argument passed itself.

Take a look at the below code for iterating through all arguments.

int main(int argc, char *argv[]){
    int i = 0;
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    }
    return 0;
}
Antarus
  • 1,573
  • 2
  • 15
  • 32
1

As mentioned by other users, The main function is the entry point of your program, and the way it gets data from the command line is through its parameters.

The first int argument is the count of all the arguments passed, including the program name, the second char ** argument is a pointer to each parameter passed, including the program name:

int main
(
    int argc,       // <-- how many parameters has been provided? 
    char **argv,    // <-- what values has each parameter?
)
{
    ...
    return 0;
}

So, knowing that, your call ./hello yes 10 must be like that:

argc = 3
argv[0] = "./hello"
argv[1] = "yes"
argv[2] = "10"

The names argc and argv are just a convention, you can name them at your pleasure, but it's a good practice to keep the names that everyone are used for.

And the argument doesn't are forced to be int, char ** they must follow a quite rigid convention, borrowed from this answer:

It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both the following definitions of main: int main() and int main(int argc, char* argv[])

Knowing that, let's focus on your question:

First of all, you must ensure that 2 arguments are passed, so you must check the argc value and ensure that equals exactly 3.

the first argument is yes or no

Next, you must store your argv[1] (because 0 contains the program name) into a string and compare it with the values "yes" and "no":

    std::string YesOrNo(argv[1]);
    if (YesOrNo == "yes" || YesOrNo == "no")

And finally, you must store your argv[2] into an integer and check if it is equal or less to 10:

    std::stringstream Stream;
    int Value = 0;
    Stream << argv[2];
    Stream >> Value;

    if (Value <= 10)

So, the result is:

int main(int argc, char **argv)
{
    if (argc == 3)
    {
        std::string YesOrNo(argv[1]);

        if (YesOrNo == "yes" || YesOrNo == "no")
        {
            std::stringstream Stream;
            int Value = 0;
            Stream << argv[2];
            Stream >> Value;

            if (Value <= 10)
            {
                // Your stuff..
            }
        }
    }

    return 0;
}

I let you deal with all the uppercase and lowercase stuff and the false positives with the numeric argument, at least I'm not going to do all your homework ;)

Community
  • 1
  • 1
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94