面向过程时运行正常,当前面加了一个类之后,添加图书之后,显示结果为空,正常情况应该是显示出添加图书的信息的
#include <iostream>
#include <string>
#include<strstream>
//#include "CBook.h"
using namespace std;
#define MAX 1000class CBook {
private:
struct Book {
char strName[256]; //书名
long nISBN; //ISBN
char strAuthor[256]; //作者
char strPublic[256]; //出版社
float fPrice; //价格
};
struct Book bookArray[MAX]; //图书数组
int nIndex = 0; //图书索引public:
//添加图书函数
void addBook()
{
//判断书库是否满了
if (nIndex == MAX)
{
cout << "书库已满,无法添加" << endl;
return;
}
else {
cout << "请输入书名:" << endl;
cin >> bookArray[nIndex].strName;
cout << "请输入ISBN:" << endl;
cin >> bookArray[nIndex].nISBN;
cout << "请输入作者:" << endl;
cin >> bookArray[nIndex].strAuthor;
cout << "请输入出版社:" << endl;
cin >> bookArray[nIndex].strPublic;
cout << "请输入单价:" << endl;
cin >> bookArray[nIndex].fPrice;//更新图书索引
nIndex++;
cout << "添加成功" << endl;
system("pause"); //暂停屏幕的输出,按下任意键结束
system("cls"); //清空输出显示
}
}//显示所有图书
void showBook()
{
if (nIndex == 0)
{
cout << "当前记录为空" << endl;
}
else {
for (int i = 0; i < nIndex; i++)
{
cout << "姓名:" << bookArray[i].strName << endl;
cout << "作者:" << bookArray[i].strAuthor << endl;
cout << "出版社:" << bookArray[i].strPublic << endl;
cout << "ISBN:" << bookArray[i].nISBN << endl;
cout << "单价:" << bookArray[i].fPrice << endl;
}
}
}//判断该书名的图书是否存在
int isExist(char name[256])
{
for (int i = 0; i < nIndex; i++)
{
if (strcmp(bookArray[i].strName, name) == 0)
return i;
}
return -1;
}//删除图书函数
void deleteBook()
{
cout << "请输入您要删除的书名" << endl;
char name[256];
cin >> name;
int ret = isExist(name);
if (ret != -1)
{
for (int i = ret; i < nIndex; i++)
{
bookArray[i] = bookArray[i + 1];
}
nIndex--;
cout << "删除成功" << endl;
}
else
{
cout << "没有此图书" << endl;
}system("pause");
system("cls");
}//查找图书函数
void findBook()
{
cout << "请输入您要查找图书的书名" << endl;
char name[256];
cin >> name;
int ret = isExist(name);
if (ret != -1)
{
cout << "姓名:" << bookArray[ret].strName << endl;
cout << "作者:" << bookArray[ret].strAuthor << endl;
cout << "出版社:" << bookArray[ret].strPublic << endl;
cout << "ISBN:" << bookArray[ret].nISBN << endl;
cout << "单价:" << bookArray[ret].fPrice << endl;
}
else
{
cout << "查无此人" << endl;
}system("pause");
system("cls");
}//修改图书函数
void modifyBook()
{
cout << "请输入您要修改图书的书名" << endl;
char name[256];
cin >> name;
int ret = isExist(name);
if (ret != -1)
{
//姓名
cout << "请输入书名:" << endl;
cin >> bookArray[ret].strName;
cout << "请输入ISBN:" << endl;
cin >> bookArray[ret].nISBN;
cout << "请输入作者:" << endl;
cin >> bookArray[ret].strAuthor;
cout << "请输入出版社:" << endl;
cin >> bookArray[ret].strPublic;
cout << "请输入单价:" << endl;
cin >> bookArray[ret].fPrice;cout << "修改成功" << endl;
}
else
{
cout << "查无此图书" << endl;
}system("pause");
system("cls");
}//清空图书函数
void cleanBook()
{
nIndex = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}//显示菜单函数
void showMenu()
{
cout << "" << endl;
cout << "***********" << "欢迎来到图书管理系统" << "**********" << endl;
cout << "**************" << " 1、添加图书 " << "**************" << endl;
cout << "**************" << " 2、显示图书 " << "**************" << endl;
cout << "**************" << " 3、删除图书 " << "**************" << endl;
cout << "**************" << " 4、查找图书 " << "**************" << endl;
cout << "**************" << " 5、修改图书 " << "**************" << endl;
cout << "**************" << " 6、清空图书 " << "**************" << endl;
cout << "**************" << " 0、退出图书系统 " << "**********" << endl;
cout << "*****************************************" << endl;
}};
int main()
{
int select = 0;
while (true)
{
CBook* cbook = new CBook;
cbook->showMenu();
//showMenu();
cin >> select;
switch (select)
{
case 1: //添加图书
cbook->addBook();
//addBook();
break;
case 2: //显示图书
cbook->showBook();
//showBook();
break;
case 3: //删除图书
cbook->deleteBook();
//deleteBook();
break;
case 4: //查找图书
cbook->findBook();
//findBook();
break;
case 5: //修改图书
cbook->modifyBook();
//modifyBook();
break;
case 6: //清空图书
cbook->cleanBook();
//cleanBook();
break;
case 0: //退出系统
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}