8

I have a program in C++ which I run for many values of a parameter. What I want to do is the following: Let's say I have two parameters as:

int main(){
    double a;
    double b;
//some more lines of codes
}

Now after after I compile I want to run it as

./output.out 2.2 5.4

So that a takes the value 2.2 and b takes the value 5.4.

Of course one way is to use cin>> but I cannot do that because I run the program on a cluster.

1
  • Why don't use command line arguments (i.e., argv ) ? Of course you have to convert them later to double type. Commented Jul 3, 2012 at 17:15

5 Answers 5

22

You need to use command line arguments in your main:

int main(int argc, char* argv[]) {
    if (argc != 3) return -1;
    double a = atof(argv[1]);
    double b = atof(argv[2]);
    ...
    return 0;
}

This code parses parameters using atof; you could use stringstream instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Don't use const char *argv[], just use char *argv[], -1 until fixed.
@RichardJ.RossIII This is now fixed.
11

If you want to use command line parameters then no, you don't use cin as it is too late. You need to change your main signature to:

int main(int argc, char *argv[]) {
    // argc is the argument count
    // argv contains the arguments "2.2" and "5.4"
}

So you now have argv which is an array of pointer to char, each pointer pointing to an argument that was passed. The first argument is typically the path to your executable, subsequent arguments are whatever was passed in when your application was launched, in the form of pointers to char.

You will need to convert the char*'s to doubles in this case.

2 Comments

I'm pretty sure that is's int argc, not size_t. Correct me if I'm wrong.
@Linuxios: Yeah you're right. Some compilers allow size_t, but the standard only mandates that int main() and int main(int, char*[]) be accepted, anything else is implementation defined (well, the C++98 standard that I have lying around anyway...)
4

That's what command-line arguments are for:

#include <sstream>

int main(int argc, char *argv[])
{
    if (argv < 3)
       // show error
    double a, b;

    std::string input = argv[1];
    std::stringstream ss = std::stringstream(input);

    ss >> a;

    input = argv[2];
    ss = std::stringstream(input);

    ss >> b;

    // a and b are now both successfully parsed in the application
}

Comments

3

Have you looked at boost program options?

It will take the command line arguments like many other are suggesting and let you provide a very consistent, clean and extensible command line interface.

3 Comments

If you are already using boost then yeah, it's nice, but I wouldn't include boost into an application just to parse a few simple command line arguments.
I've always found that argument parsing starts out simple, and quickly gets out of hand introducing subtle bugs and constraints as the tool grows.
I guess it depends on how complex your arguments can be. For simple [switch value] type args I have never had a problem.
0

You can use this form of the main() function to get command line arguments

    int main(int argc, char* argv[]) { 

    } 

where the values of the argv[] array contain your command line variables as char* that you will need to convert to floats or doubles in your case

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.