0

I have a program that needs to use variables that should be set when the program is called from the command line. I want the user to be able to specify options -r, -o, -s, and -p along with some integer. I've never dealt with command line arguments before but after doing some research I came up with the following:

int main(int argc, char *argv[]){
    for(k = 1; k < argc; k = k+2){
        if(std::string(argv[k]) == "-r"){
           n = atoi(std::string(argv[k+1]));
        }else if(std::string(argv[k]) == "-o"){
           o = atoi(std::string(argv[k+1]));
        }else if(std::string(argv[k]) == "-s"){
           p = atoi(std::string(argv[k+1]));
        }else{
          n = 1; o = 2; p = 3;
        }
    }
}

My idea is that the user would call the program like so:

$ ./my_program -r 1000 -o 10000000 -s 1

in order to set the variables in my program accordingly. However, when I try to compile this with my makefile, I get an error:

    error: std undeclared (first use in this function)
        if(std::string(argv[k]) == "-r"){
           ^

There are many more but I can't type them all. I'm sure if I figure out this error I can figure out the others. Why doesn't C like me?

EDIT:

As a quick note, I did include , but this does not fix any of the errors and the output when calling make is still the same.

KARTHIK BHAT
  • 1,410
  • 13
  • 23
Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59
  • 1
    `#include ` (you tagged C, but the code you posted is C++) – TypeIA Mar 09 '14 at 06:54
  • Including this does not fix any of the errors I have. – Jeremy Fisher Mar 09 '14 at 06:57
  • Then you should have said so, and included the "fatal error," in your question. We can't help you solve problems without all the information. – TypeIA Mar 09 '14 at 06:58
  • I didn't include it before you mentioned it, so I didn't mention it in the question. I will edit my question though to say that I did include it though. – Jeremy Fisher Mar 09 '14 at 06:59
  • Are you by chance trying to feed C++ code into a pure-C compiler? That's what it sounds like now. – TypeIA Mar 09 '14 at 07:00
  • I'm using the gcc compiler. – Jeremy Fisher Mar 09 '14 at 07:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49339/discussion-between-dvnrrs-and-jeremy-fisher) – TypeIA Mar 09 '14 at 07:02
  • 3
    `std::string` does not exist in C; to compare strings you would use the `strcmp()` function. You may want to consider using a library that already has commandline arguments worked out, such as GNU getopt. – M.M Mar 09 '14 at 07:04
  • Apart from the `std::string` in C issue, your code's logic is wrong: Remove the last `else` clause and set the default values for `n`, `o` and `p` before parsing the arguments. (Or rather then removing the `else`, turn it into an "argument not recognised" error message.) – M Oehm Mar 09 '14 at 07:08
  • You should use a library function for parsing arguments. In C, on POSIX-ish systems you'd use [`getopt()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html), or GNU [`getopt_long()`](http://www.gnu.org/software/libc/manual/html_node/Getopt.html). In C++, you'd look at the [Boost options parser](http://www.boost.org/doc/libs/1_55_0/doc/html/program_options.html). There are a myriad alternatives. You would not, however, write ad hoc code to parse the options. – Jonathan Leffler Mar 09 '14 at 07:08
  • 1
    Possible duplicate of [Parsing command-line arguments?](http://stackoverflow.com/questions/9642732/parsing-command-line-arguments) There are also many other questions that this could be a duplicate of. – Jonathan Leffler Mar 09 '14 at 07:18
  • Why use `std::string` at all? Why not use `strcmp` ? It's much more efficient than creating objects when you only need to compare two strings. – Trinopoty Mar 09 '14 at 07:05

1 Answers1

2

You should use getopt() here. The getopt() and getopt_long functions automate some of the chore involved in parsing typical unix command line options. It will save you all the effort.

http://www.ibm.com/developerworks/aix/library/au-unix-getopt.html

brokenfoot
  • 11,083
  • 10
  • 59
  • 80