I'm doing a homework assignment with the following problem statement:

"Write a program to sort and display the names in alphabet order (use selection sort). The program prompts the user to enter the name being search (use binary search). The program also makes a correction to first and last name if inputted incorrectly. Once the name is found the program prompts if the user would like to search again."

I am struggling on the looping of my program. I am not sure where the problem is. However I think the problems lies in the few spots I've mentioned in the source code. I could be wrong about where the problem lies though.

The online compiler I used (https://www.onlinegdb.com/online_c++_compiler) was giving me the following error message when I tried to run the program:

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 0) >= this->size() (which is 0) Aborted (core dumped)

I am not sure what this means, but I have ideas as mentioned before. Source Code:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;


//Given Function Prototypes
void displayNames(const string str[], int numOfNames); 
void selectionSort(string str[], int numOfNames); 
string upperCaseIt(string str); 
bool binarySearch(const string str[], int size, string searchString); 

const int numOfNames = 20;

int main()
{
char redo;

//Given array of 20 names
string allNames[numOfNames] = {"Collins, Bill", "Smith, Bart", 
"Michalski, Joe", "Griffin, Jim", 

                                "Sanchez, Manny", "Rubin, Sarah", 
"Taylor, Tyrone", "Johnson, Jill", 

                                "Allison, Jeff", "Moreno, Juan", "Wolfe, 
Bill", "Whitman, Jean", 

                                "Moretti, Bella", "Wu, Hong", "Patel, 
Renee", "Harrison, Rose", 

                                "Smith, Cathy", "Conroy, Pat", "Kelly, 
Sean", "Holland, Beth"}; 

selectionSort(allNames, numOfNames); //Sorts names

displayNames(allNames, numOfNames); //Displays names

do{ //Ideally need to loop this however does not work

    cout << "Type the name to search (Last name, first name):" << endl; 
    //Recieves a name to search from user
    string searchName;
    getline(cin, searchName); //Problem might be here?

    string fixedSearchName = upperCaseIt(searchName); //Fixes the name

    bool isInAllNames = binarySearch(allNames, numOfNames, 
  fixedSearchName); //Searches for name in array and returns true or 
  false

    if(isInAllNames == true){ 

        cout << fixedSearchName << " was found in the array." << endl << 
    endl; //Output if name is found
    }
    else if(isInAllNames == false){

        cout << fixedSearchName << " was NOT found in the array." << endl 
        << endl; //Output if names is NOT found

    }

    cout << "Another name search (Y/N): "; //Prompts user to search again 
    cin >> redo;

  } while(toupper(redo) == 'Y');

 return 0;
 }

void displayNames(const string str[], int numOfNames){

cout << "The names in sorted order are:" << endl << endl;

for(int i = 0; i < numOfNames; i++){

    cout << str[i] << endl;

}

cout << endl;

} 

void selectionSort(string str[], int numOfNames){

int startScan;
int minIndex;
string minValue;

for (startScan = 0; startScan < (numOfNames - 1); startScan++){

    minIndex = startScan;
    minValue = str[startScan];

    for(int i = startScan + 1; i < numOfNames; i++){

        if(str[i] < minValue){

            minValue = str[i];
            minIndex = i;
        }

    }

    str[minIndex] = str[startScan];
    str[startScan] = minValue;
     }
  }

 bool binarySearch(const string str[], int size, string searchString){ 
//This function uses binary search to find names 

 int first = 0;
 int last = size - 1;
 int middle = size - 1;
 int position = -1; //Problem might also might be here?
 bool found = false;

 while(!found && first <= last){

    middle = (first + last) / 2;

    if(str[middle] == searchString){

        found = true;
        position = middle;

    }
    else if(str[middle] > searchString){

        last = middle - 1;

    }
    else{

        first = middle + 1;
    }
 }

 return found;
 }

  string upperCaseIt(const string str){ //Problem might in this function?

  string fixedStr = str;

  if(islower(fixedStr[0])){

    fixedStr[0] = toupper(fixedStr[0]);

  }

  for (int i = 1; i < fixedStr.length(); i++){

    if (islower(fixedStr[i])){ 

            fixedStr[i] = fixedStr[i] - 32;

    }
    if(isupper(fixedStr[i])){

            fixedStr[i] = fixedStr[i] + 32;
      }
  }

   int lastNameLocation = fixedStr.find(" ", 0) + 1; //last name location 

 fixedStr[lastNameLocation] = toupper(fixedStr.at(lastNameLocation)); 
 //Converts first letter of last name to upper

 return fixedStr;
 }

Ideally, I'd like to know where the problems lies and how to fix it while hopefully not having to redo the whole thing. Thank you for your time.

0 Answers