程序为什么单步执行没有问题,直接运行就会出现问题

时间:2022-10-26 15:35:17
#include<iostream>
#include<iomanip>
#include <stdlib.h>
using namespace std;

class Book
{
public:
int book_id;
char book_name[20];
char writer[20];
int now_number;
int sum_number;
Book();
};
class Member
{
public:
int ID;
int borrow_number;
int borrow_book_id;
int time;
Member()
{
borrow_book_id=0;
time=0;
ID=0;
borrow_number=0;
}
};

class Node 
{
public:
    int data;
    Node* next;
    Node(int _data) 
{
data=_data;
        next = NULL;
    }
};
class Index 
{
private:
    Node* head;
public:
    Index() 
{
        head = NULL;
    }
    void insert(Node *node, int index) 
{
        if (head == NULL) 
{
            head = node;
            return;
        }
        if (index == 0) 
{
            node->next = head;
            head = node;
            return;
        }
        Node *current_node = head;
        int count = 0;
        while (current_node->next != NULL && count < index - 1) 
{
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1) 
{
            node->next = current_node->next;
            current_node->next = node;
        }
    }
    void output() 
{
        if (head == NULL) 
{
            return;
        }
        Node *current_node = head;
        while (current_node != NULL) 
{
            cout << current_node->data <<endl;
            current_node = current_node->next;
        }
        cout << endl;
    }
    void delete_node(int index)
{
        if(head==NULL)
{
return ;
}
        Node *current_node=head;
        int count=0;
        if(index==0)
{
            head=head->next;
            delete current_node;
            return ;
        }
        while(current_node->next!=NULL&&count<index-1)
{
            current_node=current_node->next;
            count++;
        }
        if(count==index-1 &&current_node->next!=NULL)
{
            Node *delete_node=current_node->next;
            current_node->next=delete_node->next;
            delete delete_node;
        }
    }
int serch(int index)
{
if (head == NULL) 
{
            return 0;
        }
Node *current_node = head;
while(current_node->data!=index && current_node->next != NULL)
{
current_node=current_node->next;
}
if(current_node->data==index)
{
return 1;
}
else
{
return 0;
}
}
};

Book::Book()
{
book_id=0;
now_number=0;
sum_number=0;
}

void add(Book *str_book,Index *str_index)
{
system("cls");
int new_id,count=0;
cout<<"请输入图书的书号"<<endl;
cin>>new_id;
while(str_book->book_id==new_id||str_book->book_id==0)
{
str_book++;
count++;
}
if(str_book->book_id==new_id)
{
cout<<"此图书存在,请输入增加库存量"<<endl;
int i;
cin>>i;
str_book->sum_number=i;
}
else
{
int i;
str_book->book_id=new_id;
cout<<"请输入图书书名"<<endl;
cin.ignore(1024,'\n');
cin.getline(str_book->book_name,20);
cout<<"作者姓名"<<endl;
cin.getline(str_book->writer,20);
cout<<"请输入采购量"<<endl;
cin>>i;
str_book->now_number=i;
str_book->sum_number=i;
cout<<"收录完毕"<<endl;
cout<<"请按回车键确认"<<endl;
getchar();
}
}
void serch(Book *str_book)
{
system("cls");
int x;
cout<<"请输入图书书号"<<endl;
cin>>x;
while(str_book->book_id==x||str_book->book_id==0)
{
str_book++;
}
if(str_book==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
cout<<"图书书号:"<<str_book->book_id<<endl;
cout<<"图书书名:"<<str_book->book_name<<endl;
cout<<"作者姓名:"<<str_book->writer<<endl;
cout<<"现存量:"<<str_book->now_number<<endl;
cout<<"库存量:"<<str_book->sum_number<<endl;
}
getchar();
}

void borrow(Book *str_book,Member *str_member)
{
system("cls");
int x,y;
cout<<"请输入借书会员号:"<<endl;
cin>>y;
while(str_member->ID==y||str_member->ID==0)
{
str_member++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"请输入所借图书书号"<<endl;
cin>>x;
while(str_book->book_id==x||str_book->book_id==0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
if(str_book->now_number==0)
{
cout<<"图书库存为0,无法借阅"<<endl;
}
else
{
int y;
cout<<"请输入借书时间:"<<endl;
cin>>y;
str_member->time=y;
str_book->now_number--;
str_member->borrow_number++;
str_member->borrow_book_id=x;
cout<<"借书成功"<<endl;
}
}
}
getchar();
}

