我是新手和新手程序员并且正在努力学习..我一直在尝试使用结构来创建库程序,我创建了以下函数。添加新客户,查找客户数量,打印客户详细信息,借书,预订簿,还本书
我失败的是那个;当我添加一个新客户时,我的程序要求输入名称,地址和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 :(得分: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。可以改进的很多。