How to write the program with parameters?

C

cdrsir

like some useful windows commands, i.e.,

if you only type

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:

it will return 5.

how to write this "myDivide"?
 
A

Alf P. Steinbach

* cdrsir:
like some useful windows commands, i.e.,


if you only type


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:


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.
 
S

Sohail Ahmed Siddiqui

cdrsir said:
like some useful windows commands, i.e.,


if you only type


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:


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 will contain your actial parameters

Br
 
A

andy

cdrsir said:
2. Like in command line I just enter:


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;
}
 
C

cdrsir

I thank all of you that give me so good help. Now I am clear how to do
so. Thanks again.
 
J

Jonathan Mcdougall

cdrsir said:
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
 
M

Markus Schoder

// 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?
 
A

andy

Markus said:
// 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top