2016-11-05 5 views
-1

私はこのような質問がたくさんあると言うことから始めたいと思いますが、私の状況を扱わない限り説明を理解するのは難しいです。構造体を作成し、構造体を参照してベクトルを渡します

ここです:私が取り組んでいるプログラムの完全な質問です。

11.7:お客様は は、顧客アカウントに関する次のデータを格納するための構造を使用するプログラムを書くアカウント:

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

プログラムは、少なくとも20の構造体の配列を使用する必要があります。ユーザーは配列にデータを入力し、要素の内容を変更し、配列に格納されているすべてのデータを表示する必要があります。プログラムには、メニュー駆動のユーザーインターフェイスが必要です。

完全なコードを貼り付けすぎると、各行の先頭に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:関数を使用してこれらの要素のデータを変更します。したがって、参照によってベクトルを渡す必要があります。

サイドは、私はプログラムが顧客名に取り、その後、ベクトル要素がその呼び出される必要があり持っているのが大好きだ

ノート。

例は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; 
} 
+0

このプログラムの例がありますので、これを行う方法を知っている人が私を助けることができます。 – Juscallmesteve

+0

コンパイルされず、 'testFunction'が正しく呼び出されていません。 'testFunction(stuff);' – UnholySheep

+0

'testFunction(vector &stuff);は、testFunctionの再宣言です。 –

答えて

4

既に参照によってベクトルを取得するようにtestFunctionを宣言しています。メインの通話を変更する必要があるのは

testFunction(stuff); 

あなたのサイドノートでは、ベクターを使用せずに地図を使用することができます。

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

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

} 
+0

それは実際に参照渡しますか?もしそうなら、あなたは私にそれがどういうことを説明することができますか(スマートなお尻ではない、私は将来私が知るように頼んでいます)。私の理解はあなたが参照によって何かを渡すために "&"を使用する必要がある、ベクトルは特殊なケースですか? – Juscallmesteve

+0

void func(ベクトル); //コピーで渡します;; void func(ベクトル&); //参照渡し。 – mkmostafa

+0

「サイドノート」ヘルプありがとうございます。 – Juscallmesteve

関連する問題