Code quản lí sách vở trong kho sách bị sai
Hiện tại em có làm 1 đoạn code quản lí sách vở trong kho sách nhưng trong đoạn có 1 số chỗ không hoạt động và hoạt động sai cụ thể là phần thống kê sách không hoạt động, phần thêm sách bị lỗi không hiển thị lúc xuất và mỗi lần nhập nó bị reset lại dữ liệu trong kho em đã kiểm tra kỹ nhưng không biết sai chỗ nào mong mọi người kiểm tra xem và sửa giúp em, em xin cảm ơn rất nhiều ạ
[code]#include “iostream”
#include “cstring”
#include “iomanip”
#include “time.h”
#include “stdio.h”
#include “conio.h”
#include “windows.h”
using namespace std;
long total_book = 0; // Tong sach cua thu vien.
long total = 0;
char ma_tam[50];
int i, p;
int info[10];
////===============================================================================================
//
// KHAI BAO CAC STRUCT.
//
////===============================================================================================
struct BOOK {
char b_ma[50];
char b_tensach[50];
char b_tacgia[50];
char b_theloai[50];
char b_nhaXB[50];
int b_soluong;
};
typedef BOOK LIST_BOOK[100]; // Kieu LIST_BOOK la mang 100 cuon sach.
LIST_BOOK lst_book; // Bien lst_book la mang 50 cuon sach.
// KHAI BAO MAU CHO HAM.
void sach_load_file();
void sach_save_file();
void sach_nhap();
void sach_xuat();
void sach_hieu_chinh();
void sach_them();
void sach_xoa();
int sach_tim(char ma_tam[]);
//-----------------------------------------------
void TK_theloai();
void TK_soluong_sach();
//-----------------------------------------------
void file_info();
void interface_main();
void interface_book_manager();
//// CHUONG TRINH CHINH.
int main() //
{ //
interface_main(); //
}
// DAY DUNG CAC HAM TO CHUC MENU.
//
void interface_book_manager() {
begin:
//system(“cls”);
cout << endl << endl;
cout << " ±------------------------------------------------+
";
cout << " | GIAO DIEN QUAN LY SACH |
";
cout << " |-------------------------------------------------|
";
cout << " | |
";
cout << " | 1. Nhap sach vao thu vien. |
";
cout << " | 2. Xoa sach khoa thu vien. |
";
cout << " | 3. Xuat Danh sach sach. |
";
cout << " | |
";
cout << " | 4. Tim sach trong Thu vien. |
";
cout << " | 5. Hieu chinh sach. |
";
cout << " | |
";
cout << " | 6. Back. |
";
cout << " | 7. Them sach vao thu vien. |
";
cout << " | |
";
cout << " ±------------------------------------------------+
";
char chon;
bool k = true;
do
{ if (k == false)
cout << "Hay chon cong viec theo so tu 1 den 3 :";
else
cout << "Chon cong viec theo so: "; cin >> chon;
k = false;
} while ((chon < '1') || (chon > '7'));
switch (chon) {
case '1': sach_nhap();
break;
case '2':
sach_xoa();
break;
case '3':
sach_xuat();
break;
case '4':
cin.ignore();
cout << "Nhap ma sach can tim : "; cin.getline(ma_tam, 50);
i = sach_tim(ma_tam);
if (i == 0)
cout << "- Khong tim thay sach co ma so tren. ";
else {
cout << "
Da tim thay sach:
";
cout << setw(13) << ".Ma Sach" << setw(13) << "Ten Sach" << setw(13) << "The Loai" << setw(13) << "So Luong" << endl << endl;
cout << setw(13) << lst_book[i].b_ma << setw(13) << lst_book[i].b_tensach << setw(13) << lst_book[i].b_theloai << setw(13) << lst_book[i].b_soluong;
cout << endl;
}
break;
case '5':
sach_hieu_chinh();
break;
case '6':
goto end;
break;
case '7':
sach_them();
break;
}
goto begin;
end:;
}
void interface_main() {
begin:
system(“cls”);
cout << endl << endl;
cout << " SV : PHAM KHAC NGUYEN ____________ MSSV: B1509753
";
cout << " +=================================================+
";
cout << " | CHUONG TRINH QUAN LY SACH |
";
cout << " |-------------------------------------------------|
";
cout << " | |
";
cout << " | 1. Quan ly sach trong kho. |
";
cout << " | |
";
cout << " | |
";
cout << " | 2. Thong ke sach theo the loai. |
";
cout << " | |
";
cout << " | 3. Exit. |
";
cout << " | |
";
cout << " ±------------------------------------------------+
";
char chon;
bool k = true;
do
{ if (k == false)
cout << "Hay chon cong viec theo so tu 1 den 3 :";
else
cout << "Chon cong viec theo so: "; cin >> chon;
k = false;
} while ((chon < '1') || (chon > '7'));
switch (chon) {
case '1':
system("cls");
interface_book_manager();
break;
case '2':
system("cls");
TK_theloai();
break;
case '3':
goto end;
break;
}
goto begin;
end:;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// DINH NGHIA HAM CHO DOI TUONG SACH.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void file_info() {
FILE * f_b;
info[0] = total;
f_b = fopen(“file_info”, “wb”);
fwrite(info, sizeof(info), 1, f_b);
fclose(f_b);
}
void sach_load_file() {
FILE * f_b;
f_b = fopen(“book_file”, “rb”);
fread(lst_book, sizeof(lst_book), 1, f_b);
fclose(f_b);
}
void sach_save_file() {
FILE * f_b;
f_b = fopen(“book_file”, “wb”);
fwrite(lst_book, sizeof(lst_book), 1, f_b);
fclose(f_b);
}
int sach_tim(char ma_tam[]) {
int jump = 1;
while ((jump <= total_book) && (strcmp(lst_book[jump].b_ma, ma_tam) != 0))
jump++;
if (jump > total_book)
return 0;
else
return jump;
}
void sach_nhap() {
char ma_tam[50];
cout << endl;
cout << "NHAP SACH.
";
cout << "Hay nhap tong so luong sach se nhap : "; cin >> total_book;
cout << endl;
for (int i = 1; i <= total_book; i++) {
nhap_ma:;
cin.ignore(1);
cout << ". Ma Sach: "; cin.getline(ma_tam, 50);
if (sach_tim(ma_tam) != 0) {
cout << " Ma sach da ton tai.";
cout << " Nhan Enter de nhap lai !";
goto nhap_ma;
}
cin.ignore(1);
strcpy(lst_book[i].b_ma, ma_tam);
cout << ". Ten Sach : "; cin.getline(lst_book[i].b_tensach, 50);
cout << ". Tac Gia : "; cin.getline(lst_book[i].b_tacgia, 50);
cout << ". The Loai : "; cin.getline(lst_book[i].b_theloai, 50);
cout << ". Nha XB : "; cin.getline(lst_book[i].b_nhaXB, 50);
cout << ". So Luong : "; cin >> lst_book[i].b_soluong;
cin.ignore();
cout << endl;
}
sach_save_file();
file_info();
}
void sach_them() {
char ma_tam[50];
total_book = total_book + 1;
cout << "THEM SACH.
";
nhap_ma:;
cin.ignore(1);
cout << ". Ma Sach: "; cin.getline(ma_tam, 50);
if (sach_tim(ma_tam) != 0) {
cout << " Ma sach da ton tai. Hay nhap lai!";
goto nhap_ma;
}
//cin.ignore(1);
cout << ". Ten Sach : "; cin.getline(lst_book[i].b_tensach, 50);
cout << ". Tac Gia : "; cin.getline(lst_book[i].b_tacgia, 50);
cout << ". The Loai : "; cin.getline(lst_book[i].b_theloai, 50);
cout << ". Nha XB : "; cin.getline(lst_book[i].b_nhaXB, 50);
cout << ". So Luong : "; cin >> lst_book[i].b_soluong;
cout << endl;
sach_save_file();
file_info();
}
void sach_xoa() {
char ma_tam[50];
cin.ignore();
cout << "Nhap ma sach can xoa : "; cin.getline(ma_tam, 50);
i = sach_tim(ma_tam);
if (i == 0)
cout << "- Khong tim thay sach co ma so tren. ";
else {
for (int j = i; j <= total_book; j++)
lst_book[j] = lst_book[j+1];
total_book = total_book - 1;
cout << "- Da xoa.";
}
sach_save_file();
file_info();
}
void sach_xuat() {
sach_load_file();
cout << "
DANH SACH SACH
";
cout << setw(13) << ".Ma Sach" << setw(13) << "Ten Sach" << setw(13) << "The Loai" << setw(13) << "So Luong" << endl << endl;
for(int i = 1; i <= total_book; i++) {
cout << setw(13) << lst_book[i].b_ma << setw(13) << lst_book[i].b_tensach << setw(13) << lst_book[i].b_theloai << setw(13) << lst_book[i].b_soluong;
cout << endl;
}
}
void sach_hieu_chinh() {
char ma_tam[50];
cin.ignore();
cout << "Nhap ma sach can hieu chinh : "; cin.getline(ma_tam, 50);
i = sach_tim(ma_tam);
if (i == 0)
cout << "- Khong tim thay sach co ma so tren. ";
else {
//cin.ignore();
cout << ". Ma Sach : "; cin.getline(lst_book[i].b_ma, 50);
cout << ". Ten Sach : "; cin.getline(lst_book[i].b_tensach, 50);
cout << ". Tac Gia : "; cin.getline(lst_book[i].b_tacgia, 50);
cout << ". The Loai : "; cin.getline(lst_book[i].b_theloai, 50);
cout << ". Nha XB : "; cin.getline(lst_book[i].b_nhaXB, 50);
cout << ". So Luong : "; cin >> lst_book[i].b_soluong;
cout << endl;
}
sach_save_file();
}
void TK_theloai() {
char sach_theloai_tam[50];
p = 0;
cin.ignore(1);
cout << ". Nhap the loai muon thong ke: "; cin.getline(sach_theloai_tam, 50);
cout << endl;
cout << setw(13) << ".Ma Sach" << setw(13) << "Ten Sach" << setw(13) << "The Loai" << setw(13) << "So Luong.";
for (int i = 1; i <= total_book ;i++)
if (strcmp(lst_book[i].b_theloai, sach_theloai_tam) == 0)
{ cout << setw(13) << lst_book[i].b_ma << setw(13) << lst_book[i].b_tensach << setw(13) << lst_book[i].b_theloai << setw(13) << lst_book[i].b_soluong;
p = p + 1;
}
cout << endl << "- Co " << p << " cuon sach co the loai : " << sach_theloai_tam;
if( p == 0)
cout << endl << "- Khong co sach nao thuoc the loai : " << sach_theloai_tam;
}[/code]