-1

In my program I am trying to take from the user lines of input actually names then storing them into a vector.

I wrote my own code but I got a runtime error telling me that "string subscript out of range".

This is my 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];
    for (int i = 0; i < Size; i++){
        cin.getline(Line, LEN);
        Names.push_back(Line);
    }
  }
}
Mahmoud Anwer
  • 164
  • 1
  • 2
  • 12
  • 2
    Why are you using character arrays for your strings instead of `std::string` for that too? Use `std::string` and [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline). There's also no need for the temporary variable `Size`, you can use `No_People`directly. – Some programmer dude Feb 08 '16 at 07:05
  • 1
    As for your error, *where* do you get the error? On which line of the code you show? – Some programmer dude Feb 08 '16 at 07:05
  • 1
    Pick one of languages you tagged.... – LPs Feb 08 '16 at 07:08
  • @JoachimPileborg if i used only string not char array i got an error that can't convert argument 1 from string to char*. i think it's because getline function takes char of array as an argument. it's a runtime error , i have edited the post. – Mahmoud Anwer Feb 08 '16 at 07:12
  • @MahmoudAnwer, see my answer below for an explanation. – iksemyonov Feb 08 '16 at 07:14
  • Please read my complete comment, it tells you a better function to use. And you still haven't told us *where* you get the error. – Some programmer dude Feb 08 '16 at 07:16
  • @JoachimPileborg isn't it the one I'm referring to? Mean, what exactly is it "better" than? I'm a bit lost. – iksemyonov Feb 08 '16 at 07:17
  • "Better" because `std::getline` will read the whole line no matter the length, it will use `std::string` which can have a dynamic length, and is general safer. – Some programmer dude Feb 08 '16 at 07:20
  • @iksemyonov i tried to use this function you wrote in your comment but i got the same runtime error that tell me " string subscript out of range " Note : my input should has spaces something like " hello world " – Mahmoud Anwer Feb 08 '16 at 07:21
  • So it's not a *compiler* error, but a runtime error, a crash (or thrown and unhandled exception)? Then you need to run your program in a debugger to see where it happens. The code you show is most likely not the problem causing the error. – Some programmer dude Feb 08 '16 at 07:27
  • ok assume i used the code below in the comment. using this code can't permit to me to enter two line of input like "hello world" – Mahmoud Anwer Feb 08 '16 at 07:31
  • @JoachimPileborg I offered to use `std::getline()` as well, if it's the same one we're talking about. – iksemyonov Feb 08 '16 at 07:32

3 Answers3

1

Personally I would define a class to represent a line. Then you can use stream iterators to load the vector.

class Line
{
    std::string   line;
    public:
        // Operator to convert a line back to a std::string
        operator std::string const&() const {return line;}

        // Friend function to read a line from a stream.
        friend std::istream& operator>>(std::istream& in, Line& data)
        {
            return std::getline(in, data.line);
        }
 };

 int main()
 {
     int countOfPeople;
     std::cin >> countOfPeople;

     std::vector<std::string>  lines;
     std::copy_n((std::istream_iterator<Line>(std::cin)), countOfPeople,
                 std::back_insert_iterator(lines));
 }
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

There's no need to use a char[] array, use std::string instead, especially given that you already are using it.

Note to OP: cin.getline() is this one:

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

The one you ned to use for std::string's is this 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();
        for (int i = 0; i < Size; i++){
            std::getline(cin, temp);
            Names.push_back(temp);
        }
    }
}

As far as compile errors go, always:

  • quote the exact error messgae
  • tell the line it happened at
  • show the code that contains the line and the relevant classes/methods
iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • i tried your code in a new project it's okay but if i entered the size to be 2 it accept only one line of input Note : the line of input contain space something like " hello world " – Mahmoud Anwer Feb 08 '16 at 07:29
  • @MahmoudAnwer Fixed already, sorry for the mistake. See above. Added `cin.ignore()`. Reference: http://stackoverflow.com/questions/18786575/using-getline-in-c – iksemyonov Feb 08 '16 at 07:29
0

Most probably you are accessing the string using subscript which is out of index. It will be easy to answer if you point at which line you are getting the error.

kiran
  • 37
  • 5