Why is my search not finding the word?

Okay this is the example. It's with integers
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
26
27
28
unsigned int seed;					//a random seed
	int num;							//to hold an int value
	arrayListType<int> list(100);		//create a list of int type
	
	cout<<"Enter a random seed => ";	//get the random seed
	cin >> seed; 
	srand(seed);						//set the randon seed

	for (int i=0; i < 10; i++)			//insert 50 random numbers into the list
	{
		num = rand(); 
		if (i%2==0)
			list.insertFirst(num);
		else 
			list.insertLast(num); 
	}
	list.print(); 

	do
	{
		cout<<"Enter a number to search or stop when -99 is entered => "; 
		cin >> num; 
		if (list.search(num))
			cout << "found " <<num << " in the list. " <<endl; 
		else
			cout << "failed to find " <<num << " in the list. " <<endl; 

	}while (num!=-99); 


Enter a random seed => 12
11981  12947  1558  6232  77
5628  29052  26150  29926  22371
Enter a number to search or stop when -99 is entered => 77
found 77 in the list.
Enter a number to search or stop when -99 is entered =>



but when I do it with strings

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
26
27
28
29
30
31
32
arrayListType<string> list(100);		
	
	srand((unsigned int)time(0));
	string word;
	int len;

	for (int i=0; i < 5; i++)			
	{
		word = " ";
		len = 3 + rand()%10;
		for(int j = 0; j < len; j++)
		{
			word = word + (char)('A' + rand()%26);
	
		}
		if (i%2==0)
			list.insertFirst(word);
		else 
			list.insertLast(word);
	}
	list.print(); 

	do
	{
		cout<<"Enter a word to search or stop when No is entered => "; 
		cin >> word; 
		if (list.search(word))
			cout << "found " <<word << " in the list. " <<endl; 
		else
			cout << "failed to find " <<word << " in the list. " <<endl; 

	}while (word!= "No");



 JXB   BXYZRKJCR   XDDNP   AZZ   ALHVOCK
Enter a word to search or stop when No is entered => JXB
failed to find JXB in the list.
Enter a word to search or stop when No is entered =>


it's not found.
any ideas why not? :o
I have an idea. Maybe your definition for arrayListType<string>::search(string) is incorrect - could we see that please?
Last edited on
this is the search function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class Type> 
bool arrayListType<Type>::search(const Type & searchItem)
{			
	bool found = false;			//flag for founding status
	
	int i = 0;					//point to first
	while (!found && i < count)
	{
		if (list[i] == searchItem)
			found = true;		//find the searchItem
		else					//otherwise move to the next node
			i++; 
	}
	return found; 
}
Oh. Well apparently it's because my declaration of word

I used
word = " "

but when I change it to just
word = ""
it finds it.
Not sure why that is..
When you use "cin >> word" it skips all leading whitespace.
Topic archived. No new replies allowed.