C++ main() Checking the number of arguments entered at the command line.

Description

C++ main() Checking the number of arguments entered at the command line.

#include <iostream>

int main(int argc, char* argv[])
{
  switch (argc - 1)
  {//ww w  .  j  a  v a  2s . com
  case 2: case 3: case 4:
    for (int i {1}; i < argc; ++i)
      std::cout << "Argument " << i << " is " << argv[i] << std::endl;
    break;
  default:
    std::cout << "You entered the incorrect number of arguments.\n" << "Please enter 2, 3 or 4 arguments. " << std::endl;
  }
}



PreviousNext

Related