void get(Book *str_book,Member *str_member)
{
system("cls");
int x,y;
cout<<"请输入归还图书会员号:"<<endl;
cin>>y;
while(str_member->ID==y||str_member->ID==0)
{
str_member++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"请输入归还图书书号:"<<endl;
cin>>x;
while(str_book->book_id==x||str_book->book_id==0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
str_book->now_number++;
str_member->borrow_number--;
str_member->time=0;
str_member->borrow_book_id=0;
cout<<"还书成功"<<endl;
}
}
getchar();
}

void drop(Book *str_book,Index *str_index)
{
system("cls");
int x,count=0;
cout<<"请输入删除图书书号:"<<endl;
cin>>x;
while(str_book->book_id==x||str_book->book_id==0)
{
str_book++;
count++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
while(str_book->book_id==0)
{
str_book[count]=str_book[count+1];
count++;
cout<<"删除成功"<<endl;
}
}
getchar();
}

void add_member(Member *str_member)
{
system("cls");
while(str_member->ID==0)
{
str_member++;
}
cout<<"请输入会员ID:"<<endl;
cin>>str_member->ID;
cout<<"增加成功"<<endl;
getchar();
}

void serch_member(Member *str_member)
{
system("cls");
int y;
cout<<"请输入查询会员号:"<<endl;
cin>>y;
while(str_member->ID==y||str_member->ID==0)
{
str_member++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"会员ID为:"<<str_member->ID<<endl;
cout<<"会员所借图书册数为:"<<str_member->borrow_number<<endl;
}
getchar();
}

void drop_member(Member *str_member)
{
system("cls");
int y,count=0;
cout<<"请输入删除会员号:"<<endl;
cin>>y;
while(str_member->ID==y||str_member->ID==0)
{
str_member++;
count++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
while(str_member->ID==0)
{
str_member[count]=str_member[count+1];
count++;
cout<<"删除成功"<<endl;
}
}
getchar();
}

void borrow_book(Book *str_book,Member *str_member)
{
system("cls");
while(str_member->borrow_number==0)
{
cout<<'\t'<<setw(8)<<"会员ID"<<'\t'<<setw(8)<<"所借图书ID"<<'\t'<<setw(8)<<"借书期限"<<endl;
cout<<'\t'<<setw(8)<<str_member->ID<<'\t'<<setw(8)<<str_member->borrow_book_id<<'\t'<<setw(8)<<str_member->time;
str_member++;
}
getchar();
}

void put()
{
system("cls");
cout<<'\t'<<"********************欢迎进入图书馆借阅系统********************"<<endl;
cout<<'\t'<<"********************  请选择所需要的功能  ********************"<<endl;
cout<<'\t'<<"*****1 增加图书                               2 查询图书******"<<endl;
cout<<'\t'<<"*****3 图书借阅                               4 归还图书******"<<endl;
cout<<'\t'<<"*****5 删除图书                               6 增加会员******"<<endl;
cout<<'\t'<<"*****7 查询会员                               8 删除会员******"<<endl;
cout<<'\t'<<"*****9 借书信息                               0 退出系统******"<<endl;
cout<<'\t'<<"*************************************************************"<<endl;
}
int main()
{

int x;
Member *str_member=new Member[100];
Book *str_book=new Book[100];
Index *str_index=new Index;
while(1)
{
put();
cin>>x;
switch(x)
{
case 1:add(str_book,str_index) ;break;
case 2:serch(str_book); break;
case 3:borrow(str_book,str_member); break;
case 4:get(str_book,str_member); break;
case 5:drop(str_book,str_index); break;
case 6:add_member(str_member); break;
case 7:serch_member(str_member);break;
case 8:drop_member(str_member);break;
case 9:borrow_book(str_book,str_member);break;
}
}
}

以上是完整程序,每次进入主函数中while循环后,子程序就很快结束,直接回到循环

6 个解决方案

#1


目前好了,我发现是因子函数中while的判断条件错误了,已经改正如下
#include<iostream>
#include<iomanip>
#include <stdlib.h>
#include <windows.h>
using namespace std;

class Book
{
public:
int book_id;
char book_name[20];
char writer[20];
int now_number;
int sum_number;
Book();
};
class Member
{
public:
int ID;
int borrow_number;
int borrow_book_id;
int time;
Member()
{
borrow_book_id=0;
time=0;
ID=0;
borrow_number=0;
}
};

