c++ - Storing lines of input into a vector -


in program trying take user lines of input names storing them vector.

i wrote own code got runtime error telling me "string subscript out of range".

this code

const int len = 100; struct case{ public: int no_people; vector<string> names; vector<string> results; void set_data(){     cin >> no_people;     int size = no_people;     char line[len];     (int = 0; < size; i++){         cin.getline(line, len);         names.push_back(line);     }   } } 

there's no need use char[] array, use std::string instead, given using it.

note op: cin.getline() one:

std::istream::getline(char*, int)

the 1 ned use std::string's one:

std::getline(istream&, string&)

struct case{ public:     int size;     vector<string> names;     vector<string> results;     void set_data(){         std::string temp;         cin >> size; cin.ignore();         (int = 0; < size; i++){             std::getline(cin, temp);             names.push_back(temp);         }     } } 

as far compile errors go, always:

  • quote exact error messgae
  • tell line happened at
  • show code contains line , relevant classes/methods

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -