C++图书馆管理系统

C++ Library management system

我正在做一个使用数组作为数据库的图书馆管理系统,当我在 bookName 中输入一个值,并在循环后再次输入一个值时,当我打印 bookName[] 中的所有值时,它只打印我输入的最后一个值。请帮帮我!!

这是我的代码:

#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;
}

每次您想添加一本新书时,代码中的 for 循环都会将变量 i 初始化为零。在 for 循环内部有一个中断,在输入描述书名的字符串后退出 for 循环。 当用户选择选项 4 时,变量 i 将始终为零。因此,您只会打印您输入的最后一本书的信息。为了完整起见,您输入的每本书都将始终存储在索引 0 处(替换之前的条目)。

以下解决了您的问题 - 快速而肮脏。但是,您可能想要 re-design 您的代码:

#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;
}

您可以在此处查看 运行 版本:https://onlinegdb.com/TNUn3jpyM