Hi everyone. I want to create program which can work like primitive library add, modify, show and delete, sell etc books(objects). I think I am should adding books dynamically, right? So am I uses vector to adding new books bit what I should do to show info about particular book from array? Use iterator to find this book in vector array ?
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
usingnamespace std;
class Book
{
public:
string title;
string author;
int index;
int pages;
double price;
Book() {};
Book(string title, string author,int index,int pages,double price)
{
this->title = title;
this->author = author;
this->index = index;
this->pages = pages;
this->price = price;
}
Show()
{
cout<<"Title of book: "<<title<<endl;
cout<<"Author of book: "<<author<<endl;
cout<<"Index of book: "<<index<<endl;
cout<<"Number of pages of book: "<<pages<<endl;
cout<<"Price of book: "<<price<<endl;
}
~Book() {};
};
int main()
{
int n;
string title;
string author ;
int index;
int pages;
double price;
vector<Book> lib;
cout<<"Menu: \n 1: Add new book \n 2: Show info about book \n 3: Modify data about book \n 4: Delete book \n 5: Quit "<<endl;
while(n!=5)
{
cin>>n;
switch(n)
{
case 1:
{
cin.ignore();
cout<<"Enter title of book: "<<endl;
getline(cin,title);
cout<<"Enter author of book: "<<endl;
getline(cin,author);
cout<<"Enter index of book: "<<endl;
cin>>index;
cout<<"Enter number of pages of book: "<<endl;
cin>>pages;
cout<<"Enter price of book: "<<endl;
cin>>price;
Book new_book(title,author,index,pages,price);
lib.push_back(book);
break;
}
case 2: cout<<"Show";
{
cout<<"Enter title of books to get info: "<<endl;
cin>>title;
cout<<"Enter title of books to get info: "<<endl;
getline(cin,title);
for(vector<Book>::iterator it = lib.begin() ; it !=lib.end(); ++it)
{
if(it->title==title)
it->Show();
}
break;
}
case 3: cout<<"Modify";
break;
case 4: cout<<"Delete";
break;
case 5: cout<<"Quit";
break;
}
}
return 0;
}
You forgot semicolons on lines 39 - 42. You forgot to name a type for the Show() method on line 24.
You use std::cin on line 50 and then you use std::getline() in line 56 and on, without clearing the state of the stream using cin.ignore(), which means the next character read off of the stream would be a '\n', which isn't correct.
You also access "title" on line 79 using the dot operator (.) when you should access it using the arrow operator (->).