Search an array for an element

I'm trying to prompt a user to enter a number 1 through 20 whose elements are all stored in an array. If they select a number 1 through 20, the program would let the user know. However, every time I try this, it just tells me what's already in the array with no regard to my input. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>  
using namespace std;     

 int main()  
 {  
   int search;
     int array[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};   
     cout<<"Please enter a number for me to search in the array." << endl;
		cin >> search;
	 
		 cout << "Enter your twenty numbers" << endl;
 for (int i = 0; i <= 19; i++) 
 { 
	if (search = array[i])
	cout << search << "is in your array" << endl;
	else if (search /= array[i])
	cout << search << "is in your array" << endl;
	
 } 

	 system ("pause");
}
Hi there,

Well first, you output the exact same message for both cases:

1
2
3
4
if (search = array[i])
	    cout << search << "is in your array" << endl;
else if (search /= array[i])
	    cout << search << "is in your array" << endl;


Second, you are using the assignment operator, rather than the equal operator. You are also using the divide/assign combined operator. I think what you intend is the following:

1
2
3
4
if (search == array[i])
	cout << search << "is in your array" << endl;
else if (search != array[i])
	cout << search << "is in not in your array" << endl;



You could simplify that to:

1
2
3
4
if (search == array[i])
	cout << search << "is in your array" << endl;
else
	cout << search << "is in not in your array" << endl;


Also, since your array is ordered, you could just use the std::binary_search algorithm:

1
2
3
4
5
6
7
8
#include <algorithm>

bool found = std::binary_search(array, array[19], search, [](int i, int j) { return i==j; });

if (found==true)
	cout << search << "is in your array" << endl;
else
	cout << search << "is in not in your array" << endl;



On a sidenote, please don't use system():
http://www.cplusplus.com/forum/beginner/1988/
http://www.cplusplus.com/forum/articles/11153/

Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Last edited on
First of all, you should notice :

if (search = array[i]) // one equal sign, an assignment

And :

if (search == array[i]) // double equal sign, a comparison

Also,
1
2
else if (search /= array[i])
cout << search << "is in your array" << endl;


What does this code do? Do you mean you want to inform that your input is not in your array?

If so, replace it with :
else cout << search << "is not in your array" << endl;
Last edited on
Thanks for your help, I appreciate it. When I tried using this code...

1
2
3
4
if (search == array[i])
	cout << search << "is in your array" << endl;
else
	cout << search << "is not in your array" << endl;


...it did work, but now it gives me 19 "is not in your array" and 1 "is in your array". How do I get it show it displays just 1 statement regardless of whether it's in or not.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (search == array[i]
{
	cout << search << " is in your array" << endl;
	break; //This will terminate the loop once the match is found
	       //And there will be no need to complete 19 iterations
}
else if (search != array[i] && i == 19)  
	cout << search << " is not in your array" << endl;
// i == 19 is to guarantee that at least 19 iterations has been
// performed to check at 20 values and no match was found.
// search != array[i] alone will print out the statement
//each time search != array[i] is true

Topic archived. No new replies allowed.