class Node 
{
public:
    int data;
    Node* next;
    Node(int _data) 
{
data=_data;
        next = NULL;
    }
};
class Index 
{
private:
    Node* head;
public:
    Index() 
{
        head = NULL;
    }
    void insert(Node *node, int index) 
{
        if (head == NULL) 
{
            head = node;
            return;
        }
        if (index == 0) 
{
            node->next = head;
            head = node;
            return;
        }
        Node *current_node = head;
        int count = 0;
        while (current_node->next != NULL && count < index - 1) 
{
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1) 
{
            node->next = current_node->next;
            current_node->next = node;
        }
    }
    void output() 
{
        if (head == NULL) 
{
            return;
        }
        Node *current_node = head;
        while (current_node != NULL) 
{
            cout << current_node->data <<endl;
            current_node = current_node->next;
        }
        cout << endl;
    }
    void delete_node(int index)
{
        if(head==NULL)
{
return ;
}
        Node *current_node=head;
        int count=0;
        if(index==0)
{
            head=head->next;
            delete current_node;
            return ;
        }
        while(current_node->next!=NULL&&count<index-1)
{
            current_node=current_node->next;
            count++;
        }
        if(count==index-1 &&current_node->next!=NULL)
{
            Node *delete_node=current_node->next;
            current_node->next=delete_node->next;
            delete delete_node;
        }
    }
int serch(int index)
{
if (head == NULL) 
{
            return 0;
        }
Node *current_node = head;
while(current_node->data!=index && current_node->next != NULL)
{
current_node=current_node->next;
}
if(current_node->data==index)
{
return 1;
}
else
{
return 0;
}
}
};

Book::Book()
{
book_id=0;
now_number=0;
sum_number=0;
}

void add(Book *str_book,Index *str_index)
{
system("cls");
int new_id,count=0;
cout<<"请输入图书的书号"<<endl;
cin>>new_id;
while(str_book->book_id!=new_id&&str_book->book_id==0)
{
str_book++;
count++;
}
if(str_book->book_id==new_id)
{
cout<<"此图书存在,请输入增加库存量"<<endl;
int i;
cin>>i;
str_book->sum_number=i;
}
else
{
int i;
str_book->book_id=new_id;
cout<<"请输入图书书名"<<endl;
cin.ignore(1024,'\n');
cin.getline(str_book->book_name,20);
cout<<"作者姓名"<<endl;
cin.getline(str_book->writer,20);
cout<<"请输入采购量"<<endl;
cin>>i;
str_book->now_number=i;
str_book->sum_number=i;
cout<<"收录完毕"<<endl;
cout<<"回到主程序"<<endl;
Sleep(2000);
}
}
void serch(Book *str_book)
{
system("cls");
int x;
cout<<"请输入图书书号"<<endl;
cin>>x;
while(str_book->book_id!=x&&str_book->book_id!=0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
cout<<"图书书号:"<<str_book->book_id<<endl;
cout<<"图书书名:"<<str_book->book_name<<endl;
cout<<"作者姓名:"<<str_book->writer<<endl;
cout<<"现存量:"<<str_book->now_number<<endl;
cout<<"库存量:"<<str_book->sum_number<<endl;
}
Sleep(2000);
}

void borrow(Book *str_book,Member *str_member)
{
system("cls");
int x,y;
cout<<"请输入借书会员号:"<<endl;
cin>>y;
while(str_member->ID!=y&&str_member->ID!=0)
{
str_member++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"请输入所借图书书号"<<endl;
cin>>x;
while(str_book->book_id!=y&&str_book->book_id!=0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
if(str_book->now_number==0)
{
cout<<"图书库存为0,无法借阅"<<endl;
}
else
{
int y;
cout<<"请输入借书时间:"<<endl;
cin>>y;
str_member->time=y;
str_book->now_number--;
str_member->borrow_number++;
str_member->borrow_book_id=x;
cout<<"借书成功"<<endl;
}
}
}
Sleep(2000);
}

void get(Book *str_book,Member *str_member)
{
system("cls");
int x,y;
cout<<"请输入归还图书会员号:"<<endl;
cin>>y;
while(str_member->ID!=y&&str_member->ID!=0)
{
str_member++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"请输入归还图书书号:"<<endl;
cin>>x;
while(str_book->book_id!=y&&str_book->book_id!=0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
str_book->now_number++;
str_member->borrow_number--;
str_member->time=0;
str_member->borrow_book_id=0;
cout<<"还书成功"<<endl;
}
}
Sleep(2000);
}

