Vector user-defined search function!

Hi I've been trying to figure this out for hours now and I can't get it so I thought I'd ask the genious' here for help. The assignment is to create three functions based on the main. I got them all except the last one, the search one. When i run the program I get "error vector subscript out of range" when it goes through the second cycle.

I checked the debugger and it seems it's trying to go to vector[3], which doesnt exist, and it understandably errors. I cant figure out why its going that far, since in my for loop it says until less than the size of the vector, which means 2.

I'm also NOT ALLOWED to change the main IN ANY WAY SHAPE OR FORM, only to implement functions that work accordingly. Thanks again.

Heres the code:
#include <iostream>
#include <vector>

using namespace std;

void processVector(vector<int>&);

void print(const vector<int>&);

bool search(const vector<int>&, int);
/***** DO NOT MODIFY THE MAIN FUNCTION *****/
int main()
{
vector<int> intList;

/**** FUNCTION processVector() ****/
//Uncomment the following statement
// AFTER implementing processVector()

processVector(intList);

//OUTPUT (as the user, enter: 1 2 3 -1)
/*
Enter a positive number (to quit enter -1): 1
Enter a positive number (to quit enter -1): 2
Enter a positive number (to quit enter -1): 3
Enter a positive number (to quit enter -1): -1
*/


/*************************************************************/


/**** FUNCTION print() ****/
//Uncomment the following two statements
// AFTER implementing processVector()

cout << "\nList contains: ";
print(intList);

//OUTPUT
/*
List contains: 1 2 3
*/


/*************************************************************/


//**** (1) FUNCTION search() ****/
//Uncomment the following block
// AFTER implementing search()


if (search(intList, intList.front()))
cout << "\n\nNumber (" << intList.front()<< ") was found.";
else
cout << "\n\nNumber (" << intList.front() << ") was not found.";
if (search(intList, intList.back()))
cout << "\nNumber (" << intList.back()<< ") was found.";
else
cout << "\nNumber (" << intList.back() << ") was not found.";
if (search(intList, -100))
cout << "\nNumber (-100) was found.";
else
cout << "\nNumber (-100) was not found.";

if (search(intList, -1))
cout << "\nYour vector should not contain (-1).";
else
cout << "\nNumber (-1) was not found.\n";


//OUTPUT
/*
Number (1) was found.
Number (3) was found.
Number (-100) was not found.
Number (-1) was not found.
*/


/*************************************************************/

cout << endl;
system("Pause");
return 0;
}

void processVector(vector<int>& myVector)
{
int answer;
do{
cout << "Enter a positive number (to quit enter -1): ";
cin >> answer;
if(answer != -1)
myVector.push_back(answer);
}while(answer != -1);
}

void print(const vector<int>& myVector)
{
for(int count = 0; count < myVector.size(); count++)
cout << myVector[count] << " ";
}

bool search(const vector<int>& myVector, int number)
{
for(int count = 0; count < myVector.size(); count++)
{
if(myVector[count] == number)
return true;
}
}


Works for me.
search() does not return anything in particular if the loop does not return true.
Topic archived. No new replies allowed.