gpt4 book ai didi

C++比较结构数组中的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:50 25 4
gpt4 key购买 nike

处理这段代码时,我有一系列帐户信息。我需要编写一个函数,要求用户输入名称的一部分并在数组中搜索它。

更具体地说:“搜索特定客户帐户的结构数组。它应该接受客户姓名的一部分作为参数,然后搜索名称与其匹配的帐户。应显示所有匹配的帐户(包括所有客户的信息)。如果没有匹配的帐户,则应显示一条消息。”

我似乎无法正确比较功能,我认为我只是错误地解决了这个问题,非常感谢一些指导。

这是代码,我正在处理函数“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
}
}

最佳答案

这听起来像是一个典型的数据库应用程序,所以我会首先考虑使用 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 来存储您的数据。

关于C++比较结构数组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34038267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com