Adding and deleting objects

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 ?
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
 #include <iostream>
#include <string>
#include <cstdio>
#include <vector>
using namespace 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;
}
Last edited on
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 (->).
Thank you!
Topic archived. No new replies allowed.