Comparing Strings Problem

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
Susan,
I do believe that a string, a regular string such as an user entry string or , say, a string from a file is NULL terminated - meaning, as I suspect you know already, that it contains the char "/0"
- Whereas, the string inside of an array is not so terminated.

I "suspect" that this is why those two "Identical" strings are not matching.

If you can "Look" at both strings in another form, say, HEX , then I think you'll see what the difference is.

A good HEX editor is useful at this time, but so is simply printing the two strings on the screen in HEX format.
Something like this:

string s_my_str = "abcde";
string a_names[] = "abcde";


print( " String contains: #5X Array Contains: #5X" , s_my_str, a_names );

( or maybe 6 chars, 5 or 6, try both)

This statement will print on the screen the value of both items in HEX format and you ought to be able to see the difference.

I'm expecting that the string from the array will be exactly 5 chars
and the string from the "String" will will be 5+1 chars.
With the extra char being a control (invisible ) char.



Thanks very much for your help. I appreciate it.

I looked into it more, and it turned out that the comparison was working and the deletion was happening, but I had an error message that would print regardless of whether the deletion happened or not.

So it was a stupid error on my part. D'oh!!

I will keep your advice in mind for the future, though. I didn't know about HEX editors and checking out the strings that way.

Thanks again for your help. I really appreciate it.

Susan
Topic archived. No new replies allowed.