There is a code:

struct orders { string product; string mark; string surname; string name; string secondName; string phoneNumber; int cost; int dayOfRecept; int monthOfRecept; int yearOfRecept; int dayOfIssue; int monthOfIssue; int yearOfIssue; bool status; }; void showOredrsFile(orders Orders) { cout << endl; ifstream file("order.txt"); int i = 0; cout << "\t\tOrders" << endl; while (file >> Orders.product >> Orders.mark >> Orders.surname >> Orders.name >> Orders.secondName >> Orders.phoneNumber >> Orders.cost >> Orders.dayOfRecept >> Orders.monthOfRecept >> Orders.yearOfRecept >> Orders.dayOfIssue >> Orders.monthOfIssue >> Orders.yearOfIssue >> Orders.status) { cout << i << " " << Orders.product << " " << Orders.mark << " " << Orders.surname << " " << Orders.name << " " << Orders.secondName << " " << Orders.phoneNumber << " " << Orders.cost << " " << Orders.dayOfRecept << " " << Orders.monthOfRecept << " " << Orders.yearOfRecept << " " << Orders.dayOfIssue << " " << Orders.monthOfIssue << " " << Orders.yearOfIssue << " " << Orders.status << endl; i++; } file.close(); if (i == 0) { cout << "There're no orders now!" << endl; } cout << endl; } void addOrder(orders Orders) { ofstream file("order.txt", ios_base::app); cout << "Enter the product" << endl; cin >> Orders.product; cout << "Enter the mark" << endl; cin >> Orders.mark; cout << "Enter the surname" << endl; cin >> Orders.surname; cout << "Enter the name" << endl; cin >> Orders.name; cout << "Enter the second name" << endl; cin >> Orders.secondName; cout << "Enter the phone number" << endl; cin >> Orders.phoneNumber; cout << "Enter the cost" << endl; cin >> Orders.cost; cout << "Enter the day of recept" << endl; cin >> Orders.dayOfRecept; cout << "Enter the month of recept" << endl; cin >> Orders.monthOfRecept; cout << "Enter the year of recept" << endl; cin >> Orders.yearOfRecept; cout << "Enter the day of issue" << endl; cin >> Orders.dayOfIssue; cout << "Enter the month of issue" << endl; cin >> Orders.monthOfIssue; cout << "Enter the year of issue" << endl; cin >> Orders.yearOfIssue; cout << "Enter the status" << endl; cin >> Orders.status; file << Orders.product << " " << Orders.mark << " " << Orders.surname << " " << Orders.name << " " << Orders.secondName << " " << Orders.phoneNumber << " " << Orders.cost << " " << Orders.dayOfRecept << " " << Orders.monthOfRecept << " " << Orders.yearOfRecept << " " << Orders.dayOfIssue << " " << Orders.monthOfIssue << " " << Orders.yearOfIssue << " " << Orders.status <<"\n"; file.close(); cout << "Order added!" << endl; } void editOrdersFile(orders Orders) { vector <orders> ord; ifstream file("orders.txt"); while (file >> Orders.product >> Orders.mark >> Orders.surname >> Orders.name >> Orders.secondName >> Orders.phoneNumber >> Orders.cost >> Orders.dayOfRecept >> Orders.monthOfRecept >> Orders.yearOfRecept >> Orders.dayOfIssue >> Orders.monthOfIssue >> Orders.yearOfIssue >> Orders.status) { ord.push_back(Orders); } file.close(); showOredrsFile(Orders); cout << "Enter the number of order, you want to edit" << endl; int choise = 0; cin >> choise; cout << "Enter the product" << endl; cin >> Orders.product; cout << "Enter the mark" << endl; cin >> Orders.mark; cout << "Enter the surname" << endl; cin >> Orders.surname; cout << "Enter the name" << endl; cin >> Orders.name; cout << "Enter the second name" << endl; cin >> Orders.secondName; cout << "Enter the phone number" << endl; cin >> Orders.phoneNumber; cout << "Enter the cost" << endl; cin >> Orders.cost; cout << "Enter the day of recept" << endl; cin >> Orders.dayOfRecept; cout << "Enter the month of recept" << endl; cin >> Orders.monthOfRecept; cout << "Enter the year of recept" << endl; cin >> Orders.yearOfRecept; cout << "Enter the day of issue" << endl; cin >> Orders.dayOfIssue; cout << "Enter the month of issue" << endl; cin >> Orders.monthOfIssue; cout << "Enter the year of issue" << endl; cin >> Orders.yearOfIssue; cout << "Enter the status" << endl; cin >> Orders.status; ord[choise] = Orders; ofstream file1("orders.txt"); for (int i = 0; i < ord.size(); i++) { file1 << ord[i].product << " " << ord[i].mark << " " << ord[i].surname << " " << ord[i].name << " " << ord[i].secondName << " " << ord[i].phoneNumber << " " << ord[i].cost << " " << ord[i].dayOfRecept << " " << ord[i].monthOfRecept << " " << ord[i].yearOfRecept << " " << ord[i].dayOfIssue << " " << ord[i].monthOfIssue << " " << ord[i].yearOfIssue << " " << ord[i].status << "\n"; } } 

The task is to change information about the selected order and record the updated information to a file. However, when entering new order data, it displays the error "Vector subscript out of range". Where is the mistake?

    1 answer 1

    In the editOrdersFile function editOrdersFile you open a file, fill a vector with orders from a file, then you open the same file again when you call showOredrsFile(Orders); and output all orders from the file. Well, this is not the answer to the question, although the state of your file is no longer good .

      cin >> choise; 

    it is not known what you initialized choise , but then you quietly perform the assignment

     ord[choise] = Orders; 

    What makes you think that a vector has more than choise elements? .. The compiler swears because of this, but in general, write the code again better, otherwise it looks more like a disgrace than a code ...