You're on the right track in trying to create a Book class and input book details in a loop, but there are several issues in your code that need to be corrected. Let's go through them step by step:
🔍 Problems in Your Code
1. ❌ Typo in class keyword
Class Book{ // ❌
C++ is case-sensitive. It should be:
class Book { // ✅
2. ❌ Incorrect method declarations and return types
You're declaring methods like:
string getName(string); // ❌
This implies getName takes a string and returns a string, which is not standard for getter methods.
✅ It should be:
string getName(); // ✅ no parameter
Same issue with getAuthor and getPrice.
3. ❌ Incorrect use of parameters in setters
Setters shouldn't take the same type as their value unless you're validating, but even then, your current usage is off.
Also, this:
obj.setName(""); // You're passing an empty string ❌
makes no sense if you're trying to read input from the user.
4. ❌ Missing #include <string>
You're using std::string, so don't forget:
#include <string>
5. ❌ Undeclared variable i in for-loop
for(i=0; i<5; i++){ // ❌ 'i' is not declared
Should be:
for(int i = 0; i < 5; i++){ // ✅
6. ❌ BinarySearch method doesn't belong here
Unless you're planning to search a list of books, having a BinarySearch method inside the Book class is logically misplaced. But we'll leave it for now.
7. ❌ No actual input taken from user
You're setting names to empty strings instead of reading input.
✅ Corrected & Improved Version of Your Code
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string bookname;
string author;
int price;
public:
// Corrected getters (no parameters)
string getName() { return bookname; }
string getAuthor() { return author; }
int getPrice() { return price; }
// Setters
void setName(string name) { bookname = name; }
void setAuthor(string authorName) { author = authorName; }
void setPrice(int bookPrice) { price = bookPrice; }
void details() {
cout << "Book Name: " << bookname << endl;
cout << "Author: " << author << endl;
cout << "Price: " << price << endl;
}
// Placeholder for BinarySearch (logic unclear in current context)
int BinarySearch(int, int, int) {
// Not implemented
return -1;
}
};
int main() {
Book obj;
string name;
string author;
int price;
cout << "Enter the book details:" << endl;
for (int i = 0; i < 5; i++) {
cout << "\nBook " << i + 1 << ":" << endl;
cout << "Enter name: ";
getline(cin, name);
obj.setName(name);
cout << "Enter author: ";
getline(cin, author);
obj.setAuthor(author);
cout << "Enter price: ";
cin >> price;
cin.ignore(); // To consume leftover newline character
obj.setPrice(price);
// Display entered info
cout << "\nEntered Details:\n";
obj.details();
}
return 0;
}
Wait — actually, in your original code you created only one Book object and were trying to enter data into it 5 times. That would overwrite previous values each time. Is that really what you want?
If you want to store 5 different books, you'll need an array or vector of Book objects, like this:
✅ Better Version: Store Multiple Books Using vector
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book {
private:
string bookname;
string author;
int price;
public:
string getName() { return bookname; }
string getAuthor() { return author; }
int getPrice() { return price; }
void setName(string name) { bookname = name; }
void setAuthor(string authorName) { author = authorName; }
void setPrice(int bookPrice) { price = bookPrice; }
void details() {
cout << "Book Name: " << bookname << endl;
cout << "Author: " << author << endl;
cout << "Price: " << price << endl;
}
int BinarySearch(int, int, int) {
// Not implemented
return -1;
}
};
int main() {
vector<Book> books(5); // Create 5 Book objects
string name;
string author;
int price;
cout << "Enter details for 5 books:\n";
for (int i = 0; i < 5; i++) {
cout << "\nBook " << i + 1 << ":" << endl;
cout << "Enter name: ";
getline(cin, name);
books[i].setName(name);
cout << "Enter author: ";
getline(cin, author);
books[i].setAuthor(author);
cout << "Enter price: ";
cin >> price;
cin.ignore(); // clean up newline
books[i].setPrice(price);
cout << "\nEntered Details:\n";
books[i].details();
}
return 0;
}
✅ Summary of Fixes
| Issue | Fix |
|---|
Class → class | C++ is case-sensitive |
| Incorrect getters/setters | Remove parameters from getters |
| No input being taken | Use getline() and cin properly |
| Only one object | Use array/vector for multiple books |
| Missing headers | Add #include <string> and #include <vector> |
Would you like help implementing the BinarySearch function as well?