void drop(Book *str_book,Index *str_index)
{
system("cls");
int x,count=0;
cout<<"请输入删除图书书号:"<<endl;
cin>>x;
while(str_book->book_id!=x&&str_book->book_id!=0)
{
str_book++;
count++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
while(str_book->book_id==0)
{
str_book[count]=str_book[count+1];
count++;
cout<<"删除成功"<<endl;
}
}
Sleep(2000);
}

void add_member(Member *str_member)
{
system("cls");
while(str_member->ID==0)
{
str_member++;
}
cout<<"请输入会员ID:"<<endl;
cin>>str_member->ID;
cout<<"增加成功"<<endl;
Sleep(2000);
}

void serch_member(Member *str_member)
{
system("cls");
int y;
cout<<"请输入查询会员号:"<<endl;
cin>>y;
while(str_member->ID!=y&&str_member->ID!=0)
{
str_member++;
cout<<str_member->ID<<endl;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"会员ID为:"<<str_member->ID<<endl;
cout<<"会员所借图书册数为:"<<str_member->borrow_number<<endl;
}
Sleep(2000);
}

void drop_member(Member *str_member)
{
system("cls");
int y,count=0;
cout<<"请输入删除会员号:"<<endl;
cin>>y;
while(str_member->ID==y&&str_member->ID==0)
{
str_member++;
count++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
while(str_member->ID==0)
{
str_member[count]=str_member[count+1];
count++;
cout<<"删除成功"<<endl;
}
}
Sleep(2000);
}

void borrow_book(Book *str_book,Member *str_member)
{
system("cls");
while(str_member->borrow_number!=0)
{
cout<<'\t'<<setw(8)<<"会员ID"<<'\t'<<setw(8)<<"所借图书ID"<<'\t'<<setw(8)<<"借书期限"<<endl;
cout<<'\t'<<setw(8)<<str_member->ID<<'\t'<<setw(8)<<str_member->borrow_book_id<<'\t'<<setw(8)<<str_member->time;
str_member++;
}
Sleep(2000);
}

void put()
{
system("cls");
cout<<'\t'<<"********************欢迎进入图书馆借阅系统********************"<<endl;
cout<<'\t'<<"********************  请选择所需要的功能  ********************"<<endl;
cout<<'\t'<<"*****1 增加图书                               2 查询图书******"<<endl;
cout<<'\t'<<"*****3 图书借阅                               4 归还图书******"<<endl;
cout<<'\t'<<"*****5 删除图书                               6 增加会员******"<<endl;
cout<<'\t'<<"*****7 查询会员                               8 删除会员******"<<endl;
cout<<'\t'<<"*****9 借书信息                               0 退出系统******"<<endl;
cout<<'\t'<<"*************************************************************"<<endl;
}
int main()
{

int x=1;
Member *str_member=new Member[10];
Book *str_book=new Book[10];
Index *str_index=new Index;
while(x!=0)
{
put();
cin>>x;
switch(x)
{
case 1:add(str_book,str_index) ;break;
case 2:serch(str_book); break;
case 3:borrow(str_book,str_member); break;
case 4:get(str_book,str_member); break;
case 5:drop(str_book,str_index); break;
case 6:add_member(str_member); break;
case 7:serch_member(str_member);break;
case 8:drop_member(str_member);break;
case 9:borrow_book(str_book,str_member);break;
}
}
return 0; 
}

但是,,有另外一个问题,就是为什么我的add函数赋值以后,其他函数在调用就没有值了

#2


插入算法错误,书被你插入到数组外部了

#3


LZ会单步跟踪,到关键步骤打印一下值不就找到错误所在了?

#4


void add_member(Member *str_member)
{
    system("cls");
    Member *s=str_member;
    while(s->ID==0)
    {
        s++;
    }
    cout<<"请输入会员ID:"<<endl;
    cin>>s->ID;
    cout<<"增加成功"<<endl;
    Sleep(2000);
}

#5


程序也太长了。

#6


单步调试没问题,直接运行出错
有很大可能就是索引越界,前几步都正常,推进大了,代码逻辑出错,指针越界、数组越界都有可能

#1


目前好了,我发现是因子函数中while的判断条件错误了,已经改正如下
#include<iostream>
#include<iomanip>
#include <stdlib.h>
#include <windows.h>
using namespace std;

class Book
{
public:
int book_id;
char book_name[20];
char writer[20];
int now_number;
int sum_number;
Book();
};
class Member
{
public:
int ID;
int borrow_number;
int borrow_book_id;
int time;
Member()
{
borrow_book_id=0;
time=0;
ID=0;
borrow_number=0;
}
};

