Linear search function

Consider a program that is supposed to have an array holding few integers. The program asks the user to enter a number. The program is supposed to go through the array and look for the number entered by the user. If its not in the array, use a cout statement to say that the number is invalid. And vice versa. I could use a general outline.
Try using a for loop to loop over the array and compare each element with the number the user entered.
Make sure you do some reasonable error checking while receiving the input. Here is an example on that.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3

Instead of writing your own for loop take a look at this info on the std::find algorithm. You can easily use some parts of the example in order to build your program. The example does a good job of explaining the concept.
http://cplusplus.com/reference/algorithm/find/
Topic archived. No new replies allowed.