hello every one
am new here and really confused with my C++ subject. i saw a few post here was deleted or not answered. i hope my one wont go without answer.
now here is my problem.
I REALLY DUNNO what am doing. i was able to write the program like this but the errors coming up and i cannot fix.
Please help me with fixing this one and if u have other suggestions to make it better plz it will be real helpful.
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <fstream>
#include <sys\stat.h>
using namespace std;
void main();
struct Book_Record;
{ int Book_TYPE;
union
{
struct
{
char Book_title;
char Book_category;
int Book_date; // added in librery
char Book_author;
char Book_publisher;
int YEAR_PUBLISHED;
};
char title;
char category;
char author;
int date;
char publisher;
int YEAR;
bool deleted;
};
#define Book_FILE_NAME "Book.txt"
Book_Record Book;
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\n < Book Record Database > \n\n";
cout << "(1) Add a new Book Record \n";
cout << "(2) Modify an existing Book Record\n";
cout << "(3) Delete an existing Book Record \n";
cout << "(4) Display Book Records \n";
cout << "(5) Quit \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(Book_FILE_NAME))
{
cout << "\n Database file ('" << Book_FILE_NAME << "') doesn't exist, then there are no records." << endl;
system("pause");
continue;
}
}
switch ( choice )
{
int recs_num;
int Book_TYPE;
case '1' :
cout << "\n\t\t < Entering a new Book record > ";
cout << "\n Enter the following informations for the new record : ";
cout << "\n\n Book_TYPE : ";
cin >> Book.Book_TYPE;
cout << "\n Title of the Book : ";
cin >> Book.Book_TITLE;
cout << "\n Catagory of the Book : ";
cin >> Book.Book_Catagory;
cout << "\n Year Published : ";
cin >> Book.YEAR_PUBLISHED;
cout <<"\n Author of the Book : ";
cin >>Book.author;
cout <<"\n Publisher of the Book : ";
cin >>Book.publisher;
cout <<"\n Addwd to library ( Date) :" ;
cin >>Book.date;
}
Book.deleted = 0;
fs = new fstream( Book_FILE_NAME, ios::out | ios::app | ios::binary );
if (!fs)
{
cout << "\n ERROR: Cannot Access or Open '" << Book_FILE_NAME << "' file" << endl;
system("pause");
break;
}
fs->write( (char *) &Book, sizeof(Book) );
closeFile(fs);
cout << "\n Book Record added successfully." << endl;
system("pause");
break;
case '2' :
cout << "\n Enter the Title of the Book that you want modify : ";
cin >> Book.Book_TITLE;
fs = new fstream( Book_FILE_NAME, ios::in | ios::out | ios::binary );
if (!fs)
{
cout << "\n ERROR: Cannot Access or Open '" << Book_FILE_NAME << "' file" << endl;
system("pause");
break;
}
recs_num = -1;
while (fs->read( (char *) &Book, sizeof(Book) ))
{
recs_num++;
if ( Book.Book_TITLE == Newest && !Book.deleted)
break;
}
if (fs->eof())
{
cout << "\n The Book file cannot be found." << endl;
closeFile(fs);
system("pause");
break;
}
cout << "\n Enter new information again : ";
cout << "\n\n Book_TYPE : ";
cin >> Book.Book_TYPE;
cout << "\n Title of the Book : ";
cin >> Book.Book_TITLE;
cout << "\n Catagory of the Book : ";
cin >> Book.Book_Catagory;
cout << "\n Year Published : ";
cin >> Book.YEAR_PUBLISHED;
cout <<"\n Author of the Book : ";
cin >>Book.author;
cout <<"\n Publisher of the Book : ";
cin >>Book.publisher;
cout <<"\n Addwd to library ( Date) :" ;
cin >>Book.date;
fs->seekp ( sizeof(Book) * recs_num, ios::beg );
fs->write( (char *) &Book, sizeof(Book) );
closeFile(fs);
cout << "\n The Book Record has been successfully modified." << endl;
system("pause");
break;
case '3' :
cout << "\n Enter the Book you want to delete : ";
cin >> Book.Book_TITLE;
fs = new fstream( Book_FILE_NAME, ios::in | ios::out | ios::binary );
if (!fs)
{
cout << "\n ERROR: Cannot Access or Open '" << Book_FILE_NAME << "' file." << endl;
system("pause");
break;
}
recs_num = -1;
while (fs->read( (char *) &Book, sizeof(Book) ))
{
recs_num++;
if ( Book.Book_TITLE == Book_TYPE && !Book.deleted )
break;
}
if (fs->eof())
{
cout << "\n The Book that you are finding is not in the database file." << endl;
closeFile(fs);
system("pause");
break;
}
Book.deleted = 1;
fs->seekp ( sizeof(Book) * recs_num, ios::beg );
fs->write( (char *) &Book, sizeof(Book) );
closeFile(fs);
deletion = true;
cout << "\n Book Record has bees successfully deleted." << endl;
system("pause");
break;
case '4' :
fs = new fstream( Book_FILE_NAME, ios::in | ios::binary );
if (!fs)
{
cout << "\n ERROR: Cannot Access or Open '" << Book_FILE_NAME << "' file." << endl;
system("pause");
break;
}
cout << "Book_TITLE\tBook_GENRE\tYEAR_PUBLISHED\n"
<< "-------------------------------------------------- \n";
while (fs->read( (char *) &Book, sizeof(Book) ))
{
if ( Book.Book_TYPE == Newest && !Book.deleted )
{
cout << Book.title << '\t';
cout << Book.category << '\t';
cout << Book.date << '\t' ;
cout << Book.author << '\t';
cout << Book.publisher << '\t';
cout << Book.YEAR_PUBLISHED <<'\t';
}
}
closeFile(fs);
fs = new fstream( Book_FILE_NAME, ios::in | ios::binary );
if (!fs)
{
cout << "\n ERROR: Cannot Access or Open '" << Book_FILE_NAME << "' file." << endl;
system("pause");
break;
}
}
cout << "\n To go back to the main menu, "; system("pause");
closeFile(fs);
break;
case '5' :
if (deletion)
{
cout << "\n Updating '" << Book_FILE_NAME << "' File..." << endl;
fs = new fstream( Book_FILE_NAME, ios::in | ios::binary );
if (!fs)
{
cout << "\n ERROR: Cannot Access or Open '" << Book_FILE_NAME << "' file, Update failed." << endl;
system("pause");
system( "cls" );
return 1;
}
fs1 = new fstream( "temp", ios::out | ios::binary);
if (!fs1)
{
cout << "\n Cannot create temp file, Update failed." << endl;
system("pause");
closeFile(fs);
system( "cls" );
return 1;
}
while (fs->read( (char *) &Book, sizeof(Book) ))
if ( !Book.deleted )
fs1->write( (char *) &Book, sizeof(Book) );
closeFile(fs);
closeFile(fs1);
if( remove( Book_FILE_NAME ) == -1 )
{
cout << "\n ERROR: Cannot delete the Book '" << Book_FILE_NAME << "' file, Update failed." << 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", Book_FILE_NAME) )
{
cout << "\n ERROR: Cannot rename temp file, Update failed." << endl;
system("pause");
system( "cls" );
return 1;
}
cout << "\n Update completed." << endl;
system("pause");
}
system( "cls" );
return 0;
break ;
}
}
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);
}
And am real sorry if i break any rule of the forum. hope some one can help me