So its taking in all the information but I think I might be passing it wrong. As well I think something is wrong with my if else tree but I am not sure what right now. Here is the code:
//This program performs a binary search on a string array whose elements are in
//ascending order.
//Includes
#include<iostream>
//function declarations
int SearchStrings(const std::string IDstring[], const int NumOfString, const std::string empID);
int main(){
//var declaration and array declaration
const int NumOfStrings=20;
std::string IDstring[NumOfStrings]={
"eight","eighteen","eleven","fifteen","five","four",
"fourteen","nine","nineteen",
"one","seven","seventeen","six","sixteen","ten",
"three","thirteen","twelve","twenty","two",};
std::string empID;
int results;
// start of outside input
std::cout<<"Enter the Employee name that you wish to search for: ";
std::cin>> empID;
//function call
results=SearchStrings(IDstring, NumOfStrings,empID);
//the guts of the program
if (results== -1){
std::cout<<"That number does not exist in the system."<<std::endl;
}else{
std::cout<<"ID "<<empID<<" was found in element "<<results<<" of the array"<<std::endl;
}
/*for(int i=0; i<20; i++){
std::cout<<IDstring[i]<<std::endl;
}
*/
return 0;
}
//the binary search function
int SearchStrings(const std::string IDstring[], const int NumOfStrings, const std::string empID){
//variable declaration and initialization
int first=0;
int last=NumOfStrings-1;
int middle;
int position;
bool found=false;
//while not found and first is less than last
The function SearchStrings() has a number of issues.
The == operator is used incorrectly, when the intention was to assign a value. Should be =. Only use == when testing for equality.
Calculation of new value of first and last is wrong, so the loop condition first<=last will always be true, thus the loop will never terminate.
break was added where it should not be needed. You might justify the use of break when the value is found, but the loop would terminate in any case because the found flag is set to true. Thus there is no need for break at all.
position is not initialised, so a garbage value will be returned if the item is not found.