0

私は新しく、初心者のプログラマーであり、学習しようとしています..私は、作成した次の関数を使用して、構造を使用してライブラリプログラムを実行しようとしています。新しい顧客を追加する、顧客の数を調べる、顧客の詳細を印刷する、本を借りる、本を予約する、本を返す

私が失敗したのはそれです; 新しい顧客を追加するとき、私のプログラムは名前、住所、および ID を要求します。また、既存の ID で新しい顧客を登録しようとすると、プログラムがエラー メッセージを表示するようにします。コード。

私はあなたにコードを求めているわけではありません。知りたいのは、私が間違っていたことと、それを修正する方法です。ヒントをいただければ幸いです。

私のコード:

    #include <iostream>
    using namespace std;

    const int maxx=100;             //max 100 users
    const int maxborrow=5;          //maxx borrow books
    int bi=0;                       //counter for books
    int i=0;                        //counter for users
    int number_of_customers=0;

        //initialize numebr of users to 0


struct loanreserved
{
    int loan;               // 1 indicates true 0 indicates false  if a book is reserved for example it's 1 if available 0
    int reserved;
};

struct duedate
{
    int day;
    int month;
    int year;
};

struct bookinf
{
    char title[maxx];
    char author[maxx];
    int ISBN;

    loanreserved loanorreserved;

    duedate bookduedate;

};

struct userinf
{
    char name[maxx];
    char address[maxx];
    int Id;
    int number_of_reserved_books;
    int number_of_loan_books;
    bookinf customersbookinf[maxborrow];
};

 userinf uniclibrary[maxx];


 int readcustomer()
 {      
     int uniqueid;
     cout<<"Customer name: ";
     cin>>uniclibrary[i].name;

     cout<<"Customer address: ";
     cin>>uniclibrary[i].address;


     cout<<"Customer Id: ";
     cin>>uniqueid;             //save id to temp file;

     for(int x=0;x<maxx;x++)
     {
        if(uniqueid!=uniclibrary[x].Id)
        {
            uniclibrary[i].Id=uniqueid;
            cout<<"Customer registration succeeded ! \n";
            number_of_customers++;
            return 1;           //success

        }

     }



     cout<<"This user is already registered ! ";
     return 0;                  //fail


     system("pause");
     system("cls");
4

1 に答える 1

0

static既存の顧客を追跡する変数を使用できます。

#include <set>

int readcustomer()
{      
   static std::set<std::string> existingNames;

   std::string name;
   cout<<"Customer name: ";
   cin>>name;

   if ( existingNames.find(name) != existingNames.end() )
   {
      //name already exists
      return 0;
   }
   else
   {
      existingNames.insert(name);
   }

   //....
}

もちろん、これは迅速な修正です。コードをコードレビューに持っていくほうがよいでしょう。改善できることはたくさんあります。

于 2012-05-29T11:30:51.557 に答える