2

Almost everything is working fine until The Title of your Book is. As you can see, i have provided cin to get the title and display it. The problem is that part alone isn't allowing me to type the title as required. If anyone can help i will be grateful. If i am unable to express myself very well, just draw me attention.

   #include <iostream>
#include <string>
using namespace std;

class library  {      
  public:
  
  // data members
    string title;
    int price;
    int accessionNumber;
    
    // public member function for getting info
    void getInfo() 
    { 
       cout << "\n";      
       cout << "Please Provide the Following: " <<endl;
       
       cout << "Book Title: ";
       getline (cin, title);
       
       cout << "Book Price: ";
       cin >> price;
       
       cout << "Book Accession Number: ";
       cin >> accessionNumber;
       
    }
    
     // public member function for displaying info
    void showInfo() 
    { 
      cout << " \n";
      cout <<"The Title of your Book is: " <<title <<endl;
      cout <<"The Price is $" <<price <<endl;
      cout <<"Use the Accession Number " <<accessionNumber << " to locate it"<<endl;
      
    }
};

class books: public library  {
  public:
  
  // data member
    int pages;
    
    void getPageNumber(){
       cout << " \n";
       cout << "What page can i find the Summary? ";
       cin >> pages;
    }
    
    void displayPageNumber(){
        cout <<"Check page "<< pages <<endl;
        
    }
};

class media: public library  {
  public:
  
   // data member
    string audioName;
    
    void getaudioName(){
       cout << " \n";
       cout << "Can you tell me the title of the Christian audio? " <<endl;
       //getline (cin, audioName);
       cin >> audioName;
    }
    
    void printaudioName(){
        cout <<"It is the voice of Bishop Ansah. He titled it "<< audioName <<endl;
    }
};

class CD: public library  {
  public:
  
   // data member
    double playTime;
    
    void getPlayTime(){
       cout << " \n";
       cout << "The OOPs Video lessons is about how many hours? ";
       cin >> playTime;
    }
    
    void showPlayTime(){
        cout <<"It should be about "<< playTime << " hours in all" <<endl;
    }
};


int main() {
  
  // creating an object for the various class
  books bk;
  media md;
  CD cd;
  
  // calling member functions from books class
   bk.getPageNumber();
   bk.displayPageNumber();
   
  // calling member functions from media class
   md.getaudioName();
   md.printaudioName();
   
   cd.getPlayTime();
   cd.showPlayTime();
   
  // calling member functions from library class
  cd.getInfo();
  cd.showInfo();
  
  return 0;
}

This is the output of the code. TAKE NOTE OF WHERE THE BOOK TITLE: IS. It is suppose to allow me to input a book title but it does not seem to be doing so. The asterik is only attached to make stand out.

What page can i find the Summary? 77                                                                                                   
Check page 77                                                                                                                          
                                                                                                                                   
Can you tell me the title of the Christian audio?                                                                                      
Resurrection                                                                                                                           
It is the voice of Bishop Ansah. He titled it Resurrection                                                                             
                                                                                                                                   
The OOPs Video lessons is about how many hours? 8                                                                                      
It should be about 8 hours in all                                                                                                      
                                                                                                                                   
Please Provide the Following:                                                                                                          
Book Title: Book Price: 9                                                                                                              
Book Accession Number: 8                                                                                                               
                                                                                                                                   
**Book Title**:                                                                                                             
The Price is $9                                                                                                                        
Use the Accession Number 8 to locate it                                                                                                
                                     
Kennedy Owusu
  • 3,235
  • 2
  • 9
  • 16

1 Answers1

2
class CD: public library  {
  public:
  
   // data member
    double playTime;
    
    void getPlayTime(){
       cout << " \n";
       cout << "The OOPs Video lessons is about how many hours? ";
       cin >> playTime;
       cin.ignore();
    }
    
    void showPlayTime(){
        cout <<"It should be about "<< playTime << " hours in all" <<endl;
    }
};
cd.getPlayTime();
cd.showPlayTime();
   
// calling member functions from library class
cd.getInfo();

Add cin.ignore() after playTime input because you have to clear the buffer before using getline().

For further Explanation see this link: https://www.tutorialspoint.com/what-is-the-use-of-cin-ignore-in-cplusplus

Usman Ali
  • 336
  • 1
  • 9