0

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++; }` 
11
  • 2
    stackoverflow.com/questions/5837639/eof-bad-practice Commented Feb 28, 2018 at 6:46
  • you haven't shown us the set_token function!!! Commented Feb 28, 2018 at 6:48
  • @john it crashes before even executing a command in the set_token function. Commented Feb 28, 2018 at 6:50
  • 5
    int size = sizeof(n); super broken. Commented Feb 28, 2018 at 6:56
  • 1
    @Nignig Often given advice is to use a debugger for this kind of problem. It would have found the problem very quickly Commented Feb 28, 2018 at 7:02

1 Answer 1

1

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():

Sign up to request clarification or add additional context in comments.

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.