argc and argv?

Hello,

As I'm teaching myself C++ and I am using a book to do so.
I have came across this program(below) and do not understand it 100%.

Firstly, my book talk about the command line arguments with the argc and argv but it didn't really explain it very well, so if you could explain that any better that would be great.

Also, the program is supposed to require a password and tell me if it is right or wrong, and it just says
Password required!
.

Lastly, this if statement:
1
2
3
4
5
if(!strcmp("mypassword", argv[1]))

	cout << "Access permitted.\n"; 
	else
cout << "Access denied.\n";
I tried putting my computer password in the "mypassowrd" area and still nothing changes.

Whole program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> 
#include <cstring> 
using namespace std;


int main(int argc, char *argv[]) { 
	if(argc != 2) {
cout << "Password required!\n"; return 0;
}

 if(!strcmp("mypassword", argv[1]))

	cout << "Access permitted.\n"; 
	else
cout << "Access denied.\n";

return 0;

}


Thanks, Luke.
Last edited on
closed account (S6k9GNh0)
The program is meant to be execute like this:
program mypassword


It expects the literal "mypassword", not your computer password....
strcmp does nothing but compare strings.
argc and argv are the arguments to the program itself. argc being the number of arguments, and argv being the actual arguments.

If you dont understand the concept, think of it as: The same way that a function can but doesnt have to take arguments, your program can also take arguments. These arguments are given via the command line/terminal.

If you use windows mostly, you probably dont use the command line often, if at all to start a program. In linux, this is used all the time.

The only way i can explain it is via example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main(int argc, char *argv[]){
    if (argc != 2){
        std::cout << "Password required" << std::endl;
    }
    else{
        std::cout << "Password is " << argv[1] << std::endl;
    }
}


this will execute "password required" if no argmuents are given or 3 or more agruments are given. These arguments are separated by a space (normally). However if you give one argument, it will print it out. This is not anything else other than the input that you provide.
example:

1
2
3
4
5
6
7
8
9
metulburr@ubuntu:~/programs/cplusplus$ g++ test2.cpp -o test2
metulburr@ubuntu:~/programs/cplusplus$ ./test2
Password required
metulburr@ubuntu:~/programs/cplusplus$ ./test2 password
Password is password
metulburr@ubuntu:~/programs/cplusplus$ ./test2 password password
Password required
metulburr@ubuntu:~/programs/cplusplus$ ./test2 b
Password is b


here by executing:
./test2
i gave no arguments. This is essentially what happens when you double click the executable, AKA the exe
here i gave 2 arguments:
./test2 password password
and here i bypassed the if condition because there is only one argument:
./test2 password
and here i also gave another correct number of arguments:
./test2 b
so it again printed out b

I tried putting my computer password in the "mypassowrd" area and still nothing changes.

password is literally the string password, not the computer password.
Last edited on
Thank you both, C++ can be really confusing when you have no one teaching it you one to one.

just keep reading I guess ;)

Thanks again.
Topic archived. No new replies allowed.