如何处理函数检索到的空字符串? - 解决了

时间:2019-05-12 09:39:04

标签: c++ arrays string binary-search selection-sort

我正在用以下问题陈述进行作业:

“编写一个程序以按字母顺序对名称进行排序和显示(使用选择排序)。该程序提示用户输入要搜索的名称(使用二进制搜索)。该程序还会对名字和姓氏进行更正如果输入不正确。找到名称后,程序将提示用户是否要再次搜索。”

我正在为程序循环而苦苦挣扎。我不确定问题出在哪里。但是我认为问题出在我在源代码中提到的几个地方。但问题出在哪里我可能是错的。

当我尝试运行程序时,我使用的在线编译器(docs)给我以下错误消息:

抛出'std :: out_of_range'实例后调用

terminate   what():basic_string :: at:__n(0)> = this-> size()(0) 中止(核心已弃用)

我不确定这意味着什么,但是我有前面提到的想法。 源代码:

#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;
 }

理想情况下,我想知道问题出在哪里以及如何解决,而希望不必重做整个事情。谢谢您的宝贵时间。

0 个答案:

没有答案