Home > Software design >  C Library management system
C Library management system

Time:06-03

I'm doing a library management system using arrays as database, when I enter a value in bookName, and after a loop and enter a value again, when I print all values in bookName[], it only prints the last value I entered. Please help me!!

Heres my code:

#include<iostream>
using namespace std;

int main(){
    char yesNo;
    string bookName[50];
    string bookAuthor[50];
    string bookID[50];
    int i, choice;
    do{
    
    cout << "==========================" << endl;
    cout << "LIBRARY MANAGEMENT SYSTEM" << endl;
    cout << "==========================" << endl;
    cout << "[1] Add Books" << endl;
    cout << "[2] Delete Books" << endl;
    cout << "[3] Search Books" << endl;
    cout << "[4] View Book List" << endl;
    cout << "[5] Close Application" << endl;
    cout<<"Enter a number: ";
    cin>>choice;
    switch(choice){
    case 1:
        cin.ignore();
        cout<<"Enter book name: ";
        for(i=0; i<10; i  ){
        getline(cin, bookName[i]);
        break;}

        cout<<"Enter book author: ";
        getline(cin, bookAuthor[i]);
        cout<<"Enter book ID: ";
        getline(cin, bookID[i]);
        
        cout<<"Book succesfully added!"<<endl;
        break;
    case 4:
        cout<<"All Books"<<endl;
        for(int x=0; x<=i; x  ){
            cout<< bookName[x];
        }   
    }
    cout<<"Do you want to continue (Y/N)";
    cin>>yesNo;
    

    }while (yesNo == 'y' || yesNo == 'Y');
        cout<<"Thank You!";

    return 0;
}

CodePudding user response:

The for loop in your code initializes the variable i to zero, everytime you want to add a new book. Inside the for loop there is a break that exits the for loop after entering the string describing the book name. When the user selects option 4, the variable i will always be zero. Therefore, you will only print the info of the last book you entered. For completeness, every book you enter will always be stored at index 0 (replacing the previous entry).

The following fixes your problem - quick and dirty. However, you might want to re-design your code:

#include<iostream>
using namespace std;

int main(){
  char yesNo;
  string bookName[50];
  string bookAuthor[50];
  string bookID[50];
  int i = 0, choice; // Important! You need to initialize the 
                     // variable i since this is going to be used 
                     // as index to access the array!

  do
  {
    cout << "==========================" << endl;
    cout << "LIBRARY MANAGEMENT SYSTEM" << endl;
    cout << "==========================" << endl;
    cout << "[1] Add Books" << endl;
    cout << "[2] Delete Books" << endl;
    cout << "[3] Search Books" << endl;
    cout << "[4] View Book List" << endl;
    cout << "[5] Close Application" << endl;
    cout<<"Enter a number: ";
    cin>>choice;
    switch(choice)
    {
      case 1:
      cin.ignore();
      cout<<"Enter book name: ";
      getline(cin, bookName[i]);
      cout<<"Enter book author: ";
      getline(cin, bookAuthor[i]);
      cout<<"Enter book ID: ";
      getline(cin, bookID[i]);
      i  ; // Increment i. Check here that you don't go above 50
      cout<<"Book succesfully added!"<<endl;
      break;
    case 4:
      cout<<"All Books"<<endl;
      for(int x=0; x<i; x  ) // C/C   start from 0. So the loop needs to end when index is i-1
      {
          cout<< bookName[x] << endl;
      }   
     }
     cout<<"Do you want to continue (Y/N)";
     cin>>yesNo;
}while (yesNo == 'y' || yesNo == 'Y');
    cout<<"Thank You!";

return 0;
}

You can see a running version here: https://onlinegdb.com/TNUn3jpyM

  •  Tags:  
  • c
  • Related