Nignig
Nignig

Reputation: 9

read from file to string and pass to function

I am having some issues trying to understand why I am getting a Exception thrown to this section when I am trying to pass a string that contains content from a getline() statement.

`reference operator[](const size_type _Off)
{   // subscript mutable sequence
auto& _My_data = this->_Get_data();
_IDL_VERIFY(_Off <= _My_data._Mysize, "string subscript out of range");
return (_My_data._Myptr()[_Off]);
}`

void set_token(string n);
    string token;

        while (fin.peek() != '0' && !fin.eof())
        {
            getline(fin, token);
            set_token(token);
        }

`void set_token(string n)
{
    string strarray[20];
    string token;
    int size = sizeof(n);
    int i = 0;
    int j = 0;
    while (i < size)
    {
        if (n[i] != ' ' && n[i] != '\0')
        {
            token += n[i];
        }
        else
        {
            strarray[j] = token;
            j++;
            token.clear();
            lexical(strarray[j]);
        }
        i++;
    }`

Upvotes: 0

Views: 52

Answers (1)

Archie Yalakki
Archie Yalakki

Reputation: 552

I don’t see any problem in getline() but in set_token() there is obvious run-time issue when you extract the size of n. The sizeof doesn’t give you total number of characters in string but size() does.

Try this: size = n.size():

Upvotes: 1

Related Questions