I am getting this error when I am running -
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr
As suggested in this post(http://www.cplusplus.com/reference/string/string/getline/) , to read a line in c++, we use std::getline (std::cin,name);
My code is :
int t;
cin>>t;
while(t--)
{
string s;
int num;
getline(std::cin,s);
string q= s.substr(0,3);
num =stoi(s.substr(4));
}
When I run only this:
string s;
int num;
getline(std::cin,s);
string q= s.substr(0,3);
num =stoi(s.substr(4));
It works. I think I have to flush my cin buffer. What should be the best practice for these type of cases? Thanks in advance