0

I am working in simple library project in CPP, in which for taking input, i was using a getline() function.....

But at a line no 22 it is misbehaving, i have to write that function two times to get input from user, if i uses it only one time then, it doesn't give chance to get input, and moves to take next input......

that type of weird bug is occuring.....please help me to solve it :)


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

class library *arr_books[10];
int book_count=0;

//-------------------------------------------------------------------------------
class library{
      private:
            string b_name;
            string b_author;
            int b_pages;
            int b_price;
            int b_rating;
            int b_copies;
      public:
            void add_Book(){
                  cout << "Enter Book Name : " ;
                  getline(cin,b_name);
                  // getline(cin,b_name);
                  cout << "Enter Book Author : ";
                  getline(cin,b_author);
                  cout << "Enter Total Pages : ";
                  cin >> b_pages;
                  cout << "Enter Price : ";
                  cin >> b_price;
                  cout << "Enter Its Rating : ";
                  cin >> b_rating;
                  cout << "Enter Book Copies : ";
                  cin >> b_copies;
                  cout << "\n";
            }
            void show_book(){
                  cout << "Book Name   : " << b_name << endl;
                  cout << "Book Author : " << b_author << endl;
                  cout << "Total Pages : " << b_pages << endl;
                  cout << "Price       : " << b_price << endl;
                  cout << "Its Rating  : " << b_rating << endl;
                  cout << "Book Copies : " << b_copies << endl <<endl;
            }
            string book_name(){
                  return b_name;
            }
};
//-------------------------------------------------------------------------------
int add_counter(){
      book_count  += 1;
      return book_count;
}
int show_counter(){
      cout << "Total Books Present In Library : " << book_count << endl;
      cout << "Limit Of Library Is : " << 10 << endl << endl;
}
void add_new_book(){
      if (book_count != 10){
            arr_books[book_count] = new library;
            arr_books[book_count]->add_Book();
            add_counter();
      }
      else{
            cout << "Maximum Limits Of Books Are Reached :( " << endl;
      }
}
int show_book_list(){

      if (!book_count){
            cout << "Currently No Books Are Present In A Library ! \n\n";
            return 0;
      }
      for (int i=0;i<book_count;i++){
            class library *book = arr_books[i];
            cout << i+1 << ". " << book->book_name() << endl;
      }
      int num;
      cout << "\n Choose Book Number : ";
      cin >> num;
      cout << "\n";

      if (num>book_count){
            cout << " There Are Only "<< book_count << " book present in Library\n" << endl;
      }
      else if(num<=0){
            cout <<"Please Enter Right Book Number " << endl;
      }
      else{
            class library *book = arr_books[num-1];
            book->show_book();
      }
}
int show_book_names(){
      if (!book_count){
            cout << "Currently No Books Are Present In A Library ! \n" << endl;
            return 0;
      }
      for (int i=0;i<book_count;i++){
            class library *book = arr_books[i];
            cout << i+1 << ". " << book->book_name() << endl;
      }
}
//-------------------------------------------------------------------------------
int main(){

      int command;
      cout  << "\nC O M M A N D S  : " << endl;
      cout << "1. Add Book To Library " << endl;
      cout << "2. Show Books Info " << endl;
      cout << "3. List Of Books In Library " << endl;
      cout << "4. Number Of Books In Library " << endl;
      cout << "0. EXIT from Library \n" << endl;
      
      cout << "~ ";
      cin >> command;

      while (command){
            if (command == 1){add_new_book();}
            else if (command == 2){show_book_list();}
            else if (command == 3){show_book_names();}
            else if (command == 4){show_counter();}
            else{cout << "Wrong Command\n\n";}
            
            cout << "~ ";
            cin >> command;
      }
      return 0;
}```

0 Answers0