我必须编写一个程序来创建具有10个结构的数组。它应该让用户将数据输入到数组中,更改任何元素的内容,并显示存储在数组中的所有数据。应该是菜单驱动的用户界面。我必须具有一个允许用户在结构数组中搜索客户名称的功能。它应接受部分客户名称作为参数,然后搜索名称与其匹配的帐户。应显示所有匹配的帐户。如果没有帐户匹配,则应该显示一条消息。我试图编写函数,但是遇到了一些麻烦。我还是C ++的新手,我的教科书并不是很有帮助。该函数无效findAccountInfo。当我运行程序时,它不会提示用户按名称搜索。我不确定我缺少什么。我将在下面发布我的代码。
#include <iostream>
#include<string>
using namespace std;
struct customerAccount
{
string name, address, city, state, zip, phone;
double account_balance;
string last_payment_date;
};
void menu()
{
cout << "\n MENU " << endl;
cout << "1. Enter new customer information" << endl;
cout << "2. Change customer information" << endl;
cout << "3. Display all customer information" << endl;
cout << "4. Exit" << endl;
}
void createNewAccount(struct customerAccount cus_arr[], int index)
{
cout << "\nCustomer name: ";
cin.ignore();
getline(cin, cus_arr[index].name);
cout << "Customer address: ";
getline(cin, cus_arr[index].address);
cout << "City: ";
getline(cin, cus_arr[index].city);
cout << "State: ";
getline(cin, cus_arr[index].state);
cout << "ZIP Code: ";
getline(cin, cus_arr[index].zip);
cout << "Telephone: ";
getline(cin, cus_arr[index].phone);
cout << "Account balance: ";
cin >> cus_arr[index].account_balance;
if (cus_arr[index].account_balance < 0)
{
cout << "Account balance cannot be negative. Please re-enter account balance: " << endl;
cin >> cus_arr[index].account_balance;
}
cout << "Date of last payment: ";
cin.ignore();
getline(cin, cus_arr[index].last_payment_date);
cout << "You have entered information for customer";
cout << " number " << index << endl;
}
void changeAccountInfo(struct customerAccount cus_arr[], int cus_num, int size)
{
if (cus_num < 0 || cus_num > size)
{
cout << "Invalid Customer Number!" << endl;
}
else
{
cout << "\nCustomer name: " << cus_arr[cus_num].name << endl;
cout << "Customer address: " << cus_arr[cus_num].address << endl;
cout << "City: " << cus_arr[cus_num].city << endl;
cout << "State: " << cus_arr[cus_num].state << endl;
cout << "ZIP Code: " << cus_arr[cus_num].zip << endl;
cout << "Telephone: " << cus_arr[cus_num].phone << endl;
cout << "Account balnace: " << cus_arr[cus_num].account_balance << endl;
cout << "Date of last payment: " << cus_arr[cus_num].last_payment_date << endl;
cout << endl;
cout << "Edit the account information of the ";
cout << "customer " << cus_num << endl;
cout << "\nCustomer name: ";
cin.ignore();
getline(cin, cus_arr[cus_num].name);
cout << "Customer address: ";
getline(cin, cus_arr[cus_num].address);
cout << "City: ";
getline(cin, cus_arr[cus_num].city);
cout << "State: ";
getline(cin, cus_arr[cus_num].state);
cout << "ZIP Code: ";
getline(cin, cus_arr[cus_num].zip);
cout << "Telephone: ";
getline(cin, cus_arr[cus_num].phone);
cout << "Account balance: ";
cin >> cus_arr[cus_num].account_balance;
cout << "Date of last payment: ";
cin.ignore();
getline(cin, cus_arr[cus_num].last_payment_date);
cout << "Account information for customer " << cus_num << " has been updated." << endl;
}
}
void displayAccountInfo(struct customerAccount cus_arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "\nCustomer name: " << cus_arr[i].name << endl;
cout << "Customer address: " << cus_arr[i].address << endl;
cout << "City: " << cus_arr[i].city << endl;
cout << "State: " << cus_arr[i].state << endl;
cout << "ZIP Code: " << cus_arr[i].zip << endl;
cout << "Telephone: " << cus_arr[i].phone << endl;
cout << "Account balnace: " << cus_arr[i].account_balance << endl;
cout << "Date of last payment: " << cus_arr[i].last_payment_date << endl;
}
cin.ignore();
cout << "\nPress enter to continue..." << endl;
if (cin.get() == '\n')
{
return;
}
}
这是我遇到的功能
void findAccountInfo(struct customerAccount cus_arr[], customerAccount cus_acc_arr)
{
int index = 0;
int cuss_num = 0;
string searchName = "";
bool found = false;
cout << "Enter a name (partial or full) to search for: ";
cin.ignore();
getline(cin, searchName);
for (index = 0; index < cuss_num; index++)
{
if (cus_arr[index].name.find(searchName) != string::npos)
{
cout << "Account Name:" << (index + 1) << "" << cus_arr[index].name << "\n";
found = true;
}
}
if (found == true)
{
cout << "Enter account id:";
cin >> cuss_num;
}
else
{
cout << "This name was not found in the database";
}
return;
}
我的其他代码
int main()
{
int choice = 0;
int i, f = 0;
i = 0;
int cus_num = 0;
customerAccount cus_acc_arr[20];
menu();
cout << "Please enter a choice: ";
cin >> choice;
while (true)
{
switch (choice)
{
case 1:
createNewAccount(cus_acc_arr, i);
i++;
break;
case 2:
cout << "Enter a customer number to ";
cout << "change the account ";
cout << "information:" << endl;
cout << "Customer number: ";
cin >> cus_num;
changeAccountInfo(cus_acc_arr, cus_num, i);
break;
case 3:
displayAccountInfo(cus_acc_arr, i);
break;
case 4:
system("pause");
return 0;
default:
f = 1;
cout << "Please enter a valid choice ";
cout << "between 1, 2, 3, and 4:" << endl;
menu();
cout << "Please enter a choice: ";
cin >> choice;
}
if (f != 1)
{
menu();
cout << "Please enter a choice: ";
cin >> choice;
}
}
system("pause");
return 0;
}