Search key problem

first of all hi,

im trying to make a "search key" of some sort (- Linear search), but when i want to output the array number, in which the number user typed is, it always outpust 0. Solution? ty.


heres the code:

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
#include <iostream>

using namespace std;

int main()
{
	int Array[17] = {7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5, 42, 39, 8};

	int n;

	cout << "List = 7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5, 42, 39, 8" << endl;
	cout << "Enter an integer in the list to search for: ";
	cin >> n;

    int pos;

	for(int i=0; i < 17; ++i)
	{
		if( Array[i] = n )
		{
		    pos = i;
		    break;
		}
		else
			continue;
	}
	
	cout << "Item found at index [" << pos << "]" << endl;
}

Last edited on
Line 19: if( Array[i] = n )
You should have == not =
The == operator check for equallity, the = operator makes the equality.

Also you don't need the else continue;(at least if you don't have any more code)
Hope this helps
ty :)
Topic archived. No new replies allowed.