Exception thrown: read access violation. **_Pnext** was 0x15556E4. occurred
Не выдает никаких ошибок при компиляции, но при запуске выскакивает ошибка
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <Windows.h>
using namespace std;
class SportShop
{
public:
SportShop() {
name = "";
manufacturer = "";
country = "";
price = 0;
}
SportShop(string name,string manufacturer,string country, int price) {
this->name = name;
this->country = country;
this->manufacturer = manufacturer;
this->price = price;
}
void Print() {
cout << "Название:\t" << name << endl;
cout << "Производитель:\t" << manufacturer << endl;
cout << "Страна производителя:\t" << country << endl;
cout << "Цена:\t" << price << endl;
cout << endl;
}
private:
string name;
string manufacturer;
string country;
int price;
};
void SaveClass(string name, string manufacturer, string country, int price);
void OutPutClass();
int GetValue(string s);
int SelectionMenu();
string EnterStringDate(string message);
void AddProduct();
void MainMenu();
bool AskToRepeatFunction(string s);
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
setlocale(LC_ALL, "ru");
while (true)
{
MainMenu();
}
return 0;
}
void MainMenu() {
switch (SelectionMenu())
{
case 1:
AddProduct();
break;
case 2:
OutPutClass();
break;
default:
break;
}
}
int SelectionMenu() {
int choice;
cout << endl << "Введите пункт, в который вы хотите перейти." << endl << endl;
do
{
cout << "1)Добавить товар\n2)Посмотреть товары\n3)Удалить товары\n4)Выход." << endl << endl;
choice = GetValue("Ввод: ");
if (choice < 1 || choice > 4) {
cout << "Нет такого действия, попробуйте ввести действие снова!" << endl;
}
cout.flush();
} while (choice < 1 || choice > 4);
return choice;
}
void AddProduct() {
while (true)
{
string name;
string manufacturer;
string country;
int price;
name = EnterStringDate("Введите название продукта\nВвод: ");
manufacturer = EnterStringDate("Введите производителя\nВвод: ");
country = EnterStringDate("Введите страну производства\nВвод: ");
price = GetValue("Введите цену\nВвод: ");
SaveClass(name, manufacturer, country, price);
if (AskToRepeatFunction("Хотить добавить еще один товар?")){
return;
}
}
}
string EnterStringDate(string message) {
string s;
cout << message;
getline(cin, s);
return s;
}
bool AskToRepeatFunction(string s)
{
char choice;
bool exitFunction = false;
do
{
cout << s << endl;
cout << "Y)Да\nN)Нет\n" << endl;
cout << "Ввод: ";
cin >> choice;
if (!(choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N')) {
cout << "Нет такого действия, попробуйте ввести действие снова!" << endl;
}
} while (!(choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N'));
if (choice == 'n' || choice == 'N') {
exitFunction = true;
}
return exitFunction;
}
int GetValue(string s) {
int a;
while (true)
{
cout << s;
cin >> a;
if (cin.fail()) {
cin.clear();
cin.ignore(32767, '\n');
cout << "Ошибка: \"Ввод недопустимых символов\", попробуйте снова" << endl;
}
else {
cin.ignore(32767, '\n');
return a;
}
}
}
void SaveClass(string name,string manufacturer,string country,int price) {
ofstream fout;
SportShop sportShop(name, manufacturer,country,price);
fout.open("items.txt", ofstream::app);
if (!fout.is_open())
{
cout << "Ошибка открытия файла" << endl;
}
else
{
cout << "Файл открыт!" << endl;
fout.write((char*)&sportShop, sizeof(SportShop));
}
fout.close();
}
void OutPutClass() {
ifstream fin;
fin.open("items.txt");
if (!fin.is_open())
{
cout << "Ошибка открытия файла!" << endl;
}
else
{
cout << "Файл открыт!" << endl;
SportShop sp;
while (fin.read((char*)&sp, sizeof(SportShop)))
{
sp.Print();
}
}
fin.close();
}