class Node 
{
public:
    int data;
    Node* next;
    Node(int _data) 
{
data=_data;
        next = NULL;
    }
};
class Index 
{
private:
    Node* head;
public:
    Index() 
{
        head = NULL;
    }
    void insert(Node *node, int index) 
{
        if (head == NULL) 
{
            head = node;
            return;
        }
        if (index == 0) 
{
            node->next = head;
            head = node;
            return;
        }
        Node *current_node = head;
        int count = 0;
        while (current_node->next != NULL && count < index - 1) 
{
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1) 
{
            node->next = current_node->next;
            current_node->next = node;
        }
    }
    void output() 
{
        if (head == NULL) 
{
            return;
        }
        Node *current_node = head;
        while (current_node != NULL) 
{
            cout << current_node->data <<endl;
            current_node = current_node->next;
        }
        cout << endl;
    }
    void delete_node(int index)
{
        if(head==NULL)
{
return ;
}
        Node *current_node=head;
        int count=0;
        if(index==0)
{
            head=head->next;
            delete current_node;
            return ;
        }
        while(current_node->next!=NULL&&count<index-1)
{
            current_node=current_node->next;
            count++;
        }
        if(count==index-1 &&current_node->next!=NULL)
{
            Node *delete_node=current_node->next;
            current_node->next=delete_node->next;
            delete delete_node;
        }
    }
int serch(int index)
{
if (head == NULL) 
{
            return 0;
        }
Node *current_node = head;
while(current_node->data!=index && current_node->next != NULL)
{
current_node=current_node->next;
}
if(current_node->data==index)
{
return 1;
}
else
{
return 0;
}
}
};

Book::Book()
{
book_id=0;
now_number=0;
sum_number=0;
}

void add(Book *str_book,Index *str_index)
{
system("cls");
int new_id,count=0;
cout<<"请输入图书的书号"<<endl;
cin>>new_id;
while(str_book->book_id!=new_id&&str_book->book_id==0)
{
str_book++;
count++;
}
if(str_book->book_id==new_id)
{
cout<<"此图书存在,请输入增加库存量"<<endl;
int i;
cin>>i;
str_book->sum_number=i;
}
else
{
int i;
str_book->book_id=new_id;
cout<<"请输入图书书名"<<endl;
cin.ignore(1024,'\n');
cin.getline(str_book->book_name,20);
cout<<"作者姓名"<<endl;
cin.getline(str_book->writer,20);
cout<<"请输入采购量"<<endl;
cin>>i;
str_book->now_number=i;
str_book->sum_number=i;
cout<<"收录完毕"<<endl;
cout<<"回到主程序"<<endl;
Sleep(2000);
}
}
void serch(Book *str_book)
{
system("cls");
int x;
cout<<"请输入图书书号"<<endl;
cin>>x;
while(str_book->book_id!=x&&str_book->book_id!=0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
cout<<"图书书号:"<<str_book->book_id<<endl;
cout<<"图书书名:"<<str_book->book_name<<endl;
cout<<"作者姓名:"<<str_book->writer<<endl;
cout<<"现存量:"<<str_book->now_number<<endl;
cout<<"库存量:"<<str_book->sum_number<<endl;
}
Sleep(2000);
}

void borrow(Book *str_book,Member *str_member)
{
system("cls");
int x,y;
cout<<"请输入借书会员号:"<<endl;
cin>>y;
while(str_member->ID!=y&&str_member->ID!=0)
{
str_member++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"请输入所借图书书号"<<endl;
cin>>x;
while(str_book->book_id!=y&&str_book->book_id!=0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
if(str_book->now_number==0)
{
cout<<"图书库存为0,无法借阅"<<endl;
}
else
{
int y;
cout<<"请输入借书时间:"<<endl;
cin>>y;
str_member->time=y;
str_book->now_number--;
str_member->borrow_number++;
str_member->borrow_book_id=x;
cout<<"借书成功"<<endl;
}
}
}
Sleep(2000);
}

void get(Book *str_book,Member *str_member)
{
system("cls");
int x,y;
cout<<"请输入归还图书会员号:"<<endl;
cin>>y;
while(str_member->ID!=y&&str_member->ID!=0)
{
str_member++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"请输入归还图书书号:"<<endl;
cin>>x;
while(str_book->book_id!=y&&str_book->book_id!=0)
{
str_book++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
str_book->now_number++;
str_member->borrow_number--;
str_member->time=0;
str_member->borrow_book_id=0;
cout<<"还书成功"<<endl;
}
}
Sleep(2000);
}

