创建一个Struct然后通过引用传递带有这些结构的向量

时间:2016-11-05 20:18:47

标签: c++

我想先说有很多这样的问题,但对我而言,除非处理我的情况,否则很难理解这些解释。

这里:是我正在进行的计划的完整问题。

11.7:客户账户 编写一个程序,使用结构存储有关客户帐户的以下数据:

 Customer name 
 Customer address
 City
 State
 ZIP code
 Telephone
 Account balance
 Date of last payment

该程序应使用至少20个结构的数组。它应该让用户将数据输入到数组中,更改任何元素的内容,并显示存储在数组中的所有数据。该程序应具有菜单驱动的用户界面。

这是一些示例代码,因为粘贴我的完整代码太多b / c我必须经历并在每行的开头添加4个空格。

#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct CustomerAccount{
        string customerName;
        string customerAddress;
        string customerCity;
        string customerState;
        int customerZipCode;
        string customerTelephone;
        double customerAccountBalance;
        string customerDateOfLastPayment;
};

void testFunction(vector<CustomerAccount> &stuff){
    stuff.push_back(CustomerAccount());
    stuff[0].customerName = "dale";
    stuff[0].customerAddress = "123 test road";
    stuff[0].customerCity = "Fake City";
    stuff[0].customerState = "`Merica";
    stuff[0].customerZipCode = 12345;
    stuff[0].customerTelephone = "123-456-7899";
    stuff[0].customerAccountBalance = 200.20;
    stuff[0].customerDateOfLastPayment = "11/5/2016";
};



int main(){
    vector<CustomerAccount> stuff;
    //testFunction(vector<CustomerAccount> &stuff); ---Incorrect way (Thank you mkmostafa)
    testFunction(stuff); //The Correct way
    cout << stuff[0].customerName << endl;

};

基本上

1:创建一个矢量。

2:该向量中的每个元素都有结构和相关数据。

3:使用函数修改这些元素中的数据,因此我需要通过引用传递向量。

附注

我希望让程序接受客户名称,然后调用vector元素。

一个例子是代替stuff [0] .customerZipCode,它可能是东西[Janet] .customerZipCode,我或者能够编辑它或查看信息。我不知道如何做到这一点,但这只是一个侧面说明,知道该怎么做会很酷。

    #include <iostream>
#include <vector>
#include <string>
using namespace std;

struct CustomerAccount{
        string customerName;
        string customerAddress;
        string customerCity;
        string customerState;
        int customerZipCode;
        string customerTelephone;
        double customerAccountBalance;
        string customerDateOfLastPayment;
};

void newCustomerAccount(vector<CustomerAccount> &custAcct){
    string newCustomerName, newCustomerAddress, newCustomerCity, newCustomerState, newCustomerTelephone, newCustomerDateOfLastPayment;
    int newCustomerZipCode;
    custAcct.push_back(CustomerAccount());
    double newCustomerAccountBalance;
    int id = custAcct.size();

    cout << endl;
    cout << "Customer Name: ";
    cin >> newCustomerName;
    custAcct.customerName = newCustomerName;
    cout << "Test" << endl;
    cout << endl;


};


void customerMenu(vector<CustomerAccount> &custAcct){
    int customerChoice;

    cout << "=======MENU=======" << endl;
    cout << "1. Enter new account information" << endl;
    cout << "2. Change account information" << endl;
    cout << "3. Display all account information" << endl;
    cout << "4. Exit the program " << endl;
    cout << "Make a selection" << endl;
    cin >> customerChoice;

    switch(customerChoice){
    case(1):
        //"Enter new account information
        cout << "You have chosen to Enter new account information" << endl;
        newCustomerAccount(custAcct);

        break;
    case(2):
        //Change account information
        cout << "You have chosen to Change account information" << endl;

        break;
    case(3):
        //Display all account information
        cout << "You have chosen to display all account information" <<   endl;

        break;
    case(4):
        //Exit the program
        cout << "You have chosen to Exit the program" << endl;
        cout << "Bye!" << endl;
        cout << "The Size of the Array is: " << custAcct.size() << endl;
        break; 
    default:
        cout << "You did not make a valid selection" << endl;
        customerMenu(custAcct);
        break;
    };

};



int main()
{
    vector<CustomerAccount> custAcct;
    customerMenu(custAcct);
   return 0;
}

1 个答案:

答案 0 :(得分:4)

您已经声明testFunction通过引用获取向量。您只需将主调用更改为

即可
testFunction(stuff);

对于您的旁注,您可以使用地图而不是矢量。

#include <map>
int main(){
  std::map<std::string, Customer> m;

  m["Janet"].name = "Janet"
  // set the rest

}