#include <cstdlib>
#include <conio.h>
#include <fstream>
#include <sys\stat.h>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
struct Student_Record
{
string name;
int id;
string ic;
string parentName;
string address;
int telNum, parentTelNum;
bool deleted;
};
Student_Record Student;
#define STUDENT_NAME "Student.txt"
Student_Record Employee;
char choice;
fstream *fs = NULL, *fs1 = NULL;
bool deletion = false;
void closeFile(fstream *);
bool isFileExist(const char *);
int main()
{
while (true)
{
do
{
system( "cls" );
cout << "\n < Employees Database > \n\n";
cout << "(1) Add new student record. \n";
cout << "(2) Modify an existing student record\n";
cout << "(3) Delete an existing student record \n";
cout << "(4) Display a students record \n";
cout << "(6) Calculate the fee of the student\n";
cout << "(7) Exit \n\n";
cout << " Enter a choice (1-5) : " << flush;
choice = getch();
} while ( choice < '1' || choice > '5');
system( "cls" );
if (choice == '2' || choice == '3' || choice == '4')
{
if (!isFileExist(STUDENT_NAME))
{
cout << "\n Database file ('" << STUDENT_NAME << "') doesn't exist, then there are no records." << endl;
system("pause");
continue;
}
}
switch ( choice )
{
int recs_num;
int id;
case '1' :
string name;
cout << "\n\t\t < Entering a new record > ";
cout << "\n Enter the following informations for the new record : " << endl;
cout << "Name : ";
cin >> name;
cout << "\n ID number : ";
cin >> id;
cout << "\n IC number : ";
cin >> Student.ic;
cout << "Telephone number : ";
cin >> Student.telNum;
cout << "Parent's name : ";
cin >> Student.parentName;
cout << "Parent's telephone number : ";
cin >> Student.parentTelNum;
cout << "Mailing address : ";
cin >> Student.address;
Student.deleted = 0;
fs = new fstream( STUDENT_NAME, ios::out | ios::app | ios::binary );
if (!fs)
{
cout << "\n Can't open or create '" << STUDENT_NAME << "' file" << endl;
system("pause");
break;
}
fs->write( (char *) &Student, sizeof(Student) );
closeFile(fs);
cout << "\n Record added." << endl;
system("pause");
break;
case '2' :
cout << "\n Enter employee ID, that you want modify its informatin : ";
cin >> id;
fs = new fstream( STUDENT_NAME, ios::in | ios::out | ios::binary );
if (!fs)
{
cout << "\n Can't open or create '" << STUDENT_NAME << "' file" << endl;
system("pause");
break;
}
recs_num = -1;
while (fs->read( (char *) &Student, sizeof(Student) ))
{
recs_num++;
if ( Student.id == id && !Student.deleted)
break;
}
if (fs->eof())
{
cout << "\n Your specified employee doesn't exist in file." << endl;
closeFile(fs);
system("pause");
break;
}
cout << "\n Enter new informations for this record : ";
cout << "Name :";
cin >> Student.name;
cout << "\n ID number : ";
cin >> Student.id;
cout << "\n IC number :";
cin >> Student.ic;
cout << "Telephone number :";
cin >> Student.telNum;
cout << "Parent's name :";
cin >> Student.parentName;
cout << "Parent's telephone number :";
cin >> Student.parentTelNum;
cout << "Mailing address :";
cin >> Student.address;
fs->seekp ( sizeof(Student) * recs_num, ios::beg );
fs->write( (char *) &Student, sizeof(Student) );
closeFile(fs);
cout << "\n Record is modified." << endl;
system("pause");
break;
case '3' :
cout << "\n Enter student's ID, for deletion : ";
cin >> id;
fs = new fstream(STUDENT_NAME, ios::in | ios::out | ios::binary );
if (!fs)
{
cout << "\n Can't open or create '" << STUDENT_NAME << "' file." << endl;
system("pause");
break;
}
recs_num = -1;
while (fs->read( (char *) &Student, sizeof(Student) ))
{
recs_num++;
if ( Student.id == id && !Student.deleted )
break;
}
if (fs->eof())
{
cout << "\n Your specified employee doesn't exist in database file." << endl;
closeFile(fs);
system("pause");
break;
}
Student.deleted = 1;
fs->seekp ( sizeof(Student) * recs_num, ios::beg );
fs->write( (char *) &Student, sizeof(Student) );
closeFile(fs);
deletion = true;
cout << "\n Record is deleted." << endl;
system("pause");
break;
case '4' :
fs = new fstream( STUDENT_NAME, ios::in | ios::binary );
if (!fs)
{
cout << "\n Can't open or create '" << STUDENT_NAME << "' file." << endl;
system("pause");
break;
}
cout << "\n\t\t < Students Details >\n\n";
cout << "ID\tAGE\tDEPT\tMONTHLY_RATE\tSTART_DATE\n"
<< "-------------------------------------------------- \n";
while (fs->read( (char *) &Employee, sizeof(Employee) )) // display records
{
if ( Employee.id != !Employee.deleted )
{
cout << Student.id << '\t';
cout << Student. ic << '\t';
cout << Student.name << '\t';
cout << Student. telNum << "\t\t";
cout << Student. parentName << '\n';
cout << Student. parentTelNum << '\n' ;
cout << Student. address << "\t\t";
}
}
cout << "\n To see Hourly records, "; system("pause");
closeFile(fs);
system( "cls" );
fs = new fstream( STUDENT_NAME, ios::in | ios::binary );
if (!fs)
{
cout << "\n Can't open or create '" << STUDENT_NAME << "' file." << endl;
system("pause");
break;
}
cout << "\n\t\t\t < Hourly > \n\n";
cout << "ID\tAGE\tDEPT\tRATE_PER_HOUR\tREG_HOURS\tOVERTIME_HOURS\n"
<< "---------------------------------------------------------------------- \n";
while ( fs->read( (char *) &Student, sizeof(Student_Record) ) )
{
}
cout << "\n To see menu, "; system("pause");
closeFile(fs);
break;
case '5' :
if (deletion)
{
cout << "\n Updating '" << STUDENT_NAME << "' File..." << endl;
fs = new fstream( STUDENT_NAME, ios::in | ios::binary );
if (!fs)
{
cout << "\n Can't open '" << STUDENT_NAME << "' file, then Updating is incomplete." << endl;
system("pause");
system( "cls" );
return 1;
}
fs1 = new fstream( "temp", ios::out | ios::binary);
if (!fs1)
{
cout << "\n Can't create temp file, then Updating is incomplete." << endl;
system("pause");
closeFile(fs);
system( "cls" );
return 1;
}
while (fs->read( (char *) &Student, sizeof(Student) ))
if ( !Student.deleted )
fs1->write( (char *) &Student, sizeof(Student) );
closeFile(fs);
closeFile(fs1);
if( remove( STUDENT_NAME ) == -1 )
{
cout << "\n Can't delete '" << STUDENT_NAME << "' file, then Updating is incomplete." << endl;
system("pause");
system( "cls" );
return 1;
}
struct stat st;
int res = stat( "temp", &st );
if (st.st_size == 0)
remove( "temp" );
else
if ( rename ("temp", STUDENT_NAME) )
{
cout << "\n Can't rename temp file, then Updating is incomplete." << endl;
system("pause");
system( "cls" );
return 1;
}
cout << "\n Updating database file completed." << endl;
system("pause");
}
system( "cls" );
return 0;
break;
}
}
return 0;
}
void closeFile(fstream *fs)
{
fs->close();
delete fs;
fs = NULL;
}
bool isFileExist(const char * file_name)
{
struct stat st;
int res = stat( file_name, &st );
return (res == 0);
}
i know it's kinda long, but hope you guys could help...
these codes said that the initialization of 'name' is skipped by case label...
how to solve??