我用c++创建了一个图书信息管理系统,但是在运行时出现了异常处理,导致运行不出来,怎么解决

//单链表的基本操作和应用
#include <iostream>
#include <cstdio>
#include <string>
#include<iomanip>
#include< sstream >
using namespace std;
//定义图书的结构体
class book
{
public:
string name;
string author;
double price;
int number;
book* next;
book(string n, string a, double p, int num) :name(n), author(a), price(p), number(num) {
next = nullptr;
}
};
class List
{
public:book* head;
List()//单链表的无参构造函数也是一个初始化函数
{
head = new book(0, 0, 0, 0);
head->next = nullptr;
}
//按照位置插入结点
void insert(int position, string n, string a, double p, int num) {
if (head == nullptr) {
cout << "链表未初始化。" << endl;
return;
}
if (position < 1) {
cout << "插入位置必须大于 0" << endl;
return;
}
book* p1 = head; // 作为遍历指针
int i = 0;
while (p1 != nullptr && i < position - 1) {
p1 = p1->next;
i++;
}
if (p1 == nullptr) {
cout << "插入位置异常" << endl;
}
else {
book* p2 = new book(n, a, p, num);
p2->next = p1->next;
p1->next = p2;
cout << "插入成功!" << endl;
}
}
void delete_node(int position) {
if (head == nullptr) {
cout << "链表未初始化。" << endl;
return;
}
if (position < 1) {
cout << "删除位置必须大于 0" << endl;
return;
}
book* p1 = head; // 作为遍历指针
book* p2; // p2 用于指向被删除节点
int i = 0;
while (p1 != nullptr && i < position - 1) {
p1 = p1->next;
i++;
}
if (p1 == nullptr || p1->next == nullptr) {
cout << "删除位置异常" << endl;
}
else {
p2 = p1->next; // p2 指向要被删除的节点
p1->next = p2->next; // 更新 p1 的指针
delete p2; // 删除节点
cout << "删除成功!" << endl;
}
}
//按书名查询节点返回位置
int search(string name)
{
book* p = head->next;
int i = 0;
while (p)
{
if (name == p->name)
return i;
i++;
p = p->next;
}
return -1;
}
//输出书籍信息
void Print()
{
book* p = head->next;
cout << left << setw(10) << "书名"
<< "\t" << left << setw(30) << "作者"
<< "\t" << left << setw(10) << "价格"
<< "\t" << left << setw(20) << "数量" << endl;
while (p)
{
cout << left << setw(10) << p->name
<< "\t" << left << setw(30) << p->author
<< "\t" << left << setw(10) << p->price
<< "\t" << left << setw(20) << p->number << endl;
p = p->next;
}
}
//修改节点信息
void modify(int position, string n, string a, double p, int num)
{
book* p1 = head->next;
int i = 0;
while (i < position - 1 && p1)
{
p1 = p1->next;
i++;
}
if (p1)
{
p1->name = n;
p1->author = a;
p1->price = p;
p1->number = num;
cout << "修改成功!" << endl;
}
else
cout << "修改位置异常" << endl;
}
~List() { // 析构函数
clear(); // 确保释放所有书籍节点
delete head; // 释放头节点
}
void clear() { // 清空所有书籍节点
book* current = head->next;
book* next;
while (current) {
next = current->next;
delete current;
current = next;
}
head->next = nullptr; // 清空后的链表
}
};
void showMenu() {
cout << "\n====== 书籍管理系统 ======" << endl;
cout << "1. 插入书籍" << endl;
cout << "2. 删除书籍" << endl;
cout << "3. 查询书籍位置" << endl;
cout << "4. 输出书籍信息" << endl;
cout << "5. 修改书籍信息" << endl;
cout << "0. 退出" << endl;
cout << "==========================" << endl;
cout << "请输入你的选择: ";
}
int main() {
List bookList;
int choice;
string name, author;
double price;
int number, position;
while (true) {
showMenu();
cin >> choice;
switch (choice) {
case 1: // 插入书籍
cout << "请输入书名: ";
cin >> name;
cout << "请输入作者: ";
cin >> author;
cout << "请输入价格: ";
cin >> price;
cout << "请输入数量: ";
cin >> number;
cout << "请输入插入位置: ";
cin >> position;
bookList.insert(position, name, author, price, number);
break;
case 2: // 删除书籍
cout << "请输入删除位置: ";
cin >> position;
bookList.delete_node(position);
break;
case 3: // 查询书籍位置
cout << "请输入书名查询位置: ";
cin >> name;
position = bookList.search(name);
if (position != -1) {
cout << "书籍 '" << name << "' 的位置: " << position << endl;
}
else {
cout << "未找到书籍: " << name << endl;
}
break;
case 4: // 输出书籍信息
bookList.Print();
break;
case 5: // 修改书籍信息
cout << "请输入要修改书籍的位置: ";
cin >> position;
cout << "请输入新的书名: ";
cin >> name;
cout << "请输入新的作者: ";
cin >> author;
cout << "请输入新的价格: ";
cin >> price;
cout << "请输入新的数量: ";
cin >> number;
bookList.modify(position, name, author, price, number);
break;
case 6://清空书库中书籍的信息
bookList.clear();
cout << "清空成功" << endl;
break;
case 0: // 退出
cout << "退出程序..." << endl;
return 0;
default:
cout << "无效的选择,请重新输入." << endl;
}
}
return 0;
}