|
-
December 16th, 2012, 06:41 AM
#1
Please Help, Very Simple Code, Newbie, Really Frustrated
I have been stuck with this code for the past month.
Its got some logical errors.
Please HELP ME OUT.
I am newbie.
Highly appreciate the effort.
Thanks A Ton!!!
    
Code:
//***************************************************************************
// Header Files
//***************************************************************************
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include<dos.h>
int Bempty,Sempty;
//***************************************************************************
// Class Definitions
//***************************************************************************
class Book
{
int No;
char Name[80];
char Auth[80];
unsigned int Stat;
public:
void Input()
{
cout<<"Book Number: ";cin>>No;
cout<<"Book Name: ";gets(Name);
cout<<"Book Author: ";gets(Auth);
}
void Disp()
{
cout<<"Book Number: "<<No<<endl;
cout<<"Book Name: ";puts(Name);
cout<<"Book Author: ";puts(Auth);
(Stat==1)?cout<<"Book Status: Issued.":cout<<"Book Status: Available"<<endl;
}
int BNo() {return No;}
int BStat() {return Stat;}
char* BName() {return Name;}
char* BAuth() {return Auth;}
void Issue(){Stat=1;}
void Depo(){Stat=0;}
Book(){Stat=0;}
};
class Student
{
int Admno;
char Name[10];
unsigned int Stat;
public:
int SAdmno() {return Admno;}
int SStat() {return Stat;}
char* SName() {return Name;}
void Input()
{
cout<<"Student Admission Number: ";cin>>Admno;
cout<<"Student Name: ";gets(Name);
}
void Disp()
{
cout<<"Student Admission Number: "<<Admno<<endl;
cout<<"Student Name: ";puts(Name);
(Stat==1)?cout<<"Book Status: Issued.":cout<<"Book Status: Not Issued."<<endl;
}
void Issue(){Stat=1;}
void Depo(){Stat=0;}
Student(){Stat=0;}
};
Book temporarybook;
Student temporarystud;
//***************************************************************************
// Creating,Editing, Deleting Records
//***************************************************************************
//Handling The Book Records
void BookFile()
{
fstream BookFile;
Book temp;
cout<<"Select one of the following options:\n";
cout<<"1.Create new Record.\n2.Modify existing Record.\n3.Delete existing Record.\n";
int choice;cin>>choice;
switch(choice)
{
case 1:
{
clrscr();
char choice1;
BookFile.open("BookList.dat",ios::app|ios::binary);
do
{
cout<<"Enter the details for the Book.\n";
temp.Input();
BookFile.write((char*) &(temp),sizeof(temp));
cout<<"Book Record created Successfully.\n";getch();Bempty++;
cout<<"More Records?";
cin>>choice1;
}while ((choice1=='y') || (choice1=='Y'));
BookFile.close();
break;
}
case 2:
{
clrscr();
if(Bempty==0)
{
gotoxy(20,12);
cout<<"\nNo Book Records Present.";
getch();
break;
}
char choice1;
BookFile.open("BookList.dat",ios::app|ios::in|ios::binary);
do
{
cout<<"Please input the Book Number.";
int data;cin>>data;
int found=0;
BookFile.seekg(0,ios::beg);
while(BookFile.read((char*) &temp,sizeof(temp)) && (!found))
if(temp.BNo()==data)
found++;
if(found)
{
BookFile.seekg(-sizeof(temp),ios::cur);
cout<<"Enter the details for the Book.\n";
temp.Input();
BookFile.write((char*) &temp,sizeof(temp));
cout<<"Book Record has been modified Successfully.\n";getch();
}
else cout<<"Record Not Found!\n";
cout<<"More records?";
cin>>choice1;
}while((choice1=='y') || (choice1=='Y'));
BookFile.close();
break;
}
case 3:
{
int bfound=0;
clrscr();
if(Bempty==0)
{
gotoxy(20,12);
cout<<"\nNo Book Records Present.";
getch();
break;
}
char choice1;
do
{
cout<<"Enter Book No of the Book Record to be deleted.";
int Bno;cin>>Bno;
fstream TempFile;
TempFile.open("BTemp.dat",ios::out|ios::binary);
BookFile.open("BookList.dat",ios::in|ios::binary);
while (BookFile.read((char*) &temp,sizeof(temp)))
{
if(temp.BNo()!=Bno)
TempFile.write((char*) &temp,sizeof(temp));
else bfound++;
}
BookFile.close();
TempFile.close();
if(bfound) {cout<<"The Book Record has been deleted successfully.\n";getch();}
else {cout<<"The Book Record was not found.\n";getch();break;}
remove("BookList.dat");
rename("BTemp.dat","BookList.dat");
cout<<"\nBook Record has been deleted successfully.\n";getch();Bempty--;
cout<<"More Records?";cin>>choice1;
}while((choice1=='y')||(choice=='Y'));
break;
}
default: {cout<<"Wrong choice entered.\n";getch();}
}
}
//Handling The Student Records
void StudentFile()
{
fstream StudentFile;
Student temp;
cout<<"Select one of the following options:\n";
cout<<"1.Create new Record.\n2.Modify existing Record.\n3.Delete existing Record.\n";
int choice;cin>>choice;
switch(choice)
{
case 1:
{
clrscr();
char choice1;
StudentFile.open("StudentList.dat",ios::app|ios::binary);
do
{
cout<<"Enter the details for the Student.\n";
temp.Input();
StudentFile.write((char*) &(temp),sizeof(temp));
cout<<"Record added successfully.\n";getch();Sempty++;
cout<<"More Records?";
cin>>choice1;
}while((choice1=='y') || (choice1=='Y'));
StudentFile.close();
break;
}
case 2:
{
clrscr();
if (Sempty==0)
{
cout<<"\nNo Student Records present!";
getch();
break;
}
char choice1;
do
{
StudentFile.open("StudentList.dat",ios::app|ios::in|ios::binary);
cout<<"Enter the Student's Admission Number.";
int data;cin>>data;
int found=0;
StudentFile.seekg(0,ios::beg);
while( (!found) && (StudentFile.read((char*) &temp,sizeof(temp))) )
if(temp.SAdmno()==data)
found++;
if(found)
{
temp.Disp();
StudentFile.seekg(-sizeof(temp),ios::cur);
cout<<"Enter the new details for the Student.\n";
temp.Input();
StudentFile.write((char*) &temp,sizeof(temp));
cout<<"Record has been modified successfully.\n";getch();
}
else cout<<"Record Not Found!\n";
cout<<"More Records?";
cin>>choice1;
StudentFile.close();
}while((choice1=='y') || (choice1=='Y'));
break;
}
case 3:
{
clrscr();
if (Sempty==0)
{
cout<<"\nNo Student Records present!";
getch();
break;
}
char choice1;
int sfound=0;
do
{
cout<<"Enter Admission No of Student Record to be deleted.";
int Ano;cin>>Ano;
fstream TempFile;
TempFile.open("STemp.dat",ios::out|ios::binary);
StudentFile.open("StudentList.dat",ios::in|ios::binary);
while(StudentFile.read((char*) &temp,sizeof(temp)))
{
if(temp.SAdmno()!=Ano)
TempFile.write((char*) &temp,sizeof(temp));
else sfound++;
}
StudentFile.close();
TempFile.close();
if (sfound)
{cout<<"The Student Record has been successfully deleted.\n";getch();Sempty--;}
else {cout<<"The Student Record was not found.\n";getch();break;}
remove("StudentList.dat");
rename("STemp.dat","StudentList.dat");
cout<<"More Records?";
cin>>choice1;
}while((choice1=='y') || (choice=='Y'));
break;
}
default: {cout<<"Wrong choice entered.\n";getch();}
}
}
//***************************************************************************
// Splash Screen
//***************************************************************************
void Splash()
{
clrscr();
gotoxy(30,12);
cout<<"W";delay(50);
cout<<"e";delay(50);
cout<<"l";delay(50);
cout<<"c";delay(50);
cout<<"o";delay(50);
cout<<"m";delay(50);
cout<<"e";delay(50);
cout<<" ";delay(50);
cout<<"T";delay(50);
cout<<"o";delay(50);
cout<<" ";delay(50);
cout<<"T";delay(50);
cout<<"h";delay(50);
cout<<"e";delay(50);
cout<<" ";delay(50);
cout<<"L";delay(50);
cout<<"i";delay(50);
cout<<"b";delay(50);
cout<<"r";delay(50);
cout<<"a";delay(50);
cout<<"r";delay(50);
cout<<"y";delay(50);
getch();
}
//***************************************************************************
// Searching and Displaying Records
//***************************************************************************
//Searching Book Record
Book BookSearch()
{
if(Bempty==0)
{
gotoxy(20,12);
cout<<"\nNo Book Records Present.";
getch();
return temporarybook;
}
fstream BookFile;
BookFile.open("BookList.dat",ios::in|ios::binary);
Book temp;
int found=0;
cout<<"Choose the search parameter:\n1.Name\n2.Author\n3.Book Number\n";
int choice;cin>>choice;
switch(choice)
{
case 1:
{
cout<<"\nEnter Book Name.";char data[80];gets(data);
while ((BookFile.read((char*) &temp,sizeof(temp))) && (!found))
{
if (strcmpi(temp.BName(),data)==0)
{
found++;
}
if (found)
{
cout<<"Book Record Found.";
temp.Disp();
getch();
}
else cout<<"Book Record Not Found!!";
}break;
}
case 2:
{
cout<<"\nEnter Author's Name.";char data[80];gets(data);
while(BookFile.read((char*) &temp,sizeof(temp)) && (!found))
{
if (strcmpi(temp.BAuth(),data)==0)
{
found++;
}
if (found)
{
cout<<"Book Record Found.";
temp.Disp();
getch();
}
else cout<<"Book Record Not Found!!";
}break;
}
case 3:
{
cout<<"\nEnter Book Number.";int data;cin>>data;
while(BookFile.read((char*) &temp,sizeof(temp))&&(!found))
{
if (temp.BNo()==data)
{
found++;
}
if (found)
{
cout<<"Book Record Found.";
temp.Disp();
getch();
}
else cout<<"Book Record Not Found!!";
}
}break;
}
BookFile.close();
return temp;
}
//Searching Student Record
Student StudentSearch()
{
if(Sempty==0)
{
gotoxy(20,12);
cout<<"\nNo Student Records Present.";
getch();
return temporarystud;
}
fstream StudentFile;
StudentFile.open("StudentList.dat",ios::in|ios::binary);
Student temp;
int found=0;
cout<<"Choose the search parameter:\n1.Name\n2.Admission Number\n";
int choice;cin>>choice;
switch(choice)
{
case 1:
{
cout<<"\nEnter Student Name.";char data[80];gets(data);
while(StudentFile.read((char*) &temp,sizeof(temp))&&(!found))
{
if(strcmpi(temp.SName(),data)==0)
{
found++;
cout<<"Student Record Found.";
temp.Disp();
getch();
}
else cout<<"Student Record Not Found!!";
}
break;
}
case 2:
{
cout<<"\nEnter Admission Number.";int data;cin>>data;
while(StudentFile.read((char*) &temp,sizeof(temp))&&(!found))
{
if(temp.SAdmno()==data)
{
found++;
cout<<"Student Record Found.";
temp.Disp();
getch();
}
else cout<<"Student Record Not Found!!";
}
break;
}
}
StudentFile.close();
return temp;
}
//***************************************************************************
// Displaying Entire Records
//***************************************************************************
//Displaying Book Records
void Book_All()
{
if(Bempty==0)
{
gotoxy(20,12);
cout<<"\nNo Book Records Present.";
getch();
return;
}
fstream BookFile;
clrscr();
BookFile.open("BookList.dat",ios::in|ios::binary);
Book temp;
char BStat[10];
cout<<setw(10)<<"Book No "<<setw(25)<<"Book Name"<<setw(25)<<"Book Author"<<setw(10)<<" Book Status"<<endl<<endl;
while(BookFile.read((char*) &temp,sizeof(temp)))
{
if(temp.BStat()==1) strcpy(BStat,"Issued"); else strcpy(BStat," Not Issued");
cout<<setw(10)<<temp.BNo()<<setw(25)<<temp.BName()<<setw(25)<<temp.BAuth()<<" "<<setw(10)<<BStat<<endl<<endl;
}
BookFile.close();
getch();
}
//Displaying Student Records
void Student_All()
{
if(Sempty==0)
{
gotoxy(20,12);
cout<<"\nNo Student Records Present.";
getch();
return;
}
fstream StudentFile;
clrscr();
StudentFile.open("StudentList.dat",ios::in|ios::binary);
Student temp;
char SStat[10];
cout<<setw(10)<<"Admission No"<<setw(40)<<"Student Name"<<setw(10)<<" Book Issued"<<endl<<endl;
while(StudentFile.read((char*) &temp,sizeof(temp)))
{
if(temp.SStat()==1)
strcpy(SStat," Issued"); else strcpy(SStat," Not Issued");
cout<<setw(10)<<temp.SAdmno()<<setw(40)<<temp.SName()<<" "<<setw(10)<<SStat<<endl<<endl;
}
StudentFile.close();
getch();
}
//***************************************************************************
// Book Issue And Deposit
//***************************************************************************
void Book_Issue()
{
if(Bempty==0)
{
gotoxy(20,12);
cout<<"\nNo Book Records Present.";
delay(200);
cout<<"Please contact the Librarian for more details.\n";
getch();
return;
}
if(Sempty==0)
{
gotoxy(20,12);
cout<<"\nNo Student Records Present.";
delay(200);
cout<<"Please contact the Librarian for more details.\n";
getch();
return;
}
fstream BookFile,StudentFile;
BookFile.open("BookList.dat",ios::out|ios::in|ios::binary);
StudentFile.open("StudentList.dat",ios::out|ios::in|ios::binary);
Student Issuer,Stemp;
Issuer=temporarystud;
Book Issued,Btemp;
Issued=temporarybook;
Issuer=StudentSearch();
if (strcpy(Issuer.SName(),temporarystud.SName())==0)
{
cout<<"The Student Record was not found.\nPlease contact the Librarian.";
return;
}
Issued=BookSearch();
if ( strcpy( Issued.BName(),temporarybook.BName() )==0 )
{
cout<<"The Book Record was not found.\nPlease contact the Librarian.";
return;
}
int Sdata=Issuer.SAdmno();
int Bdata=Issued.BNo();
int Bfound=0,Sfound=0;
StudentFile.seekg(0,ios::beg);
BookFile.seekg(0,ios::beg);
while ( (!Sfound) && (StudentFile.read((char*) &Stemp,sizeof(Stemp))) )
if (Stemp.SAdmno()==Sdata)
Sfound++;
while ( (!Bfound) && (BookFile.read((char*) &Btemp,sizeof(Btemp))) )
if (Btemp.BNo()==Bdata)
Bfound++;
if (Bfound)
{
BookFile.seekg(-sizeof(Btemp),ios::cur);
Btemp.Depo();
BookFile.write((char*) &Btemp,sizeof(Btemp));
}
else cout<<"Book Record Not Found!\n";
if (Sfound)
{
StudentFile.seekg(-sizeof(Stemp),ios::cur);
Stemp.Depo();
StudentFile.write((char*) &Stemp,sizeof(Stemp));
}
else cout<<"Student Record Not Found!\n";
StudentFile.close();
BookFile.close();
if (Bfound)
cout<<"\nThe Book has been successfully issued.";
cout<<"\nPlease return it in the stipulated time period.Thank You!!\n";
getch();
}
void Book_Deposit()
{
if(Bempty==0)
{
gotoxy(20,12);
cout<<"\nNo Book Records Present.";
delay(200);
cout<<"Please contact the Librarian for more details.\n";
getch();
return;
}
if(Sempty==0)
{
gotoxy(20,12);
cout<<"\nNo Student Records Present.";
delay(200);
cout<<"Please contact the Librarian for more details.\n";
getch();
return;
}
fstream BookFile,StudentFile;
BookFile.open("BookList.dat",ios::out|ios::in|ios::binary);
StudentFile.open("StudentList.dat",ios::out|ios::in|ios::binary);
Student Issuer,Stemp;
Book Issued,Btemp;
Issuer=temporarystud;
Issued=temporarybook;
Issuer=StudentSearch();
if ( strcpy(Issuer.SName(),temporarystud.SName())==0 )
{
cout<<"The Student Record was not found.\nPlease contact the Librarian.";
return;
}
Issued=BookSearch();
if ( strcpy(Issued.BName(),temporarybook.BName())==0 )
{
cout<<"The Book Record was not found.\nPlease contact the Librarian.";
return;
}
int Sdata=Issuer.SAdmno();
int Bdata=Issued.BNo();
int Bfound=0,Sfound=0;
StudentFile.seekg(0,ios::beg);
BookFile.seekg(0,ios::beg);
while ( (!Sfound) && (StudentFile.read((char*) &Stemp,sizeof(Stemp))) )
if (Stemp.SAdmno()==Sdata)
Sfound++;
while ( (!Bfound) && (BookFile.read((char*) &Btemp,sizeof(Btemp))) )
if (Btemp.BNo()==Bdata)
Bfound++;
if (Bfound)
{
BookFile.seekg(-sizeof(Btemp),ios::cur);
Btemp.Depo();
BookFile.write((char*) &Btemp,sizeof(Btemp));
}
else cout<<"Book Record Not Found!\n";
if (Sfound)
{
StudentFile.seekg(-sizeof(Stemp),ios::cur);
Stemp.Depo();
StudentFile.write((char*) &Stemp,sizeof(Stemp));
}
else cout<<"Student Record Not Found!\n";
StudentFile.close();
BookFile.close();
if (Bfound)
cout<<"The book has been returned.Thank You!!"<<endl;
getch();
}
//***************************************************************************
// Counting Number Of Records
//***************************************************************************
void BookNumbers()
{
fstream BookFile;
Book Btemp;
BookFile.open("BookList.dat",ios::in|ios::binary);
while (BookFile.read((char*) &Btemp,sizeof(Btemp)))
Bempty++;
}
void StudentNumbers()
{
fstream StudentFile;
Student Stemp;
StudentFile.open("StudentList.dat",ios::in|ios::binary);
while (StudentFile.read((char*) &Stemp,sizeof(Stemp)))
Sempty++;
}
//***************************************************************************
// Administrator Function
//***************************************************************************
void Admin()
{
int selection;
Book Btemp;
Student Stemp;
do
{
clrscr();
cout<<"Select an option:\n";
cout<<"1.Manage Student Records.\n2.Display Single Student Record.\n3.Display All Student Records.\n";
cout<<"4.Manage Book Records.\n5.Display Single Book Record.\n6.Display All Book Records.\n";
cout<<"7.Go back to previous menu.\n";
cin>>selection;
switch(selection)
{
case 1 : {StudentFile();break;}
case 2 : {Stemp=StudentSearch();Stemp.Disp();break;}
case 3 : {Student_All();break;}
case 4 : {BookFile();break;}
case 5 : {Btemp=BookSearch();Btemp.Disp();break;}
case 6 : {Book_All();break;}
case 7 : {cout<<"Going back!\n";getch();break;}
default : {cout<<"Wrong choice entered in Admin.Please try again.\n";}
}
}while(selection!=7);
}
//***************************************************************************
// Main Function
//***************************************************************************
void main()
{
int user;
// Splash();
BookNumbers();
StudentNumbers();
do
{
clrscr();
cout<<"Please select one of the following options:\n";
cout<<"1.Issue a Book.\n2.Deposit a Book.\n3.Administrator Functions.\n4.Exit.\n";
cin>>user;
clrscr();
switch(user)
{
case 1: {Book_Issue();break; }
case 2: {Book_Deposit();break;}
case 3: {Admin();break;}
case 4: {gotoxy(20,12);cout<<"Thanks for using the Library.See you again Soon!\n";break;}
default:{gotoxy(20,12);cout<<"Wrong choice in Main.Please enter again.\n";getch();}
}
}while(user!=4);
getch();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|