Sign in to follow this  
dcuk

String problems

Recommended Posts

Hiya all. Experiencing a few problems with my bank simulation program. Basically I have a manager, creates an array of Customers which each creates an array of Accounts. However only the array of Customers only really works and also a function such as the follow, called in Main, through the manager. Doesn't update anything, and if i used a cout to output 'fname' for example - i would get the right value. But when its out of the function, nothing has changed. At a loss please help.
void Customer::updatePersonDetails() {
	string t_fname; string t_lname; string t_phone;
	string t_dob; char t_sex;
	cout << "\nUpdate Customer Details\n~~~~~~~~~~~~~~~~~~~~~~~~\n";

	cout << "For fields you would like to leave unchanged - input '0'\n";

	cout << "First Name: " << fname << "\t\tNew First Name: ";
	cin >> t_fname;
	if(t_fname != "0") { fname = t_fname; }

	cout << "Surname: " << lname << "\t\t\tNew Surname: ";
	cin >> t_lname;
	if(t_lname != "0") lname = t_lname;

	cout << "Phone: " << phone << "\t\tNew Phone: ";
	cin >> t_phone;
	if(t_phone != "0") phone = t_phone;

	cout << "Sex: " << sex << "\t\t\t\tNew Sex: ";
	cin >> t_sex;
	if(t_sex != '0') sex = t_sex;

	cout << "Date Of Birth: " << fname << "\t\tNew Date Of Birth: ";
	cin >> t_dob;
	if(t_dob != "0") dob = t_dob;
	cout << "\n";
}
This is how its called in main.cpp.
	case 3:	
					cout << "Customer Account number: ";
					cin >> custAcc;
					if(mgr->search(custAcc) == -1) { cout << "Error: "; break; }
					else {  
						mgr->getCust(mgr->search(custAcc)).updatePersonDetails();
					}
					break;

Share this post


Link to post
Share on other sites
Does getCust() return a Customer by value? If so, you are calling updatePersonDetails() on a temporary object that disappears at the end of the line.

To fix this, you can have getCust() return a reference to Customer.

Note that, depending on how the manager stores it's data, hanging on to the reference might be dangerous. There are ways around this, but I'll stop here and give you a chance to respond.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Does getCust() return a Customer by value? If so, you are calling updatePersonDetails() on a temporary object that disappears at the end of the line.

To fix this, you can have getCust() return a reference to Customer.

Note that, depending on how the manager stores it's data, hanging on to the reference might be dangerous. There are ways around this, but I'll stop here and give you a chance to respond.


Thank you for your response. I believe this could under pin all of my problems.

Manager class holds its Customers like:



Customer getCust(int i) { return cust[i]; }
...
private:
Customer *cust;



I am not certain the changes that would need to be made. Is my call from main ok, as i can get into the function fine no compile/run time errors there.

Share this post


Link to post
Share on other sites
Yes it's what I said. The problem is that getCust() returns the customer by value. That means that it returns a copy of the object that's stored in the array. Anything you do to this copy will not affect the original object.

To fix this, you can return the customer by reference:

Customer & getCust(int i) { return cust[i]; }

However this could be dangerous and you should make sure not to hang on to the reference for long, as it might become invalid (for example, the manager might reallocate it's internal array). In your example code it should be fine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this