-1

I want to take argv[(2 in this example)], store it into vector and use it later in program. The problem is that no operand matches those operands std::string == int. So does that mean that the app sees '-r' as int? I'm a bit confused here.

int main(int argc, char* argv[])
{
    std::vector<std::string> argList;
    cout<<"argc: "<<argc<<endl;
    for(int i=2; i<=argc; i++)
    {
        argList.push_back(argv[i]);
    }
    if(argList.at(2) == '-r') cout<<" Good job ";
}
5
  • 6
    You just need double quotes to make a string literal: "-r". With single quotes, it's a literal of type int. Commented May 14, 2014 at 1:52
  • argv[2] is being placed in argList[0] and argList[2] contains argv[4] so are you passing 4 parameters to your program? Really you should check the size of the vector before you try to access element 2 Commented May 14, 2014 at 1:55
  • Also a duplicate of / answered by stackoverflow.com/questions/3683602/… Commented May 14, 2014 at 1:56
  • @AndrewMedico actually it's of type char that can be autoboxed into an int. Commented May 14, 2014 at 3:08
  • Oops, you're right - one of the differences between C and C++. I wouldn't say "autoboxed" though (maybe "implicitly converted") - C++ doesn't do boxing and int isn't an object anyway. Commented May 14, 2014 at 3:20

2 Answers 2

1

There are several issues with your program:

  1. You iterate i until i == argc, that will attempt to construct a string from argv[argc], a NULL pointer due to the requirement by C and C++ standards that argv[argc] be 0 (NULL), see this SO question. argv is an array with argc pointers to null-terminated character strings (terminated by ASCII NUL, 0), the array itself is terminated with a NULL pointer (not counted in argc). Now, in C++ you can construct a string from a pointer to a null-terminated character string, but passing a NULL pointer to a string constructor results in undefined behavior, see this SO question, also see the list of std::string constructors here, you are implicitly using constructor (4) in that list (from c-string).
  2. You start pushing onto argList with i==2, which means argList[0] will contain argv[2], you then reference argList.at(2), which would correspond to argv[4], this is not likely what you meant.
  3. String literals use double quotes

I've corrected these and created a working program, click here

#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::endl;

int main(int argc, char* argv[])
{
    vector<string> argList;
    cout << "argc: " << argc << endl;
    for(int i=0; i < argc; ++i)
    {
        argList.push_back(argv[i]);
    }
    cout << "Program name is " << argList[0] << endl;
    if(argc > 1) {
        if(argList.at(1) == "-r") {
            cout << " Good job, you supplied -r\n";
        } else {
            cout << "Unrecognized option " << argList[1]
                << "\nUsage: " << argList[0] << " -r\n";
        }
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

"So argv[argc] == NULL" - No, not at all. argv[argc] invokes undefined behavior and you cannot make assumptions as to what that next bit of memory contains.
Section 3.6.1 of C++ Standard (draft) open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf says "The value of argv[argc] shall be 0."
Section 5.1.2.2.1 Program startup of C Standard (draft) open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf says "argv[argc] shall be a null pointer."
@EdS. Also see this SO question stackoverflow.com/questions/3772796/argvargc
Oh, wow, I didn't know that. Thanks, learned something.
0

The problem is your use of single quotes in '-r'. You want double quotes here:

if(argList.at(2) == "-r") cout<<" Good job ";

The reason is that in C++, single quotes are used for characters only. There is such a thing as a "multi-byte character constant", which is what '-r' ends up being. This is something completely different from a string constant, which is what you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.