String comparison loop

Hi all,

I am creating a banking application and after working on it pretty much solid for 2 weeks, I am close to completion. I thought creating my menu driver would be the easy part but how wrong could I be?

Anyway...

I have a vector of customers and 1 of my functions searches for a customer by name (user enters the name). It does the string comparison fine although it always enters the "not found" if statement and I can't for the life of me see why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout<<"Please enter customers name: ";
string name;
getline(cin,name);
for(int i=0;i<(int)Customers.capacity();i++)
{
          if (Customers[i].getName()==name)
	 {
	         cout<<"Customer found!";
		 cout<<"\n--Customer--\n"<<Customers[i].getName();
		 cID=i;
	}
	else if (Customers[i].getName()!=name)
	{
		cout<<"Not found!";
	}
}


Would output...
Pleas enter customers name: 2
Not found!Customer found!
--Customer--
2


Any help is much appreciated.

Thanks

Matt
Last edited on
although it always enters the "not found" if statement

?! The output clearly shows that it doesn't.

Also, why are you testing the condition again in else if (Customers[i].getName()!=name) when you already know the result?
It does as it results "Not found" before it results "Customer found"

It was initially an else statement, it was a matter of despiration when I changed it to an else if
Last edited on
That's not "always". Or are you saying the vector contains only one element?
It did only have 1 element in the previous example I gave, although if it contains more than 1 element, it would output "Not found!Not found!Customer found!"(if it contained 2 elements)
Last edited on
Ah. I only just noticed this:
i<(int)Customers.capacity()

capacity() does not give you the number of elements in the vector, size() does.
See http://www.cplusplus.com/reference/stl/vector/capacity/
Actually I see what the problem is now, it's always going to output "not found" if it doesn't match
Thanks alot, it's working now

Been working on it for like 8 hours straight now, just want it finished before I go to sleep.

Recently I've been struggling to sleep because I've been coding for hours late at night and then going to bed, and then I'll wake up in the morning and instantly code pops to mind, sad I know, lol.
Topic archived. No new replies allowed.