Google "How C Programming works" and go the 39th chapter. And don't use System("pause").
the module 5 mastery check said:
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.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.
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; }
Last edited by lalakijilp; 10-20-2009 at 10:01 AM.
Google "How C Programming works" and go the 39th chapter. And don't use System("pause").
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
Last edited by B1ackAnge1; 10-20-2009 at 10:28 AM.
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)
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
create a shortcut to the exe and put it behind the path?
Ah we-a blaze the fyah, make it bun dem!
lalakijilp (10-20-2009)
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).
lalakijilp (10-20-2009)
thanks it worked