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.