Juscallmesteve Juscallmesteve - 5 months ago 74
C++ Question

Creating a Struct then passing a vector with those struct by reference

I would like to start off by saying there are many questions like this asked but for me it is quit hard to understand the explanations unless it deals with my situation.

HERE: is the Full Questions for the program I am working on.

11.7: Customer Accounts
Write a program that uses a structure to store the following data about a customer account:

Customer name
Customer address
ZIP code
Account balance
Date of last payment

The program should use an array of at least 20 structures. It should let the user enter data into the array , change the contents of any element , and display all the data stored in the array . The program should have a menu-driven user interface.

Here is some example code because pasting my full code was too much b/c I had to go through and add 4 spaces at the 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[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: Create a Vector.

2: Each element in that vector has the Structure and the associated data.

3: Modify the data in those elements by using functions, thus I need to pass the vector by reference.

Side Notes

I would love to have the program take in the customers name and then have the vector element be called that.

An example would be instead of stuff[0].customerZipCode, it could be stuff[Janet].customerZipCode and I either be able to edit it or see the information. I have not a clue how to do that but that is just a side note that would be cool to know how to do.


You are already declaring testFunction to take the vector by reference. You need only to change the call in main to


For your side note you can use a map not a vector.

#include <map>
int main(){
  std::map<std::string, Customer> m;
  Customer c;
  c.name = "Janet"
  // set the rest
  m["Janet"] = c