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.
// 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();