0
    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...

赵颜哲
  • 3
  • 1
  • 1
    You're leaving newlines in your input stream after the formatted extractions of `n` and `m`. Those are being picked up by the first `getline` pulls of each loop. Since you're using nothing but string data everywhere and never check for Io failures or validate input, the condition escapes you. Debug your code and you'll see your *first* book ID is `""`. – WhozCraig Feb 16 '22 at 09:09
  • In the nested loop, should it be `book[j].ID` instead of `book[i].ID`? – kiner_shah Feb 16 '22 at 09:13
  • Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – BoP Feb 16 '22 at 11:42
  • Thanks from helping!I have made some stupid mistakes... – 赵颜哲 Feb 23 '22 at 12:28

0 Answers0