Banking Software

Hi,
I am having some trouble with a project I'm doing for school. I am trying to write some bank software. The part I am having trouble with is entering a new customer's information and saving it. I have a struct called customer with all of the info I need in it. I need to be able to save that to a file and add more later, or search for a customer by name or account number. My problem is, I don't know a good way to do this. We aren't allowed to use classes in this assignment, but we can use vectors. I have been trying an array of structs, but I don't know how to add a new struct to an existing array in the next available spot. I don't even know if this is a good way to do this. Any tips would be greatly appreciated.

Thanks,
Jeff
You can't use classes but you can use structs? You know they're the same thing in c++..
Anyway, if you can use vectors then have a vector of structs and just push_back stuff when you need to.
Okay thanks. I haven't seen any vector examples for anything other than int, so if my struct is called customer, then can I do something like:
vector<customer> customerInfo ?
yes
Awesome, thanks for your help!
Okay, I thought I had it, but I cant seem to populate(?) my vector. Every time I try to add a new struct, it seem to overwrite the other one. Any suggestions?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void addCustomer()
{
	//I saw this in an example and thought it was necessary, I don't know if it is.
        string tempFirstName;
	string tempLastName;
	int tempAccountNo;
	int tempPIN;
	float tempBalance;

	vector<Customer> customerList;
	
	cout << "New customer's first name: ";
	cin >> tempFirstName;
	cout << endl <<"Last name: ";
	cin >> tempLastName;
	cout << endl << "Enter the new customer's five digit account number: ";
	cin >> tempAccountNo;
	cout << endl << "Enter the customer's Personal ID Number: ";
	cin >> tempPIN;
	cout << endl << "Enter the customer's beginning balance: ";
	cin >> tempBalance;

	Customer a;
	a.accountNo = tempAccountNo;
	a.balance = tempBalance;
	a.firstName = tempFirstName;
	a.lastName = tempLastName;
	a.PIN = tempPIN;

	customerList.push_back(a);

	//I did this just to see if it was being overwritten
        cout << customerList[0].firstName;
	
}

My struct looks like this:

1
2
3
4
5
6
7
8
struct Customer
{
    std::string firstName;
    std::string lastName;
    int accountNo;
    float balance;
    int PIN;
};


Thanks again,
Jeff
there is nothing wrong with your code. after you push_back onto the vector you can update the data then push_back onto the vector again. refer them by index depending how many you have.

cout << customerList[0].firstName << endl;
cout << customerList[1].firstName << endl;

ect


i am guessing that you want to continue to enter data and load it into the vector so create a loop with an option to exit.
Last edited on
The only problem with the loop is I don't want to do it all at once. There are a lot of functions in this program , and I want to be able to go back to the menu after I've added a customer, do something else and then come back to add another. So, it sounds like the best thing to do would be to add a variable that is just a counter to keep track of how many customers I've added already, correct?

Thanks for all of your help,
Jeff
The problem with your code is that you keep recreating your vector (since it's a local variable)

It should be
1
2
3
4
5
6
7
8
9
10
11
12
void addCustomer(vector<Customer>& vec){
   Customer a;
   cin >> a.name;
   vec.push_back(a);
}
int main(){
   std::vector<Customer> vec;
   while(true){
      //some control should be added here..
      addCustomer(vec);
   }
}
Okay, that makes sense. Thanks again hamsterman!
Topic archived. No new replies allowed.