command-line arguments

Posts 1–14 of 14 · Page 1 of 1
command-line arguments
the module 5 mastery check said:

8. Write a program that requires a password that is specified on the command line. Your program doesn’t have to actually do anything except report whether the password was entered correctly or incorrectly.
i forget how it worked so i looked back. after reading that part of the chapter again i still didn't understand it so i made the example program to make it more clear. but the program didn't work. and the last system pause is never executed.

1 can someone explain to me how command-line arguments work and what they do?

2 can someone tell me why the code doesn't work.

the code of the example program that doesn't work:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
    double a, b;
    if (argc!=3)
    {
                cout <<"usage: add num num" <<endl;
                system("pause");// added this one so the screen wouldn't disappear
                return 1;
    }
    
    a = atof(argv[1]);
    b = atof(argv[2]);
    
    cout  << a+b;
    
    system("pause");
    return 0;
}
Google "How C Programming works" and go the 39th chapter. And don't use System("pause").
Quote Originally Posted by Lynie View Post
Google "How C Programming works" and go the 39th chapter. And don't use System("pause").
if i dont use system pause the program will execute and close in a milisec and i will only see a flash...
Quote Originally Posted by lalakijilp View Post
if i dont use system pause the program will execute and close in a milisec and i will only see a flash...
Then use another way to wait for key input, such as getchar()
I ran it and it worked.
What are you passing to the command line? If you're running it from within Visual Studio, then you'll have to set the arguments in the project properties.
Right click on the project->Properties,
Then go to "Debug" and you'll see an entry for 'Command Line Arguments"
Where then you'd type : 3 5.4
or whatever numbers you want to pass in

Or you could simply open a dos prompt in the folder where you have your EXE and run it from there (which also negates the need for any of this pause/getch stuff)
Quote Originally Posted by B1ackAnge1 View Post
I ran it and it worked.
What are you passing to the command line? If you're running it from within Visual Studio, then you'll have to set the arguments in the project properties.
Right click on the project->Properties,
Then go to "Debug" and you'll see an entry for 'Command Line Arguments"
Where then you'd type : 3 5.4
or whatever numbers you want to pass in

Or you could simply open a dos prompt in the folder where you have your EXE and run it from there (which also negates the need for any of this pause/getch stuff)
i use dev c++ and i didn't "pass" anything to the command line

and how do you pass anything to the command line?
because that is the opdracht en het is niet uitgelegt in de tutorial
yeah.. what's happening is that it's running the app without anything on the command line so it always gets a argc of 1.

just googled really quickly:
"To pass command-line parameters to your program, go to the "Execute" menu, choose "Parameters" and type in any paramaters you wish to pass."

So there you'd type 2 numbers


or just open a command prompt, cd to the drive\folder where your exe sits and just type it in old-school
add.exe 3 5.4

But most people nowadays don't know how to use a command prompt
Your code works just fine, but you may want to add a '<< endl;' where you print out the numbers.

It's really not that hard;
ARGC has how many arguments there are INCLUDING the exe name.
ARGV holds the strings for these arguments

So if you have 'Add.exe 3 5.4' you'll get
Argc = 3;
and Argv[0] = "Add.exe"
Argv[1] = "3";
Argv[2] = "5.4";

Lynie and a lot of other people hate System("Pause") and would rather have you use getch() or something similar. They have a point -See my post here. Though it seems silly to keep rehashing that same point over and over to people who are just learning. If you have something that works and makes sense to you go with it.. not like any of this is 'commercial grade' software
Quote Originally Posted by B1ackAnge1 View Post
Your code works just fine, but you may want to add a '<< endl;' where you print out the numbers.

It's really not that hard;
ARGC has how many arguments there are INCLUDING the exe name.
ARGV holds the strings for these arguments

So if you have 'Add.exe 3 5.4' you'll get
Argc = 3;
and Argv[0] = "Add.exe"
Argv[1] = "3";
Argv[2] = "5.4";

Lynie and a lot of other people hate System("Pause") and would rather have you use getch() or something similar. They have a point -See my post here. Though it seems silly to keep rehashing that same point over and over to people who are just learning. If you have something that works and makes sense to you go with it.. not like any of this is 'commercial grade' software
thanks but....

the code probably is right but the program doesnt work.
it looks like the program never gets past the if
Quote Originally Posted by lalakijilp View Post
thanks but....

the code probably is right but the program doesnt work.
it looks like the program never gets past the if
Could you write the output of your program?
create a shortcut to the exe and put it behind the path?
it works but only if it is executed with dev C++
why is this happening
If you run it by just double clicking you're again not passing in any arguments thus it will find argc == 1

you have to pass the arguments to it by either
a) Passing them through your Dev environment
b) Do what HD said and make a shortcut and add them there
c) Run the program from the Dos-Prompt (which you should learn since
most of these apps you're writing are really made to be ran like that anyway).
Posts 1–14 of 14 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us