0
int main(int arg_count, char*args[])
{
if(arg_count > 1){
    name = string(args[1]);
    print_menu(name);

}
else {
    cout << "Username not supplied, exiting the program"<< endl;
}
return 0;
}

While executing the function int main, I understand that the function expects some parameters for it to run. What do the parameters int arg_count and charargs[]* mean?

Holy Man
  • 11
  • 1
  • 3
    *What do the parameters int arg_count and charargs[]* mean?*: https://en.cppreference.com/w/cpp/language/main_function – NathanOliver Dec 17 '21 at 13:59
  • Conventionally, they are `(int argc, char* argv[])` or `(int argc, char** argv)`. I like to repackage the parameters, not including `argv[0]`, into `auto args = vector(argv+1, argv+argc);` since `string` objects are much easier to work with. – Eljay Dec 17 '21 at 14:06
  • @Elijay could also use `string_view`, which would also help with accidentally modifying command line arguments. – Aykhan Hagverdili Dec 17 '21 at 14:20
  • Damn, closed, I had an answer ready to go... I suspect the OP is getting confused by `arg_count` and `args` - these refer to *the arguments passed to the program on launch by the operating system*, and have nothing to do with the function parameters in terms of code. If a C++ function expects two parameters, it must be given two parameters. – NoodleCollie Dec 17 '21 at 14:22

2 Answers2

0

The main function takes the command-line arguments supplied when running the program. arg_count is the number of such arguments passed, and args is an array of those arguments.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

On any OS, when you execute your program, there are ways to pass command line arguments..in order to support that, the entry point function of your C++ program takes in these 2 arguments. The first is the number of arguments, and second is the array of argument strings that "might" have been passed. Note that, it can be empty , so your code must check the argc/arg count, so that you don't access outside the bounds. Refer this

Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22