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

You can have a static variable that keeps track of existing customers:

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

   //....
}

Of course, this is a quick fix. Better take your code to codereview. The're MUCH that can be improved.

于 2012-05-29T11:30:51.557 回答