void drop(Book *str_book,Index *str_index)
{
system("cls");
int x,count=0;
cout<<"请输入删除图书书号:"<<endl;
cin>>x;
while(str_book->book_id!=x&&str_book->book_id!=0)
{
str_book++;
count++;
}
if(str_book->book_id==0)
{
cout<<"对不起,没有此图书"<<endl;
}
else
{
while(str_book->book_id==0)
{
str_book[count]=str_book[count+1];
count++;
cout<<"删除成功"<<endl;
}
}
Sleep(2000);
}

void add_member(Member *str_member)
{
system("cls");
while(str_member->ID==0)
{
str_member++;
}
cout<<"请输入会员ID:"<<endl;
cin>>str_member->ID;
cout<<"增加成功"<<endl;
Sleep(2000);
}

void serch_member(Member *str_member)
{
system("cls");
int y;
cout<<"请输入查询会员号:"<<endl;
cin>>y;
while(str_member->ID!=y&&str_member->ID!=0)
{
str_member++;
cout<<str_member->ID<<endl;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
cout<<"会员ID为:"<<str_member->ID<<endl;
cout<<"会员所借图书册数为:"<<str_member->borrow_number<<endl;
}
Sleep(2000);
}

void drop_member(Member *str_member)
{
system("cls");
int y,count=0;
cout<<"请输入删除会员号:"<<endl;
cin>>y;
while(str_member->ID==y&&str_member->ID==0)
{
str_member++;
count++;
}
if(str_member->ID==0)
{
cout<<"对不起,没有此会员"<<endl;
}
else
{
while(str_member->ID==0)
{
str_member[count]=str_member[count+1];
count++;
cout<<"删除成功"<<endl;
}
}
Sleep(2000);
}

void borrow_book(Book *str_book,Member *str_member)
{
system("cls");
while(str_member->borrow_number!=0)
{
cout<<'\t'<<setw(8)<<"会员ID"<<'\t'<<setw(8)<<"所借图书ID"<<'\t'<<setw(8)<<"借书期限"<<endl;
cout<<'\t'<<setw(8)<<str_member->ID<<'\t'<<setw(8)<<str_member->borrow_book_id<<'\t'<<setw(8)<<str_member->time;
str_member++;
}
Sleep(2000);
}

void put()
{
system("cls");
cout<<'\t'<<"********************欢迎进入图书馆借阅系统********************"<<endl;
cout<<'\t'<<"********************  请选择所需要的功能  ********************"<<endl;
cout<<'\t'<<"*****1 增加图书                               2 查询图书******"<<endl;
cout<<'\t'<<"*****3 图书借阅                               4 归还图书******"<<endl;
cout<<'\t'<<"*****5 删除图书                               6 增加会员******"<<endl;
cout<<'\t'<<"*****7 查询会员                               8 删除会员******"<<endl;
cout<<'\t'<<"*****9 借书信息                               0 退出系统******"<<endl;
cout<<'\t'<<"*************************************************************"<<endl;
}
int main()
{

int x=1;
Member *str_member=new Member[10];
Book *str_book=new Book[10];
Index *str_index=new Index;
while(x!=0)
{
put();
cin>>x;
switch(x)
{
case 1:add(str_book,str_index) ;break;
case 2:serch(str_book); break;
case 3:borrow(str_book,str_member); break;
case 4:get(str_book,str_member); break;
case 5:drop(str_book,str_index); break;
case 6:add_member(str_member); break;
case 7:serch_member(str_member);break;
case 8:drop_member(str_member);break;
case 9:borrow_book(str_book,str_member);break;
}
}
return 0; 
}

但是,,有另外一个问题,就是为什么我的add函数赋值以后,其他函数在调用就没有值了

#2


插入算法错误,书被你插入到数组外部了

#3


LZ会单步跟踪,到关键步骤打印一下值不就找到错误所在了?

#4


void add_member(Member *str_member)
{
    system("cls");
    Member *s=str_member;
    while(s->ID==0)
    {
        s++;
    }
    cout<<"请输入会员ID:"<<endl;
    cin>>s->ID;
    cout<<"增加成功"<<endl;
    Sleep(2000);
}

#5


程序也太长了。

#6


单步调试没问题,直接运行出错
有很大可能就是索引越界,前几步都正常,推进大了,代码逻辑出错,指针越界、数组越界都有可能