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.
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 ?
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?
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;
};
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.
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?