2012-05-29 99 views
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"); 

回答

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

    //.... 
} 

当然,这是一个快速解决。最好把你的代码放到codereview中。可以改进的很多。

相关问题