我需要关于以下代码中肯定存在的大量不良实践的反馈:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip.h>
//***************************************************************
// CLASSES USED IN PROJECT
//****************************************************************
class book{
char bno[6];
char bookName[50]; //title of book
char authName[20]; //author of book
public:
void create_book(){
cout<<"\nNEW BOOK ENTRY...\n";
cout<< "\nEnter Book Number : ";
cin>>bno;
cout<< "\n\nEnter Name of the Book : ";
gets(bookName);
cout<<"\n\nEnter Author's name : ";
gets(authName);
cout<<"\n\n\nBook Created...";
}
void show_book(){
cout<<"\nBook number : " << bno;
cout<<"\nBook Name : ";
puts(bookName);
cout<<"Author Name : ";
puts(authName);
}
void modify_book(){
cout<<"\nBook number : " << bno;
cout<<"\nModify Book Name : ";
gets(bookName);
cout<<"\nModify Author's Name : ";
gets(authName);
}
char* retbno(){ return bno; }
void report(){cout<<bno<<setw(30)<<bookName<<setw(30)<<authName<<endl;}
}; //end of class book
class student{
private:
char admno[6]; //Admission number
char name[20]; //Name of the student
char stbno[6]; //Student Book number
int token;
public:
void create_student(){
clrscr();
cout<<"\nNEW STUDENT ENTRY...\n";
cout<<"\nEnter Admission number : ";
cin>>admno;
cout << "\n\nEnter Name of Student : ";
gets(name);
token = 0;
stbno[0] = '/0';
cout<<"\n\nStudent Record Created..";
}
void show_student(){
cout<<"\nAdmission number : "<<admno;
cout<<"\nStudent Name : ";
puts(name);
cout<<"\nNumber of Book(s) issued : "<<token;
if( token == 1 ) cout<<"\nBook No "<<stbno;
}
void modify_student(){
cout<<"\nAdmission number : "<<admno;
cout<<"\nModify Student Name : ";
gets(name);
}
char* retadmno(){ return admno ; }
char* retstbno(){ return stbno ; }
int rettoken(){ return token ; }
void addtoken(){ token = 1; }
void resettoken(){ token = 0; }
void getstbno(char t[]){ strcpy(stbno,t); }
void report(){cout <<"\t"<<admno<<setw(20)<<name<<setw(10)<<token<<endl;}
};//end of class student
//***************************************************************
// Global declaration for stream object, object
//****************************************************************
fstream f, f1;
book b;
student s;
//***************************************************************
// Functions to write record to file
//****************************************************************
void write_book(){
char ch;
f.open("book.dat", ios::out|ios::app);
do{
clrscr();
b.create_book();
f.write((char*)&b, sizeof(book));
cout<<"\n\nDo you want to add more books...(y/n?)";
cin>>ch;
}while( ch == 'y'|| ch == 'Y' );
f.close();
}
void write_student(){
char ch;
f.open("student.dat", ios::out|ios::app);
do{
s.create_student();
f.write((char*)&st, sizeof(student));
cout<<"\n\nDo you want to add more students...(y/n?)";
cin>>ch;
}while(ch=='y'||ch=='Y');
f.close();
}
//***************************************************************
// Function to read specific record from file
//****************************************************************
void display_spb(char n[]){
cout<<"\nBOOK DETAILS\n";
int flag = 0;
f.open("book.dat", ios::in);
while(f.read((char*)&b, sizeof(book))){
if( strcmpi(b.retbno(),n) == 0 ){
b.show_book();
flag = 1;
}
}
f.close();
if(flag==0) cout<<"\n\nBook does not exist";
getch();
}
void display_sps(char n[]){
cout<<"\nSTUDENT DETAILS\n";
int flag = 0;
f.open("student.dat", ios::in);
while(f.read((char*)&s, sizeof(student))){
if((strcmpi(s.retadmno(),n)==0)){
s.show_student();
flag = 1;
}
}
f.close();
if( flag == 0 ) cout<<"\n\nStudent does not exist";
getch();
}
//***************************************************************
// Functions to modify records of file
//****************************************************************
void modify_book(){
char n[6];
int found = 0;
clrscr();
cout<<"\n\n\tMODIFY BOOK REOCORD.... ";
cout<<"\n\n\tEnter The book no. of The book";
cin>>n;
f.open("book.dat", ios::in|ios::out);
while(f.read((char*)&b, sizeof(book)) && found == 0){
if(strcmpi(b.retbno(),n) == 0){
b.show_book();
cout<<"\nEnter New Details of Book"<<endl;
b.modify_book();
int pos = -1 * sizeof(b);
f.seekp(pos,ios::cur);
f.write((char*)&b, sizeof(book));
cout<<"\n\n\t Record Updated";
found = 1;
}
}
f.close();
if( found == 0 ) cout<<"\n\n Record Not Found ";
getch();
}
void modify_student(){
char n[6];
int found = 0;
clrscr();
cout<<"\n\n\tMODIFY STUDENT RECORD... ";
cout<<"\n\n\tEnter Admission number of Student";
cin >> n;
f.open("student.dat", ios::in|ios::out);
while( f.read((char*)&s, sizeof(student)) && found == 0 ){
if(strcmpi(s.retadmno(),n) == 0){
s.show_student();
cout<<"\nEnter The New Details of student"<<endl;
s.modify_student();
int pos = -1 * sizeof(s);
f.seekp(pos, ios::cur);
f.write((char*)&s, sizeof(student));
cout<<"\n\n\t Record Updated";
found = 1;
}
}
f.close();
if( found == 0 ) cout<<"\n\n Record Not Found ";
getch();
}
//***************************************************************
// Function to Delete record of file
//****************************************************************
void delete_student(){
char n[6];
int flag = 0;
clrscr();
cout<<"\n\n\n\tDELETE STUDENT...";
cout<<"\n\nEnter The admission no. of the Student You Want To Delete : ";
cin>>n;
f.open("student.dat", ios::in|ios::out);
fstream f2;
f2.open("Temp.dat", ios::out);
f.seekg(0, ios::beg);
while(f.read((char*)&s, sizeof(student))){
if(strcmpi(s.retadmno(), n) != 0) f2.write((char*)&s, sizeof(student));
else flag = 1;
}
f2.close();
f.close();
remove("student.dat");
rename("Temp.dat", "student.dat");
if( flag == 1 ) cout<<"\n\n\tRecord Deleted ..";
else cout<<"\n\nRecord not found";
getch();
}
void delete_book(){
char n[6];
clrscr();
cout<<"\n\n\n\tDELETE BOOK ...";
cout<<"\n\nEnter Book number of Book to Delete : ";
cin >> n;
f.open("book.dat", ios::in|ios::out);
fstream f2;
f2.open("Temp.dat", ios::out);
f.seekg(0, ios::beg);
while( f.read((char*)&b, sizeof(book)) ) if(strcmpi(b.retbno(),n)!=0) f2.write((char*)&b, sizeof(book));
f2.close();
f.close();
remove("book.dat");
rename("Temp.dat","book.dat");
cout<<"\n\n\tRecord Deleted ..";
getch();
}
//***************************************************************
// Function to display all students list
//****************************************************************
void display_alls(){
clrscr();
f.open("student.dat",ios::in);
if(!f){
cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
getch();
return;
}
cout<<"\n\n\t\tSTUDENT LIST\n\n";
cout<<"==================================================================\n";
cout<<"\tAdmission No."<<setw(10)<<"Name"<<setw(20)<<"Book Issued\n";
cout<<"==================================================================\n";
while(f.read((char*)&s, sizeof(student))) s.report();
f.close();
getch();
}
//***************************************************************
// Function to display Books list
//****************************************************************
void display_allb(){
clrscr();
f.open("book.dat", ios::in);
if(!f){
cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
getch();
return;
}
cout<<"\n\n\t\tBook LIST\n\n";
cout<<"=========================================================================\n";
cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author\n";
cout<<"=========================================================================\n";
while(f.read((char*)&b, sizeof(book))) b.report();
f.close();
getch();
}
//***************************************************************
// function to issue book
//****************************************************************
void book_issue(){
char sn[6], bn[6];
int found = 0, flag = 0;
clrscr();
cout<<"\n\nBOOK ISSUE ...";
cout<<"\n\n\tEnter Student's Admission number : ";
cin>>sn;
f.open("student.dat", ios::in|ios::out);
f1.open("book.dat", ios::in|ios::out);
while(f.read((char*)&s, sizeof(student)) && found == 0){
if(strcmpi(s.retadmno(), sn) == 0){
found = 1;
if(s.rettoken() == 0){
cout<<"\n\n\tEnter Book number : ";
cin>>bn;
while(f1.read((char*)&b, sizeof(book)) && flag==0){
if(strcmpi(b.retbno(), bn) == 0){
b.show_book();
flag = 1;
s.addtoken();
s.getstbno(b.retbno());
int pos = -1 * sizeof(s);
f.seekp(pos, ios::cur);
f.write((char*)&s, sizeof(student));
cout<<"\n\n\t Book issued successfully\n\nPlease Note: Write the current date in backside of your book \n and submit within 15 days fine Rs. 1 for each day \n after 15 days period";
}
}
if( flag == 0 ) cout<<"Book number does not exist";
}
else cout<<"You have not returned the last book ";
}
}
if(found == 0) cout<<"Student record not exist...";
getch();
f.close();
f1.close();
}
//***************************************************************
// Function to deposit book
//****************************************************************
void book_deposit()
{
char sn[6],bn[6];
int found = 0, flag = 0, day, fine;
clrscr();
cout<<"\n\nBOOK DEPOSIT ...";
cout<<"\n\n\tEnter Student’s Admission number : ";
cin>>sn;
f.open("student.dat", ios::in|ios::out);
f1.open("book.dat", ios::in|ios::out);
while(f.read((char*)&s, sizeof(student)) && found == 0){
if(strcmpi(s.retadmno(), sn)==0){
found = 1;
if(s.rettoken() == 1){
while(f1.read((char*)&b, sizeof(book))&& flag==0){
if(strcmpi(b.retbno(), s.retstbno())==0){
b.show_book();
flag = 1;
cout<<"\n\nBook deposited in no. of days : ";
cin>>day;
if(day > 15){
fine = (day-15) * 1;
cout<<"\n\nFine to deposited is Rs. "<<fine;
}
s.resettoken();
int pos = -1 * sizeof(s);
f.seekp(pos,ios::cur);
f.write((char*)&s, sizeof(student));
cout<<"\n\n\t Book deposited successfully";
}
}
if(flag==0)cout<<"Book no does not exist";
}
else cout<<"No book is issued..please check!!";
}
}
if(found==0) cout<<"Student record not exist...";
getch();
f.close();
f1.close();
}
//***************************************************************
// INTRODUCTION FUNCTION
//****************************************************************
void intro(){
clrscr();
gotoxy(35,11);
cout<<"LIBRARY";
gotoxy(35,14);
cout<<"MANAGEMENT";
gotoxy(35,17);
cout<<"SYSTEM";
cout<<"\n\nMADE BY : Nikhil Kartha";
getch();
}
//***************************************************************
// ADMINISTRATOR MENU FUNCTION
//****************************************************************
void admin_menu(){
clrscr();
int ch2;
cout<<"\n\n\n\tADMINISTRATOR MENU";
cout<<"\n\n\t(1) CREATE STUDENT RECORD";
cout<<"\n\n\t(2) DISPLAY ALL STUDENTS RECORD";
cout<<"\n\n\t(3) DISPLAY SPECIFIC STUDENT RECORD ";
cout<<"\n\n\t(4) MODIFY STUDENT RECORD";
cout<<"\n\n\t(5) DELETE STUDENT RECORD";
cout<<"\n\n\t(6) CREATE BOOK ";
cout<<"\n\n\t(7) DISPLAY ALL BOOKS ";
cout<<"\n\n\t(8) DISPLAY SPECIFIC BOOK ";
cout<<"\n\n\t(9) MODIFY BOOK ";
cout<<"\n\n\t(10) DELETE BOOK ";
cout<<"\n\n\t(11) BACK TO MAIN MENU";
cout<<"\n\n\tPlease Enter Your Choice (1-11) ";
cin>>ch2;
switch(ch2){
case 1:
clrscr();
write_student();
break;
case 2:
display_alls();
break;
case 3:
char num[6];
clrscr();
cout<<"\n\n\tPlease Enter The Admission No. ";
cin>>num;
display_sps(num);
break;
case 4:
modify_student();
break;
case 5:
delete_student();
break;
case 6:
clrscr();
write_book();
break;
case 7:
display_allb();
break;
case 8:
char num[6];
clrscr();
cout<<"\n\n\tPlease Enter The book No. ";
cin>>num;
display_spb(num);
break;
case 9:
modify_book();
break;
case 10:
delete_book();
break;
case 11: return;
default: cout<<"\a";
}
admin_menu(); //recursion
}
//***************************************************************
// THE MAIN FUNCTION OF PROGRAM
//****************************************************************
void main(){
char ch;
intro();
do{
clrscr();
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t(1) BOOK ISSUE";
cout<<"\n\n\t(2) BOOK DEPOSIT";
cout<<"\n\n\t(3) ADMINISTRATOR MENU";
cout<<"\n\n\t(4) EXIT";
cout<<"\n\n\tPlease Select Your Option (1-4) ";
ch = getche();
switch(ch){
case '1':
clrscr();
book_issue();
break;
case '2':
book_deposit();
break;
case '3':
admin_menu();
break;
case '4': exit(0);
default : cout<<"\a";
}//end of switch
}while(ch!='4');
}
//***************************************************************
// END OF PROJECT
//***************************************************************这段代码是用Turbo C++编写的。
发布于 2014-11-03 07:25:50
代码非常长,需要一段时间才能完全检查它。然而,需要立即更新以下内容:
char类型(例如:char bookName[50])替换为std::string。std::getline而不是gets。#include <fstream>)std和其他适当的命名空间(例如:std::cout而不是cout)。std::string而不是C风格的字符串操作。这适用于你的整个程序。这将避免代码中的许多问题(溢出和相关问题)。实际上,您可以使用任何现代在线C++编译器(GCC/ in 2013)编译您的代码,并且代码中会出现许多警告/错误。您应该首先修复这些问题,然后将上述建议结合起来。
对于推荐的实践/建议,您可能希望遵循ISOCpp常见问题部分。它们包含了很好的代码片段信息,这将有助于理解任何概念。
除此之外,您可能还需要参考Bjarne入门书C++之旅 on现代C++语言。它包含了关于现代C++的非常简洁和优秀的信息。
https://codereview.stackexchange.com/questions/68713
复制相似问题