need help with class constructor!

I am trying to test to see if one of my constructors is working. It reads in a string that looks like this, <title>|<year published>|<author>. The constructor separates the string into title, year, and author separately.

ex Catcher in the Rye|1951|J. D. Salinger

So I need to ask for the string in the main program, but how do I send the string to the constructor then get the separated values to output?

Thanks for the help!!!!

b.Book(all_data) doesn't work, so i'm stumped.
Also, should I put
cout << title << endl;
cout << year << endl;
cout << author << endl;
at the bottom of the constructor or in the main function after I call the constructor?
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cstring>
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();
  bool match_author(string target_author);

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

//put fn prototypes

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;
  cout << "Enter all_data text: " << endl;
  cin >> all_data; 
  //b.Book(string all_data); this isn't working!!

  b.print_book();
  b2.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)
{
  int year2;
  year = year2;
  string s_year = to_string(year2);
 
  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);
  string 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) 
{
  if(title.find(target_title) != string::npos)
    {
      return true;
    }
  else
    {
      return false;
    }
}

bool Book::match_author(string target_author)
{
  if(author.find(target_author) != string::npos)
    {
      return true;
    } 
  else
    {
      return false;
    }
}
string Book::convert_title(string title)
{
  string low_title;
  low_title = title;

  for(int i = 0; i < low_title.length(); i++)
    {
      low_title[i] = tolower(low_title[i]);
    }
  return(low_title);
}

string Book::convert_author(string author)
{
  string low_author;
  author = low_author;

  for(int i = 0; i < low_author.length(); i++)
    {
      if(low_author[i] != ' ')
	{
	  low_author[i] = tolower(low_author[i]);
	}
    }
  return (low_author);
}
Last edited on
Topic archived. No new replies allowed.