C++ 二进制搜索,为什么 cod 不正确 [关闭]

问题描述 投票:0回答:0

Некорректно работает в месте чтения файла для смены зарплаты (40 строка)。 Вконсольневыводитьcясообщение“输入新工资”。 Чат гпт не помогает :)

enter image description here

enter image description here

Буду признателен за помощь.

#include <iostream>
#include <fstream>
using namespace std;

struct Employee{
    string surname, post;
    int age;
    double salary;
};

void input(Employee arr[], int amount);
void fwrit(Employee arr[], int amount);

int main(){
    int n, num;
    cout<<"Enter the number of employees: ";
    cin>>n;
    
    Employee emps[10];
    cout<<"Enter employees"<<endl;
    input(emps, n);
    cout<<"Saving data to file..."<<endl;
    fwrit(emps, n);
    
    cout<<"if you want to change the employee's salary enter 1: ";
    cin>>num;
    
    if(num==1){
        fstream file("file.txt", ios::in | ios::out | ios::binary);
        if(!file.is_open()){
            cout<<"File is`t open!!! Error!!"<<endl;
            exit(1);
        }
        string name;
        cout<<"\tEnter surname: ";
        cin>>name;
        
        Employee emp;
        bool found = false;
        while(file.read((char*)&emp, sizeof(Employee))){
            if(emp.surname==name){
                cout<<"Enter new salary: ";
                cin>>emp.salary;
                found = true;
                file.seekg(-sizeof(Employee), ios::cur);
                file.write((char*)&emp, sizeof(Employee));
                cout<<"Data saved\n";
                break;
            }
        }
        if(!found){
            cout<<"Surname not found!!!"<<endl;
        }
        file.close();
    }
    
    return 0;
}
c++ binaryfiles
© www.soinside.com 2019 - 2024. All rights reserved.