for (int i = 0; i < m; i++) {
getline(cin,que[i]);
cout << que[i] << endl;
int cnt = 0;
string a = que[i].substr(3);
for (int j = 0; j < n; j++) {
if (a == book[i].ID || a == book[i].title || a == book[i].author || a == book[i].key || a == book[i].pub || a == book[i].year) {
cnt++;
cout << book[i].ID << endl;
}
}
if (!cnt) {
printf("Not Found\n");
}
}
when I run the code above ,Xcode reports that "terminating with uncaught exception of type std::out_of_range: basic_string". I find the problem from the sentence "string a = que[i].substr(3);"But I don't know why...
Here are my complete code sentence:
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef struct{
string ID;
string title;
string author;
string key;
string pub;
string year;
} node;
int main(int argc, const char * argv[]) {
int n = 0;
cin >> n;
vector<node> book(n);
for (int i = 0; i < n; i++) {
getline(cin,book[i].ID);
getline(cin,book[i].title);
getline(cin,book[i].author);
getline(cin,book[i].key);
getline(cin,book[i].pub);
getline(cin,book[i].year);
}
int m = 0;
cin >> m;
vector<string> que(m);
for (int i = 0; i < m; i++) {
getline(cin,que[i]);
cout << que[i] << endl;
int cnt = 0;
string a = que[i].substr(3);
for (int j = 0; j < n; j++) {
if (a == book[i].ID || a == book[i].title || a == book[i].author || a == book[i].key || a == book[i].pub || a == book[i].year) {
cnt++;
cout << book[i].ID << endl;
}
}
if (!cnt) {
printf("Not Found\n");
}
}
return 0;
}
And here is the input:
3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla
I can not figure this out...