int size;
int input;
int x, i, n;
int searchNumber;
vector < int > numArray;
void selectionSort(vector<int>&, int);
void binarySearch(vector<int>&, int);
//int* numArray;
void generateArray()
{
cout << "Please enter the amount of random numbers to be generated: ";
cin >> input;
cout << "Please search for a value within the array: ";
cin >> userValue;
clock_t startTime = clock();
for(int i =0; i<100000;i++)
{
binarySearch(numArray, size, userValue);
}
int result = binarySearch(numArray, size, userValue);
if(userValue == numArray[result])
{
cout << "The number " << userValue << " was found in the array" << endl;
}
else
{
cout << "The number " << userValue << " was not found. " << endl;
}
So how would i reconstruct my for loops so i can access space outside my vector
Do you know what you're asking? You're asking to enter space not owned by your program (or at least owned by the vector) which is undefined behavior. At the best you'll get told to stop by your compiler. At the worst, you'll stomp on whatever data happens to be lying there and that could break anything.
numArray isn't initialized at that point. Putting everything in global space is killing you here. You have a numArray declared globally, then in generateArray() you declare another and initialize. I don't even think this should compile, but whatever. After generateArray() exits, you lose that variable. Selection sort only knows the one globally declared, it knows nothing about the one that existed in generateArray(). So when you try to access it numArray[i], it breaks because it's not even initialized.
As an aside, I got this warning when compiling under g++, with the flags - Wall - Wextra -pedantic
main.cpp: In function ‘int binarySearch(std::vector<int>, int, int)’:
main.cpp:89:1: warning: control reaches end of non-void function [-Wreturn-type]
This isn't related to your current problem, but there you go.
With debugging, the big thing is the values of the variables. For example can you see the values in the vector, and are the other values valid? One or more must be wrong for it to crash.
ResidentBiscuit is right, global variables are bad news, and could yet be causing your problem. I notice you don't use references consistently.