I am working on a project that has a variety of functions laid out to work with an array of structures.
My program is basically supposed to act as a handler of bank accounts--being able to load in customers from a file, create new customers, delete customers, change balances, etc. Our structure we are required to use has a string account number, a string name, and two floats for checking and saving balances.
I am having trouble with a specific function, trying to get strings to compare properly. The function, essentially, needs to take the user input string (an account to delete) and compare it to already existing accounts. If it finds a match, it deletes the account.
But my strings, even when they appear to be the same, are not. For most of my strings, strlen comes up with different numbers (one off) for the lengths of the two strings, even though the debugger shows them as identical. But it also doesn't work even for the one example I found where the strlen was the same number.
Any help would be appreciated to point me in the right direction. I've spent the last several hours scouring this site and others trying to find direction and I'm kind of sunk.
Here is what I have tried so far:
Version 1
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
|
int deleteCustomer(Account arr[], int numOfCust)
{
string accountNum;
bool value = false;
cout << "Please enter the account number you wish to delete: ";
cin >> accountNum;
for (int index = 0; index < numOfCust; index++)
{
if (accountNum == arr[index].accountNum)
{
arr[index] = arr[numOfCust - 1];
bool value = true;
numOfCust -= numOfCust;
}
}
if (value == false)
cout << "Account number <" << accountNum << "> does not exist.";
value = false;
cout << endl;
return numOfCust;
}
|
Version 2:
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
|
int deleteCustomer(Account arr[], int numOfCust)
{
string accountNum;
bool value = false;
cout << "Please enter the account number you wish to delete: ";
getline(cin, accountNum);
for (int index = 0; index < numOfCust; index++)
{
if (strcmp(accountNum.c_str(), arr[index].accountNum.c_str()) == 0)
{
arr[index] = arr[numOfCust - 1];
bool value = true;
numOfCust -= numOfCust;
}
}
if (value == false)
cout << "Account number <" << accountNum << "> does not exist.";
value = false;
cout << endl;
return numOfCust;
}
|
I'm not worried, at this point, if the part under the if statement in the for loop is correct. I can't get the program to even enter that if statement to run it.
Any help would be gratefully appreciated.
Susan