Reputation: 79
Suppose I wanted to open up the program through command (using argc and argv). You get to your program name, open the program. It gives you the .exe. Then once your program.exe is run, add another argument to it such as (program.exe open), which should open something in your program.
if (argc >= 5){
if (int(argv[1]) == 1){
function1();
function2();
function3();
}
}
Basically in this case, if the user were to input program.exe 1, (1 being the opening in this case) it should do the following functions. Why is this incorrect logically? (as there is nothing displaying)
Upvotes: 0
Views: 54
Reputation: 3908
Your conversion of argv[1] to int does not work. You could use atoi():
if (argc >= 2){
if (atoi(argv[1]) == 1){
function1();
function2();
function3();
}
}
Upvotes: 1
Reputation: 47844
What you need is this :
if (argc >= 2){ // the argc is count of supplied argument
// including executable name
if ( (argv[1][0]-'0') == 1){
//argv[1] will be "1"
//so take first character using argv[1][0]--> gives '1'-->49
//substract ASCII value of 0 i.e. 48
//Note: - This will only work for 0-9 as supplied argument
function1();
function2();
function3();
}
}
Upvotes: 2
Reputation: 27577
Because int(argv[1])
doesn't convert the string "1"
to the int
1
. Try this instead:
if (argv[1][0] == '1') {
Upvotes: 0