我正在处理具有以下目标的任务:
- 存储用户将客户信息提供到一系列结构中。
- 写功能以添加,显示或检索客户。
我写我的Findcust(检索例程)有问题。我想提示用户获取任何客户的名字和姓氏,然后在一系列客户中找到相关客户并打印出他们的信息。我有点卡住了,不确定如何继续。
这是我到目前为止所拥有的:
void findCust(Customer customers[], int loc)
{
string name;
const int t = 100;
cout << "nEnter the name of the customer you would like to look up: ";
getline(cin, name);
cout << endl;
for (int i = 0; i <= loc; i++) {
}
}
这就是客户结构的方式:
struct Customer {
string firstname;
string lastname;
Address home;
Address business;
};
这是我的主要功能:
int main() {
title("Customer Contact Menu");
int choice = 0;
int loc = 0;
const int SIZE = 100;
Customer contacts[SIZE];
while (choice < 5) {
choice = displayMenu();
switch (choice) {
case 1:
contacts[loc] = getCustomer();
loc++;
break;
case 2:
for (int x = 0; x < loc; x++) {
showCustomer(contacts[x]);
}
break;
case 3:
findCust(contacts,loc);
break;
case 4:
endProg();
}
}
}
我想知道如何准确读取存储在客户数组中的信息,并将其与用户输入进行比较。我尝试使用customer.compare命令,我也尝试了一些事情,我尝试了线性搜索等。但是,这是用户输入的问题无法与结构进行比较。那就是我坚持的部分。
如果我正确理解了您的问题,您想阅读和找到客户,然后打印他们的信息。为了做到这一点,我会像这样构建功能:
void findCust(Customer customers[], int array_size)
{
string first_name, last_name;
cout << "nEnter the name of the customer you would like to look up: ";
cin >> first_name >> last_name;
for (int i = 0; i < array_size; i++) {
}
}
在for循环内部,您可以进行线性搜索,并在所有客户上迭代并进行比较。即,通过数组和每个customers[i].firstname
和customers[i].lastname
检查它们是否匹配first_name
和last_name
变量。
如果他们确实致电print(customers[i])
,那将是打印出给定客户的功能。您可以将函数的定义与void print(Customer customer)
相似,并且可以包含所有打印到流中。
希望可以帮助您开始。