recursive binary search problems

hey, Im new to the forum. I have a program holding a vector of structs and i am trying to search for a certain element of the vector based off a member of the struct. but whenever i call the binary search function it gives me a segmentation fault. it compiles and runs, but just stops at the seg fault.

here is the code


/*
* prog4.cpp
*
*
*
*/

#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

struct node
{
int ID;
string name;
string address;
int gpa;
int *scores;
int avg;
};

int RecBinarySearch(vector<node> vec, int first, int last, int key)
{

int mid = ( first + last ) / 2;

// If the midpoint value is less than what you're looking for,
// then you need to look in the right half.
if (vec[mid].ID == key)
return mid+1;

else if( vec[mid].ID < key )
return RecBinarySearch( vec, mid + 1, last, key );

// If the midpoint value is greater than what you're looking for,
// then you need to look in the left half.
else if( vec[mid].ID > key )
return RecBinarySearch( vec, first, mid - 1, key );

// If it's not less than and not greater than, it must be equal to
else
return -1; // found it
}

/*int RecBinarySearch(vector<node> vec, int first, int last, int key)
{
if(first > last)
{
cout<<"\n SEARCH NOT POSSIBLE";
return -1;
}
if (first <= last)
{
int mid = (first + last) / 2;
if (vec[mid].ID == key)
return mid+1;
if (vec[mid].ID > key)
RecBinarySearch(vec,first,mid-1,key);
else
RecBinarySearch(vec,mid+1,last,key);
}

}*/

int main()
{
int numrec = 0;
int numtest;
ifstream fin;
int tempID;
char tempname[50];
char tempaddress[50];
float tempgpa;
int *tempscores;
int tempavg;
string traverse;
char choice;
int searchID;
int ri;
int i, j;

vector < node > records;

records.push_back(node());

fin.open("input_file.txt");

if(fin.fail())
{
cout << "Unable to Open File.";
return -1;
}

while (getline(fin, traverse))
{
numrec++;
}

fin.close();

fin.open("input_file.txt");

if(fin.fail())
{
cout << "Unable to Open File.";
return -1;
}

cout << "There are " << numrec << " Student Records in the File\n";
cout << "How many Tests are there for each Student? ";
cin >> numtest;

for (i = 0; i < numrec; i++)
{
records.push_back(node());
fin >> tempID;
fin.ignore();
fin.get(tempname, 50, ',');
fin.ignore();
fin.get(tempaddress, 50, ',');
fin.ignore();
fin >> tempgpa;
fin.ignore();
fin >> tempavg;
fin.ignore();

records[i].ID = tempID;
records[i].name = tempname;
records[i].address = tempaddress;
records[i].gpa = tempgpa;
records[i].avg = tempavg;
records[i].scores = new int[numtest];


}

cout << "\nNow Displaying Student Records in the Vector\n\n";

for (j = 0; j < numrec; j++)
{
if(j < 9)
{
cout << "Student " << j+1 << ": " << records[j].name << endl;
cout << "ID #: " << records[j].ID << endl;
cout << "Address: " << records[j].address << endl;
cout << "Avg Test Score: " << records[j].avg << endl;
cout << endl;
}
else
{
cout << "Student " << j+1 << ": " << records[j].name << endl;
cout << "ID #: " << records[j].ID << endl;
cout << "Address: " << records[j].address << endl;
cout << "Avg Test Score: " << records[j].avg << endl;
cout << endl;
}

}

choice = 'y';

while (choice != 'n' && choice != 'N')
{
cout << "Would You Like to Search for a Student? (y/n) ";
cin >> choice;

while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N')
{
cout << "input must be y or n\n";
cout << "Try Again? ";
cin >> choice;
}

if (choice != 'n' && choice != 'N')
{
cout << "Enter ID of Student to Search for: ";
cin >> searchID;

//segmentation fault here
ri = RecBinarySearch(records, records[0].ID, records[numrec-1].ID, searchID);
ri = 13;
//cout << ri << endl << records[0].ID << endl
// << records[numrec-1].ID << endl << searchID << endl;

if(ri < 9)
{
cout << "Student " << ri << ": " << records[ri].name << endl;
cout << "ID #: " << records[ri].ID << endl;
cout << "Address: " << records[ri].address << endl;
cout << "Avg Test Score: " << records[ri].avg << endl;
cout << endl;
}
else
{
cout << "Student " << ri << ": " << records[ri].name << endl;
cout << "ID #: " << records[ri].ID << endl;
cout << "Address: " << records[ri].address << endl;
cout << "Avg Test Score: " << records[ri].avg << endl;
cout << endl;
}
}

}

return 0;
}


and here is the input file called input_file.txt

114244,Derek Bush,5227 Bandera Rd,3.95,93
135418,Andrew Cleveland,239 Hickory St,2.43,82
176100,Andy Zier,143 Quay Rd,4.00,62
205118,Jimmy Dante,744 Mill St,1.80,77
217138,Shelby Rollings,5833 Vista Ln,3.80,94
225989,Joe Britz,1547 Hollow Dr,2.00,98
277421,Samantha Mason,6138 Highland Dr,2.00,72
295038,Rachel Wolfe,4325 Acres St,3.15,81
346446,Jeremy Stover,319 Corner St,3.00,82
455100,Zach Quaranto,9958 Orchard Rd,2.45,62
487327,Kelsi Schneider,7374 Cove Ln,4.00,58
590078,Connor Hennessy,4073 Hills St,2.00,69
715981,Justin Bowling,4835 Ridge Dr,3.00,96
730407,Tammy Rae,2552 Summit Ave,3.80,93
735638,Jessica Smith,4075 Manor Rd,3.00,49
756053,Jonnie Vue,5272 Flowers St,4.00,64
801805,Erica Leitner,2247 Pike St,4.00,83
856789,Allister Lang,5246 Crest Dr,2.50,93
947467,Sam Vega,702 Bend Rd,3.00,78
979912,Jenn Radford,1725 Court St,1.75,50

a fresh set of eyes would be awesome. thanks
Topic archived. No new replies allowed.