使用此代码,我有一系列帐户信息。我需要编写一个函数,我要求用户输入一个名称的一部分,然后在数组中搜索它。
更具体地说:“在结构数组中搜索特定客户的帐户。它应该接受客户名称的一部分作为参数,然后搜索名称与其匹配的帐户。应显示所有匹配的帐户(包括所有客户的信息。如果没有帐户匹配,则应显示一条说明的消息。“
我似乎无法正确地比较这些功能,我想我只是错误地接近这个问题而且非常感谢一些指导。
这是代码,我正在处理函数“searchAccount”
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
struct Customer
{
string name;
string address;
string city;
string state;
int zip;
string num;
string date;
double bal;
};
void readData(Customer []);
void displayData(Customer []);
void changeInfo(Customer []);
void searchAccount(Customer []);
const int MAX = 10;
int main()
{
int selection;
Customer data[MAX];
do
{
cout << "Customer Accounts Menu" << endl;
cout << "------------------------------" << endl;
cout << "1. Read customer info into array. " << endl;
cout << "2. Change account information. " << endl;
cout << "3. Display information in array. " << endl;
cout << "4. Search for customer account. " << endl;
cout << "5. Sort customer balances in descending order. " << endl;
cout << "6. Sort customer names in ascending order. " << endl;
cout << "7. Compare names of two customers & replace smallest with address of largest" << endl;
cout << "8. Exit program" << endl;
cout << "------------------------------" << endl;
cout << "Enter a selection: " << endl;
cin >> selection;
switch (selection)
{
case (1):
readData(data);
break;
case (2):
changeInfo(data);
break;
case (3):
displayData(data);
break;
case (4):
searchAccount(data);
break;
case (5):
break;
case (6):
break;
case (7):
break;
default: cout << "Please enter a valid selection." << endl;
}
}while(selection != 8);
return 0;
}
void readData(Customer data[])
{
int num = 0;
ifstream inFile;
inFile.open("customers.txt");
if (!inFile)
{
cout << "Cannot open the file" << endl;
}
else
{
inFile >> data[num].name >> data[num].address >> data[num].city >> data[num].state >> data[num].zip >> data[num].num >> data[num].date >> data[num].bal;
while (inFile)
{
num++;
inFile >> data[num].name >> data[num].address >> data[num].city >> data[num].state >> data[num].zip >> data[num].num >> data[num].date >> data[num].bal;
}
}
inFile.close();
}
void displayData(Customer data[])
{
for (int num = 0; num < MAX; num++)
{
cout << data[num].name << " " << data[num].address << " " << data[num].city << " " << data[num].state << " " << data[num].zip << " " << data[num].num << " " << data[num].date << " " << data[num].bal << endl;
}
}
void changeInfo(Customer data[])
{
string name;
cout << "Enter customer name to change" << endl;
cin >> name;
for (int i = 0; i < MAX; i++)
{
if (data[i].name == name)
{
cout << "Enter new information for " << name << endl;
cout << "Name: " << endl;
cin >> data[i].name;
cout << "Address: " << endl;
cin >> data[i].address;
cout << "City: " << endl;
cin >> data[i].city;
cout << "State: " << endl;
cin >> data[i].state;
cout << "Zip: " << endl;
cin >> data[i].zip;
cout << "Phone Number: " << endl;
cin >> data[i].num;
cout << "Date of Last Payment: " << endl;
cin >> data[i].date;
cout << "Account Balance: " << endl;
cin >> data[i].bal;
}
}
}
void searchAccount(Customer data[])
{
string name;
cout << "Enter a name to search: " << endl;
cin >> name;
for (int i = 0; i < MAX; i++)
{
if (strcmp(name, "data[i].name")
//Not sure what to put here, the function above won't correctly as well
}
}
答案 0 :(得分:0)
首先,包含包含标准库中算法的标题:
#include <algorithm>
然后,在searchAccount
函数中执行以下操作:
string name;
std::string tempLowercase;
cout << "Enter a name to search: " << endl;
cin >> name;
// Convert entered name to lower case
string enteredLowercase = name;
std::transform(enteredLowercase.begin(), enteredLowercase.end(), enteredLowercase.begin(), ::tolower);
bool found = false;
for(int i = 0; i < MAX; i++)
{
// Convert current name to lower case and compare with entered name
tempLowercase = data[i].name;
std::transform(tempLowercase.begin(), tempLowercase.end(), tempLowercase.begin(), ::tolower);
if (std::string::npos != tempLowercase.find(enteredLowercase))
{
cout << data[i].name << " " << data[i].address << " " << data[i].city
<< " " << data[i].state << " " << data[i].zip << " " << data[i].num
<< " " << data[i].date << " " << data[i].bal << endl;
if(!found)
found = true;
}
}
if(!found)
cout << "No customers found" << endl;
答案 1 :(得分:0)
这听起来像是一个典型的数据库应用程序,因此我首先考虑使用SQLite。标准C ++不提供开箱即用的此功能。但是,如果您需要坚持使用简单的C ++,那么我认为您正在寻找:
bool found_one = false;
for( int i = 0; i < MAX; i++ )
{
if( data[i].name.find( name ) != std::string::npos )
{
found_one = true;
// output the record
}
}
if( !found_one )
{
// output error message
}
作为旁注:您的代码看起来更像C而不是C ++。我建议你考虑使用std容器,例如std :: vector来存储你的数据。