-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 ";
}
nobody
  • 19,814
  • 17
  • 56
  • 77
  • 6
    You just need double quotes to make a string literal: `"-r"`. With single quotes, it's a literal of type int. – nobody May 14 '14 at 01: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 – Jerry Jeremiah May 14 '14 at 01:55
  • Also a duplicate of / answered by http://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c – nobody May 14 '14 at 01:56
  • @AndrewMedico actually it's of type `char` that can be autoboxed into an int. – Qix - MONICA WAS MISTREATED May 14 '14 at 03: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. – nobody May 14 '14 at 03:20

2 Answers2

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;
}
Community
  • 1
  • 1
amdn
  • 11,314
  • 33
  • 45
  • "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. – Ed S. May 14 '14 at 02:49
  • 1
    Section 3.6.1 of C++ Standard (draft) http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf says "The value of argv[argc] shall be 0." – amdn May 14 '14 at 02:57
  • 1
    Section `5.1.2.2.1 Program startup` of C Standard (draft) http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf says "argv[argc] shall be a null pointer." – amdn May 14 '14 at 03:01
  • 1
    @EdS. Also see this SO question http://stackoverflow.com/questions/3772796/argvargc – amdn May 14 '14 at 03:03
  • 1
    Oh, wow, I didn't know that. Thanks, learned something. – Ed S. May 14 '14 at 05:20
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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285