How to write the program with parameters?

Discussion in 'C++' started by cdrsir, May 6, 2006.

  1. cdrsir

    cdrsir Guest

    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"?
     
    cdrsir, May 6, 2006
    #1
    1. Advertising

  2. * 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?
     
    Alf P. Steinbach, May 6, 2006
    #2
    1. Advertising

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

    Br
     
    Sohail Ahmed Siddiqui, May 6, 2006
    #3
  4. cdrsir

    Guest

    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;
    }
     
    , May 6, 2006
    #4
  5. cdrsir

    cdrsir Guest

    I thank all of you that give me so good help. Now I am clear how to do
    so. Thanks again.
     
    cdrsir, May 6, 2006
    #5
  6. 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
     
    Jonathan Mcdougall, May 6, 2006
    #6
  7. 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?
     
    Markus Schoder, May 6, 2006
    #7
  8. cdrsir

    Guest

    Markus Schoder wrote:
    > 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
     
    , May 6, 2006
    #8
  9. On 6 May 2006 12:15:08 -0700, "Markus Schoder"
    <> wrote:

    > 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!
     
    Roland Pibinger, May 7, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    985
    Jonck van der Kogel
    May 27, 2004
  2. Puneet
    Replies:
    16
    Views:
    1,138
    Daniel Vallstrom
    Mar 20, 2005
  3. Replies:
    1
    Views:
    476
    Walter Roberson
    Aug 12, 2005
  4. Jason
    Replies:
    2
    Views:
    512
    Jonathan Mcdougall
    May 13, 2006
  5. sahm
    Replies:
    4
    Views:
    685
Loading...

Share This Page