Help ! MAIN () function parameters of the problem

Hello, I'm Shen Yang Yang, 21 years old, from China, which I am pleased to join you, I like to make friends, I hope you first introduce yourself!
My question is:
MAIN () function to understand how the parameters? How to use?
I welcome you for any help, preferably with examples.
[I typed out the following then I hit refresh. I don't want my reply to go to waste so I'm going to post it anyway]

Hi! I'm shacktar. I'm from Canada. I don't feel comfortable saying any other of my personal information on here :)

MAIN () function to understand how the parameters? How to use?

I assume you mean this version of main int main(int argc, char* argv[])? Because the usual version of main int main(), doesn't have any parameters, of course.

The two parameters (argc, argv) are called command line parameters because that's how they're passed in.

- argc is short form for argument count. It tells you how many inputs the program got from the command line.
- argv is short form for argument vector. It stores the inputs from the command line with each input being a char*. The first value is the full path to the program. Therefore, argc will always be at least 1.

Let's say you have a program called "prog". Running the following from the command line:

prog -n 15 -p hello

would produce the following:

1
2
argc == 5
argv == {"C:\...\prog.exe", "-n", "15", "-p", "hello"}


By convention, the inputs from the command line with a '-' in front are the parameter names. They tell the program how to treat the next value. Above, the program would register that n == 15 (after parsing). Whatever n means is up to the program. There could also be parameters as flags, for instance "-f" (where any string after it would be a different parameter altogether), would tell the program that flag "f" is on. These inputs are up to your program to interpret and each program defines its own (if any). Note that if you run code from an IDE, then you will have to provide the arguments as a setting somewhere, or else they will be empty.

Note that spaces are important. If the program expects the input as above, but the user made a mistake and typed this:

prog -n15 -phello

then

1
2
argc == 3
argv == {"C:\...\prog.exe", "-n15", "-phello"}


It would be up to the program to either parse the values out or to report an error.

Essentially, the command line parameters are just a set of strings whose form and validity are defined by the program.

Here is an example of a program that expects either a "-b" flag or nothing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//example.cpp
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
     bool b = false;

     if(argc == 2)
     {
          if(strcmp(argv[1], "-b") != 0)
          {
               cerr<<"Error in command line input."<<endl;
               return 1;
          }

          b = true;
     }
     else if(argc > 2)
     {
          cerr<<"Error in command line input."<<endl;
          return 1;
     }
     
     if(b)
     {
          cout<<"Flag -b has been entered!"<<endl;
     }
     else
     {     
          cout<<"Flag -b has not been entered :("<<endl;
     }

     return 0;
}
Last edited on
hi,Duoas
In the first post where I saw some of your help and insight, you really great love, if I may, I hope you make friends
hi,shacktar
Your help is very perfect, giving me a lot of help. Thank you for your help, if possible, you and I make friends.
There was a mistake in my initial post. I corrected it now. The mistake was that I had argc as one less than it should be (it will always be at least 1 since the first argument stores the path to the program).
Topic archived. No new replies allowed.