Error/Program aborts

For my program the user needs to enter the string all_data in the format author|year|author
ex) Catcher in the Rye|1951|J. D. Salinger
but whenever I try to enter a title or author that is more than one word I get an error.
Whenever I try and enter a title that is more than one word, I get this error
"terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
Abort (core dumped)"
and whenever I try and enter an author that is more than one word it reads the second word an the next input.
I'm not sure what I'm doing wrong...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

class Book
{
public:
  //constructors
  //default construcor (no parameters)
  Book(); //sets book's default values
  Book(string new_title, int new_year, string new_author); //reads in new values for the book's title, year, and author
  Book(string all_data); //splits all_data string into separate title, year, and author

  //getters
  string get_title(); //function to get the book's title
  int get_year(); //function to get the book's year
  string get_author(); //function to get the book's author

  //setters
  void set_title(string new_title);
  void set_year(int new_year);
  void set_author(string new_author);

  void print_book();
  bool match_title(string target_title);
  bool match_year(string target_year);
  bool match_author(string target_author);
  bool match(string target);

  string convert_target_title(string tatget_title);
  string convert_target_author(string target_author);
  string convert_target(string target);

private:
  string title;
  int year;
  string author;
  
  string convert_title(string title);
  string convert_author(string author);
  
};

string convert_target_title(string target_title);

int main()
{
  Book b; //default constructor called
  Book b2("Catcher In the Rye", 1951, "J. D. Salinger");
 
  b.set_title("The Hitchhiker's Guide to the Galaxy");
  b.set_year(1979);
  b.set_author("Douglas Adams");

  string all_data;
  string target_title;
  string target_year;
  string target_author;
  string target;

  cout << "Enter all_data text: " << endl;
  cin >> all_data;
  Book b3(all_data);

  cout << "Enter title/year/author to search: " << endl;
  cin >> target;
  b3.match(target);

  cout << "Enter title to search: " << endl;
  cin >> target_title;
  b3.match_title(target_title);
  
  cout << "Enter year to search: " << endl;
  cin >> target_year;
  b3.match_year(target_year);
  
  cout << "Enter author to search: " << endl;
  cin >> target_author;
  b3.match_author(target_author);

  //b.print_book();
  //b2.print_book();
  b3.print_book();

  return 0;
}

Book::Book()
{
  title = "***";
  year = 0;
  author = "***";
}

Book::Book(string new_title, int new_year, string new_author)
{
  title = new_title;
  year = new_year;
  author = new_author;
}

Book::Book(string all_data)
{
  string s_year = to_string(year);
  int length = all_data.length();
  int index = all_data.find('|', 0);
  title = all_data.substr(0, index);
  int index2 = all_data.find('|', index + 1);
  s_year = all_data.substr(index + 1, 4);
  author = all_data.substr(index2 + 1, length - index2);

  int i_year = stoi(s_year);
  year = i_year;
}

string Book::get_title()
{
  return title;
}

int Book::get_year()
{
  return year;
}

string Book::get_author()
{
  return author;
}

void Book::set_title(string new_title)
{
  title = new_title;
}

void Book::set_year(int new_year)
{
  year = new_year;
}

void Book::set_author(string new_author)
{
  author = new_author;
}

void Book::print_book()
{
  cout << "Title: " << title << endl;
  cout << "Year Published: " << year << endl;
  cout << "Author: " << author << endl;
}

bool Book::match_title(string target_title) 
{

  string low_target_title = convert_target_title(target_title);
  string low_title = convert_title(title);

  if(low_title.find(low_target_title) != string::npos)
    {
      cout << "Title matches." << endl;
      return true;
    }
  else
    {
      cout << "Title does not match." << endl;
      return false;
    }
}

bool Book::match_author(string target_author)
{
  string low_target_author = convert_target_author(target_author);
  string low_author = convert_author(author);

  if(low_author.find(low_target_author) != string::npos)
    {
      cout << "Author matches" << endl;
      return true;
    } 
  else
    {
      cout << "Author does not match." << endl;
      return false;
    }
}
 
bool Book::match_year(string target_year)
{
  string str_year = to_string(year);

  if(str_year.find(target_year) != string::npos)
    {
      cout << "Year matches." << endl;
      return(true);
    }
  else
    {
      cout << "Year does not match." << endl;
      return(false);
    }
}

bool Book::match(string target)
{
  string low_title = convert_title(title);
  string low_author = convert_author(author);
  string str_year = to_string(year);
  string low_target = convert_target(target);

  if(low_title.find(target) != string::npos || str_year.find(target) != string::npos  || low_author.find(target) != string::npos)
    {
      cout << "Target matches" << endl;
      return(true);
    }
  else
    {
      cout << "Target does not match." << endl;
      return(false);
    }
}

string Book::convert_target_title(string target_title)
{
  for(int i = 0; i < target_title.length(); i++)
    {
      target_title[i] = tolower(target_title[i]);
    }
  return(target_title);
}

string Book::convert_target_author(string target_author)
{
  for(int i = 0; i < target_author.length(); i++)
    {
      target_author[i] = tolower(target_author[i]);
    }

  return (target_author);
}

string Book::convert_title(string title)
{
  for(int i = 0; i < title.length(); i++)
    {
      title[i] = tolower(title[i]);
    }
  return(title);
}

string Book::convert_author(string author)
{
  for(int i = 0; i < author.length(); i++)
    {
      author[i] = tolower(author[i]);
    }

  return(author);
}

string Book::convert_target(string target)
{
  for(int i = 0; i < target.length(); i++)
    {
      target[i] = tolower(target[i]);
    }

  return(target);
}
Last edited on
Topic archived. No new replies allowed.