| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"cdrsir" |
| Date: |
06 May 2006 03:24:26 AM |
| Object: |
How to write the program with parameters? |
like some useful windows commands, i.e.,
ping www.xxx.com
if you only type
ping
it will give you just some options.
how to write such programs? A simple example is to divide a number with
2. Like in command line I just enter:
myDivide 10
it will return 5.
how to write this "myDivide"?
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: How to write the program with parameters? |
06 May 2006 03:30:08 AM |
|
|
* cdrsir:
like some useful windows commands, i.e.,
ping www.xxx.com
if you only type
ping
it will give you just some options.
how to write such programs? A simple example is to divide a number with
2. Like in command line I just enter:
myDivide 10
it will return 5.
how to write this "myDivide"?
Check out the arguments to 'main'. That's all the support standard C++
gives, unfortunately. What values you get passed depends on the
operating system, the runtime library you link with, and so forth.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
| User: "" |
|
| Title: Re: How to write the program with parameters? |
06 May 2006 04:22:33 AM |
|
|
cdrsir wrote:
2. Like in command line I just enter:
myDivide 10
it will return 5.
how to write this "myDivide"?
If the program was written in C++ it might look like this. (In reality
the program would need to be much more complicated as it would need to
thoroughly check the arguments and so on, and maybe let you know if it
couldnt make sense of them).
regards
Andy Little
--------------------
// include common libraries
#include <iostream>
#include <sstream>
// declare to program a function to convert a character array
// to a number
double double_from_string( char const s[]);
int main(int number_of_args , char* prog_arg_values[])
{
// basic check that there are a valid number of arguments
// to the program
if (number_of_args != 3){
std::cout << "useage mydivide <numerator> <denominator>\n";
return 0;
}
// convert the program arguments from text to numbers
double numerator = double_from_string(prog_arg_values[1]);
double denominator = double_from_string(prog_arg_values[2]);
// do the math
double result = numerator / denominator;
// display the result
std::cout << result << '\n';
return 0;
}
// A function to convert a character array to a number
double double_from_string( char const s[])
{
std::stringstream ss;
ss << s;
double result;
ss >> result;
return result;
}
.
|
|
|
| User: "Markus Schoder" |
|
| Title: Re: How to write the program with parameters? |
06 May 2006 02:15:08 PM |
|
|
wrote:
// A function to convert a character array to a number
double double_from_string( char const s[])
{
std::stringstream ss;
ss << s;
double result;
ss >> result;
return result;
}
What's wrong with strtod?
.
|
|
|
| User: "" |
|
| Title: Re: How to write the program with parameters? |
06 May 2006 03:52:14 PM |
|
|
Markus Schoder wrote:
andy@servocomm.freeserve.co.uk wrote:
// A function to convert a character array to a number
double double_from_string( char const s[])
{
std::stringstream ss;
ss << s;
double result;
ss >> result;
return result;
}
What's wrong with strtod?
Nothing I guess. I was trying to keep the thing as simple as possible
and give descriptive names to the functions and variables etc. I prefer
to use std::string as a rule and not deal at all in C-style strings. (
there are various C-string to Float conversion functions which I can
never really remember without looking up their semantics), but the
C-style strings are in the command line, so that would just add
complexity, and I was trying to write a minimal program to accept
command line input. Feel free to improve it...
regards
Andy Little
.
|
|
|
|
| User: "Roland Pibinger" |
|
| Title: Re: How to write the program with parameters? |
07 May 2006 04:38:19 AM |
|
|
On 6 May 2006 12:15:08 -0700, "Markus Schoder"
<a3vr6dsg-usenet@yahoo.de> wrote:
andy@servocomm.freeserve.co.uk wrote:
// A function to convert a character array to a number
double double_from_string( char const s[])
{
std::stringstream ss;
ss << s;
double result;
ss >> result;
return result;
}
What's wrong with strtod?
Both need error handling!
.
|
|
|
|
|
| User: "cdrsir" |
|
| Title: Re: How to write the program with parameters? |
06 May 2006 09:02:47 AM |
|
|
I thank all of you that give me so good help. Now I am clear how to do
so. Thanks again.
.
|
|
|
| User: "Jonathan Mcdougall" |
|
| Title: Re: How to write the program with parameters? |
06 May 2006 02:07:44 PM |
|
|
cdrsir wrote:
I thank all of you that give me so good help. Now I am clear how to do
so. Thanks again.
Please quote the message you are answering to.
I don't think you're there yet, but you may have a look at
Boost.Program_options which has some nifty classes that do a great job
at parsing a command line. See www.boost.org.
Jonathan
.
|
|
|
|
|
|
| User: "Sohail Ahmed Siddiqui" |
|
| Title: Re: How to write the program with parameters? |
06 May 2006 04:09:39 AM |
|
|
cdrsir wrote:
like some useful windows commands, i.e.,
ping www.xxx.com
if you only type
ping
it will give you just some options.
how to write such programs? A simple example is to divide a number with
2. Like in command line I just enter:
myDivide 10
it will return 5.
how to write this "myDivide"?
int main(int argc, char* argv[]) // this will enable you to read command
line parameters, argc is the number of parameters you provide and argv is
the pointer to actial parameters.
you can read your parameters with a for loop
for(int i=1;i<argc;i++) // argv[0] will always be your program name
argv[i] will contain your actial parameters
Br
.
|
|
|
|

|
Related Articles |
|
|