c++ - Creating a Struct then passing a vector with those struct by reference -
i start off saying there many questions asked me quit hard understand explanations unless deals situation.
here: full questions program working on.
11.7: customer accounts write program uses structure store following data customer account:
customer name customer address city state zip code telephone account balance date of last payment
the program should use array of @ least 20 structures. should let user enter data array , change contents of element , , display data stored in array . program should have menu-driven user interface.
here example code because pasting full code b/c had go through , add 4 spaces @ beginning of each line.
#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 mkmostafa) testfunction(stuff); //the correct way cout << stuff[0].customername << endl; };
basically
1: create vector.
2: each element in vector has structure , associated data.
3: modify data in elements using functions, need pass vector reference.
side notes
i love have program take in customers name , have vector element called that.
an example instead of stuff[0].customerzipcode, stuff[janet].customerzipcode , either able edit or see information. have not clue how side note cool know how do.
#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 account information" << endl; cout << "4. exit program " << endl; cout << "make selection" << endl; cin >> customerchoice; switch(customerchoice){ case(1): //"enter new account information cout << "you have chosen enter new account information" << endl; newcustomeraccount(custacct); break; case(2): //change account information cout << "you have chosen change account information" << endl; break; case(3): //display account information cout << "you have chosen display account information" << endl; break; case(4): //exit program cout << "you have chosen exit program" << endl; cout << "bye!" << endl; cout << "the size of array is: " << custacct.size() << endl; break; default: cout << "you did not make valid selection" << endl; customermenu(custacct); break; }; }; int main() { vector<customeraccount> custacct; customermenu(custacct); return 0; }
you declaring testfunction take vector reference. need change call in main
testfunction(stuff);
for side note can use map not vector.
#include <map> int main(){ std::map<std::string, customer> m; m["janet"].name = "janet" // set rest }
Comments
Post a Comment