search through array of objects

Hey all, working on an assignment. I'm trying to have the user search for a book by title out of the ones given and then having it display its info. could anyone tell me why this isn't working?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(option == 2)
    {
        cout << "What book would you like details for?: ";
        cin >> bookName;

        for (int i = 0; i < max; i++)
            {
               if(bookName = libBooks[i].getTitle()){

                cout <<"Book "<< i+1 <<": ";
                libBooks[i].output();
               }
            }

    }

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
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

    class Book
    {
    private:
        string title;
        string author;
        string status;
        int option;
    public:
        void setTitle(string title_);
        void setAuthor(string author_);
        void setStatus(string status_);
        void output()const;



        string getTitle()const;
        string getAuthor()const;
        string getStatus()const;

        Book();
        Book(string title_,string author_);
        Book(string title_, string author_,string status_);

    };

int main()
{
    int option;
    int max = 4;
    string bookName;

    Book object1;
    Book libBooks[max] = {Book("The Prince of Milk","Exurb1a","Available"),
                          Book("Spooky Pookie","Sandra Boynton","Available"),
                          Book("The Alchemist","Paulo Coelho","Available"),
                          Book("Primary Colors","Anonymous")};
    cout << "What would you like to do? " << endl;
    cout << "1. View all books." << endl;
    cout << "2. Search for a book." << endl;
    cout << "3. Exit." << endl;
do {



    cin >> option;

    cout << endl;

        for (int i = 0; i < max; i++)
            {
                cout <<"Book "<< i+1 <<": ";
                libBooks[i].output();
            }
    cout << endl;
}while(option == 1);

if(option == 2)
    {
        cout << "What book would you like details for?: ";
        cin >> bookName;

        for (int i = 0; i < max; i++)
            {
               if(bookName = libBooks[i].getTitle()){

                cout <<"Book "<< i+1 <<": ";
                libBooks[i].output();
               }
            }

    }

    return 0;
    }

Book::Book()
{
    title = "N/A";
    author = "N/A";
    status = "N/A";
}
Book::Book(string title_, string author_)
{
    title = title_;
    author = author_;
    status = "Available";
}
Book::Book(string title_,string author_, string status_)
{
    title = title_;
    author = author_;
    status = status_;
}
void Book::setTitle(string title_)
{
    title = title_;

}
void Book::setAuthor(string author_)
{
    author = author_;

}
void Book::setStatus(string status_)
{
    status = status_;

}
string Book::getTitle()const {
    return title;
}
string Book::getAuthor()const {
    return author;
}
string Book::getStatus()const {
    return status;
}
void Book::output()const
{
    cout << "Title: " << title;
    cout << " Author: " << author;
    cout << " Status: " << status << endl;
}
Two big issues here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(option == 2)
    {
        cout << "What book would you like details for?: ";
        cin >> bookName; //This only grabs a single word, you want to use getline

        for (int i = 0; i < max; i++)
            {
               if(bookName = libBooks[i].getTitle()){ // used = instead of ==

                cout <<"Book "<< i+1 <<": ";
                libBooks[i].output();
               }
            }

    }


The if statement has = instead of ==. A bigger issue is that you're using cin >>. Let's say they want to see the book "The Prince of Milk", they'd type it in, cin >> bookName would only grab "The" and then try to find a book by that name.

You want getline(cin, bookName). This will grab the whole line written.
cin >> bookName;

This only reads until it encounters whitespace, so "The Prince of Milk" becomes "The"

Investigate using std::getline

The max variable should be const or better constexpr

In function 'int main()':
69:52: error: could not convert 'bookName.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((* & Book::getTitle() const()))' from 'std::basic_string<char>' to 'bool'


The function returns a string, but you asking for to do bool in the if statement.

In future be more explicit about what the problem is: " could anyone tell me why this isn't working?" is not a good explanation.
Ive updated it to this, but now its just exiting after i try to search for a title,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(option == 2)
    {
        cout << "What book would you like details for?: ";
        cin >> bookName;

        getline(cin, bookName);
        for (int i = 0; i < max; i++)
            {

               if(bookName == libBooks[i].getTitle()){

                cout <<"Book "<< i+1 <<": ";
                libBooks[i].output();
               }
            }

    }


is it just not finding any matching titles? Do I need to use getline to somehow grab the whole line form the constructor where I initialized some of the books since my titles in the constructors also include spaces?
@persades, @TheIdeasMan told you to use getline INSTEAD OF cin >>, NOT BOTH.
@lastchance, The reason i landed where its at is because when i tried to use getline alone the program was exiting immediately after choosing option 2, it wont even give me the chance to search for something
The code you showed would do this:

Input: The Prince of Milk
bookName = The

....

bookName = Prince of Milk


The cin >> will take "The" and set bookName to it. Then the getline will grab "Prince of Milk" and set bookName to it. Does that sound right to you?
@persades,
Change
cin >> option;
to
cin >> option; cin.ignore( 1000, '\n' );
This will clear a residual line feed before getline() sees it.

Please don't code something that is patently wrong in an attempt to clear a problem that actually arose earlier.


Your code will still not loop back to do anything more than one book search, though.


Your indentation also makes your code difficult to read. Use a consistent style and indentation, and don't use an unholy mix of spaces and tabs. (Personally, I think you should stick to just spaces for code: the tab settings in different environments are unlikely to match those in your code editor.)
Last edited on
Perhaps something like:

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
include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Book {
private:
	string title {"N/A"};
	string author {"N/A"};
	string status {"N/A"};

public:
	void setTitle(const string& title_);
	void setAuthor(const string& author_);
	void setStatus(const string& status_);
	void output() const;

	string getTitle() const;
	string getAuthor() const;
	string getStatus() const;

	Book() {}
	Book(const string& title_, const string& author_, const string& status_ = "Available");
};

int main() {
	constexpr int max {4};

	const Book libBooks[max] {{"The Prince of Milk","Exurb1a"},
						{"Spooky Pookie","Sandra Boynton"},
						{"The Alchemist","Paulo Coelho"},
						{"Primary Colors", "Anonymous" }};

	for (int option {}; option != 3; ) {
		cout << "\n1. View all books.\n";
		cout << "2. Search for a book.\n";
		cout << "3. Exit.\n";
		cout << "\nWhat would you like to do: ";

		cin >> option;

		switch (option) {
			case 1:
				for (int i = 0; i < max; ++i) {
					cout << "Book " << i + 1 << ": ";
					libBooks[i].output();
				}

				break;

			case 2:
			{
				string bookName;
				bool fnd {};

				cout << "What book would you like details for?: ";
				getline(std::cin >> ws, bookName);

				for (int i = 0; !fnd && i < max; i++)
					if (bookName == libBooks[i].getTitle()) {
						cout << "\nBook " << i + 1 << ": ";
						libBooks[i].output();
						fnd = true;
					}

				if (!fnd)
					cout << "Not found\n";
			}
				break;

			case 3:
				break;

			default:
				cout << "Invalid option\n";
				break;
		}
	}
}

Book::Book(const string& title_, const string& author_, const string& status_) : title(title_), author(author_), status(status_) {}

void Book::setTitle(const string& title_) { title = title_; }
void Book::setAuthor(const string& author_) { author = author_; }
void Book::setStatus(const string& status_) { status = status_; }
string Book::getTitle() const { return title; }
string Book::getAuthor() const { return author; }
string Book::getStatus() const { return status; }

void Book::output() const {
	cout << "\nTitle: " << title;
	cout << "\nAuthor: " << author;
	cout << "\nStatus: " << status << '\n';
}

Topic archived. No new replies allowed.