Пытался создать функцию binarySearch для поиска объекта по полю, но не получается. Каким образом я могу реализовать поиск? (C++)

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <stdlib.h>
using namespace std;

struct CheckNumber
{
    string Number;
    string FIO;
    string Date;
};

void lineReading(string input, CheckNumber checkNumber[], int number) {
    vector<string> text;
    string buffer = "";
    for (int i = 0; i < input.length(); i++) {
        if ((input[i] != ',') and (i != input.length() - 1)) {
            buffer += input[i];
        }
        else if (i != input.length() - 1) {
            text.push_back(buffer);
            buffer = "";
        }
        else {
            buffer += input[i];
            text.push_back(buffer);
            buffer = "";
        }
    }
    checkNumber[number] = { text[0],text[1],text[2]};
}

void lineWriting(CheckNumber input) {
    cout << input.Number << ", " << input.FIO << ", " << input.Date << endl;
}

void sortFile(CheckNumber checkNumber[], int y) {
    for (int i = 0; i < y - 1; ++i)
    {
        for (int j = 0; j < y - i - 1; ++j)
        {
            CheckNumber c1 = checkNumber[j + 1];
            CheckNumber c2 = checkNumber[j];
            if (c1.FIO < c2.FIO)
            {
                CheckNumber c = checkNumber[j + 1];
                checkNumber[j + 1] = checkNumber[j];
                checkNumber[j] = c;
            }
        }
    }
}

void binarySearch(CheckNumber checkNumber[], int y) {
    string key;
    string receiptNumberArr[50];
    for (int i = 0; i < y; i++) {
        CheckNumber c = checkNumber[i];
        receiptNumberArr[i] = c.FIO;
    }
    cout << endl << "Введите фамилию, которую хотите найти: ";
    cin >> key;
    bool flag = false;
    int l = 0;
    int r = y - 1;
    int mid;
    while ((l <= r) && (flag != true)) {
        mid = (l + r) / 2;
        if (receiptNumberArr[mid] == key)
            flag = true;
        if (receiptNumberArr[mid] > key)
            r = mid - 1;
        else l = mid + 1;
    }
    if (flag) {
        cout << "Абонент с фамилией " << key << " следующий: " << endl; lineWriting(checkNumber[mid]); cout << endl;
    }
    else {
        cout << "Извините, но такого чека не существует."; cout << endl;
    }
}

void writingBinaryFile(CheckNumber checkNumber[], int y) {
    ofstream fout;
    fout.open("checknumber.bin", ios_base::out | ios_base::trunc | ios_base::binary);
    for (int i = 0; i < y; i++) {
        CheckNumber c = checkNumber[i];
        string cString = c.Number + "," + c.FIO + "," + c.Date;
        if (i != y - 1)
            cString += "\n";
        for (int j = 0; j < cString.length(); j++)
        {
            char symbol = cString[j];
            fout.write((char*)& symbol, sizeof(symbol));
        }
    }
    fout.close();
    cout << "Нужный файл создан." << endl << endl;
}

void readingBinaryFile(int y) {
    cout << "Считываем бинарный файл:\n\n";
    ifstream fout;
    fout.open("checknumber.bin", ios_base::in | ios_base::binary);
    char ch;
    while (fout) {
        char symbol = ' ';
        fout.read((char*)& symbol, sizeof(symbol));
        cout << symbol;
    }
}

int main()
{
    try {
        setlocale(LC_ALL, "rus");
        cout << "Начилось считывание файла.\n";
        CheckNumber checkNumber[50];
        string input;
        ifstream fin("checknumber.txt");
        int n = 0, m = 0;
        int y = 0;
        while (getline(fin, input)) {
            lineReading(input, checkNumber, y);
            y++;
        }
        cout << "После считывания файла получилось:\n";
        for (int i = 0; i < y; i++) {
            lineWriting(checkNumber[i]);
        }
        sortFile(checkNumber, y);
        cout << "\nПосле сортировки получилось:\n";
        for (int i = 0; i < y; i++) {
            lineWriting(checkNumber[i]);
        }
        binarySearch(checkNumber, y);
        writingBinaryFile(checkNumber, y);
        readingBinaryFile(y);
        cout << endl;
        fin.close();
    }
    catch (const char* exception)
    {
        cout << exception;
    }
}

Ответы (0 шт):