Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to use Command arguments (argv[]) and check them in an If statement?

I'm trying to make Start up commands with argc and argv[] but I can't make it work. I have also tried other words but that doesn't work. What I want is that you can put in any amount of Startup arguments and I want to be able to put these in any order with out an issue.

This is what I have so far

int main(int argc, char** argv)
{
    //Print the amount of arguments
    std::cout << "Have " << argc << " arguments: \n\n";

    //Run through all the arguments and check if they are correct
    for (int i = 1; i < argc; i++)
    {
        if (strcmp(argv[i], "arg1") == i)
        {
            std::cout << "arg1 as start up command" << std::endl;
        }
        if (strcmp(argv[i], "arg2") == i)
        {
            std::cout << "arg2 as start up command" << std::endl;
        }
        if (strcmp(argv[i], "arg3") == i)
        {
            std::cout << "arg3 as start up command" << std::endl;
        }
        //Print which argument the for loop is at the moment
        std::cout << i << std::endl;
    }
}

Debugging command argument

CMD Output

Comments