hello
am doing a library program. i am almost done with it. but now my problem is i have to make the record update section work as each of the record can be updated separately.

so far i think of making another menu and do it like that. now my question is
1. i already have ("CLS") and ("cls") ( both of them but i do not know whats the difference) will this cls duo make any problem for a 3rd one .
2. i am going to put a new menu under another menu's case . ( i do not see any problem with it ) but will it make any problem.

i added my display and update part as code here . ( did not add the full code because its more than 500 line now )

the beginning of the code is like this

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <fstream>
#include <string>
#include <ctime>
#include <sys/stat.h>
using namespace std;

struct Book_Record
{
	int ID;
	string price,requestor,call,invoice,supplier,copies,total,published;							
	string date,isbn;					
	string name,author,category,publisher;
	bool deleted;
	};

#define BOOK_FILE_NAME "Books.txt"
Book_Record Book;
int Auto = 100;
fstream *fs = NULL, *fs1 = NULL;// file streams for files : fs -> 'add', fs1 -> 'temp'
bool deletion = false;

void closeFile(fstream *); // closes a file with its pointer, then deletes the file pointer
bool isFileExist(const char *); // check if a file exists
 

int main()	
{
	ifstream input;
	input.open("id.txt"),ios::in;
	input >> Auto;
	ofstream output;
	int tries = 3;
	string acc,password,MainPage;

	cout << "\n\n\n";
        cout << "\t\t***                                             ***" <<endl;
	cout << "\t\t***                  Welcome                    ***" <<endl;
	cout << "\t\t***                    To                       ***" <<endl;
	cout << "\t\t***          Library Book Registration          ***" <<endl;
	cout << "\t\t***                  Program                    ***" <<endl;
	cout << "\t\t***                                             ***" <<endl;
	cout << "\n\n\n";
	{
	while (tries)
	{
	cout <<"Please Enter ID: \a";
	cin >> acc;
	cout <<"Please Enter Password: ";
	cin >> password;

	system("CLS");

	if (acc == "abc" && password =="123")
	{
		goto MainPage;
	}
	else 
	{
		cout << "Invalid Id or Password. Please enter again" <<endl;
		cout << "The Program Will terminate After 3 Unsuccessful Try" <<endl;
		tries--;
	}
	}
	return -1;
}
	
	
	
	
	return 0;
MainPage:
	system("CLS");

	int num;
{
		enum num { Add = 1, Search = 2, Update = 3, Delete = 4, Display = 5, Total = 6, Exit = 7};
}
		do
	{
	system( "CLS" );
	cout << "\n\n\n";
 
	cout << "\t***    Welcome to Librery Book Register System          ***" <<endl;
	cout << "\t***                                                     ***" <<endl;
	cout << "\t***          1. Add Record                              ***" <<endl; 
	cout << "\t***          2. Search Record                           ***" <<endl;
	cout << "\t***          3. Update Record                           ***" <<endl;
	cout << "\t***          4. Delete Record                           ***" <<endl;
	cout << "\t***          5. Display Records                         ***" <<endl;
	cout << "\t***          6. Total number of Books                   ***" <<endl;
	cout << "\t***          7. Exit Program                            ***" <<endl; 

	cout <<"\n\n\n\n"<<flush;
	cout<<"please select one: ";
	num= _getch();
	}

	while ( num < '1' || num > '7' );

	system ( "CLS" );

	if (num == '2' || num == '3' || num == '4' || num == '5' || num == '6' )

	{ 
		if (!isFileExist (BOOK_FILE_NAME))

		{
			cout<<"Can't open or create '"<<BOOK_FILE_NAME<<"'file"<<endl;
			system("pause");
			goto MainPage;
		}
	}

switch(num)

	{
		int confirm;
		int recs_num;
		int id;
	case '1':

and the display part is like this

fs = new fstream ( BOOK_FILE_NAME, ios::in | ios::binary );
		if (!fs)
		{
			cout << "Can not open" <<BOOK_FILE_NAME<< "file." << endl;
			system("pause");
			break;
		}
		while (fs->read( (char *) &Book, sizeof(Book) ))
		{
			if ( !Book.deleted )
			{
					cout << "\n\n\n\n";
					cout << "Book's ID\t\t: " << Book.ID << '\n';
					cout << "Book's Name\t\t: " << Book.name << '\n';
					cout << "Book's Author\t\t: " << Book.author << '\n';
					cout << "Book's Publisher\t: " << Book.publisher << '\n';
					cout << "Book's Category\t\t: " << Book.category << '\n';
					cout << "Year Publisher\t\t: " << Book.publisher << '\n';
					cout << "Book's ISBN\t\t: " << Book.isbn << '\n';
					cout << "Price Of book\t\t: " << Book.price << '\n';
					cout << "Requstor of Book\t: " << Book.requestor << '\n';
					cout << "Book in Collum No\t: " << Book.call << '\n';
					cout << "Book's Invoice No\t: " << Book.invoice << '\n';
					cout << "Supplier for The Book\t: " << Book.supplier << '\n';
					cout << "Number of Copies\t: " << Book.copies << '\n';
					cout << "Added Date\t\t: " << Book.date << '\n';
					cout << "\n\n\n\n";
			}
		}
		closeFile(fs);
		system ("pause");
		goto MainPage;
		
		break;

and the update part.

cout << "\n\nPlease enter BOOK's ID: " ;
			cin>>id;
			fs= new fstream ( BOOK_FILE_NAME, ios::in | ios::out | ios::binary);
			if (!fs)
				{
					cout << "\nCan not open or create "<<BOOK_FILE_NAME<<"File"<< endl;
					system("pause");

					break;
		}

		recs_num= -1;

		while (fs->read( (char*) &Book, sizeof(Book) ))
		{
			recs_num++;
	
			if ( Book.ID == id && !Book.deleted)

				break;
		}
		if (fs->eof())
		{
			cout << "\nBook Does Not Exist in The Librery" << endl;
			closeFile(fs);
			system("pause");
			goto MainPage;
		}
			cout << "\n Enter Book's NAME: " ;
			cin.get();
			getline (cin,Book.name);
			while (Book.name.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n* Invalid input! Please enter again!*\nNAME: " ;
				getline (cin,Book.name);
			}

////// so on as the records/////////

cout << "\nHow Many Copies of Book(example 033): " ;
			cin.get();
			getline (cin,Book.copies);
			while (Book.copies.find_first_of("1234567890") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nCopies(example 033): " ;
				getline (cin,Book.copies);
			}

//		char date[10];
//		_strdate(date);
		Book.date = date;
			

			fs->seekp ( sizeof(Book) * recs_num, ios::beg );
			fs->write( (char*) &Book, sizeof(Book) );

			closeFile(fs);
			
			cout << "\nRecord is modified."<< endl;
			system("pause");
			
			goto MainPage;
			
			break;

>>1. i already have ("CLS") and ("cls") ( both of them but i do not know whats the difference) .

CLS and cls are the exact same thing. MS-Windows does not distingish between cases line *nix does.

>>will this cls duo make any problem for a 3rd one
what 3d one? All it does is erase everything on the screen.

all rite i got it know but does any one care to read a code that is about 550 line . and help me with 50 lines in it? because i do not want to do the exact same thing i have been doing here . and thats spamming with code and getting no ans.

OMG I hope you don't have all that code in one function main() :icon_eek:

You could post it as an attachment, but don't post something that you may regret later because DaniWeb won't delete it.

Why allocate with new the stream objects? Waste of space and time. Just declare the object ifstream stream; The object is just a few bytes so there's no point in calling new to allocate it.

Remove those gotos -- they are not needed and you instructor may give you a lower grade for using them.

Edited 5 Years Ago by Ancient Dragon: n/a

i uploaded the code am working on. but now i have a strange problem.
when i add a record i miss all the 1st letters ( like if i input author : anyone it shows nyone) all the records show the same but for some unknown reason name donot show like that.

and
this update is full update i need some thing that i can update the records one by one as taking from menu.

Attachments
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <fstream>
#include <string>
#include <ctime>
#include <sys/stat.h>
using namespace std;

struct Book_Record
{
	int ID;
	string price,requestor,call,invoice,supplier,copies,total,published;							
	string date,isbn;					
	string name,author,category,publisher;
	bool deleted;
	};

#define BOOK_FILE_NAME "Books.txt"
Book_Record Book;
int Auto = 100;
fstream *fs = NULL, *fs1 = NULL;// file streams for files : fs -> 'add', fs1 -> 'temp'
bool deletion = false;

void closeFile(fstream *); // closes a file with its pointer, then deletes the file pointer
bool isFileExist(const char *); // check if a file exists
 

int main()	
{
	ifstream input;
	input.open("id.txt"),ios::in;
	input >> Auto;
	ofstream output;
	int tries = 3;
	string acc,password,MainPage;

	cout << "\n\n\n";
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***                                             ***" <<endl;
	cout << "\t\t***                  Welcome                    ***" <<endl;
	cout << "\t\t***                    To                       ***" <<endl;
	cout << "\t\t***          Library Book Registration          ***" <<endl;
	cout << "\t\t***                  Program                    ***" <<endl;
	cout << "\t\t***                                             ***" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\n\n\n";
	{
	while (tries)
	{
	cout <<"Please Enter ID: \a";
	cin >> acc;
	cout <<"Please Enter Password: ";
	cin >> password;

	system("CLS");

	if (acc == "abc" && password =="123")
	{
		goto MainPage;
	}
	else 
	{
		cout << "Invalid Id or Password. Please enter again" <<endl;
		cout << "The Program Will terminate After 3 Unsuccessful Try" <<endl;
		tries--;
	}
	}
	return -1;
}
	
	
	
	
	return 0;
MainPage:
	system("CLS");

	int num;
{
		enum num { Add = 1, Search = 2, Update = 3, Delete = 4, Display = 5, Total = 6, Exit = 7};
}
		do
	{
	system( "CLS" );
	cout << "\n\n\n";
	cout << "\t***********************************************************" <<endl;
	cout << "\t***********************************************************" <<endl;
	cout << "\t***                                                     ***" <<endl; 
	cout << "\t***    Welcome to Librery Book Register System          ***" <<endl;
	cout << "\t***                                                     ***" <<endl;
	cout << "\t***          1. Add Record                              ***" <<endl; 
	cout << "\t***          2. Search Record                           ***" <<endl;
	cout << "\t***          3. Update Record                           ***" <<endl;
	cout << "\t***          4. Delete Record                           ***" <<endl;
	cout << "\t***          5. Display Records                         ***" <<endl;
	cout << "\t***          6. Total number of Books                   ***" <<endl;
	cout << "\t***          7. Exit Program                            ***" <<endl; 
	cout << "\t***                                                     ***" <<endl; 
	cout << "\t***********************************************************" <<endl;
	cout << "\t***********************************************************" <<endl;
	cout <<"\n\n\n\n"<<flush;
	cout<<"please select one: ";
	num= _getch();
	}

	while ( num < '1' || num > '7' );

	system ( "CLS" );

	if (num == '2' || num == '3' || num == '4' || num == '5' || num == '6' )

	{ 
		if (!isFileExist (BOOK_FILE_NAME))

		{
			cout<<"Can't open or create '"<<BOOK_FILE_NAME<<"'file"<<endl;
			system("pause");
			goto MainPage;
		}
	}

switch(num)

	{
		int confirm;
		int recs_num;
		int id;
	case '1':		
	cout << "\n\n\n";
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***                Add new records                      ***" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;

			cout << "\n\nBook's ID<AutoGenerate>: ";
			Book.ID = Auto++;
			cout << Book.ID << endl;

			if (output.open("id.txt"),ios::out)
			{
				output<<Auto;
			}
			else 
				cout << "The file is empty." ;
				output.close();

			cout << "\nEnter Book's NAME: " ;
			cin.get();
			getline (cin,Book.name);
			while (Book.name.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n* Invalid input! Please enter again!*\nNAME: ";
				getline (cin,Book.name);
			}

			cout << "\nBook Author: " ;
			cin.get();
			getline (cin,Book.author);

			while (Book.author.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nAuthor: " ;
				getline (cin,Book.author);
			}
			cout << "\nBook's Publisher: " ;
			cin.get();
			getline (cin,Book.publisher);

			while (Book.publisher.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nPublisher: " ;
				getline (cin,Book.publisher);
			}
			cout << "\nBook's Category: " ;
			cin.get();
			getline (cin,Book.category);
			
			cout << "\nYear Book was Published: " ;
			cin.get();
			getline (cin,Book.published);
			while ( (Book.published.size() !=4 ) || (Book.published.find_first_of( "0123456789" ) == string::npos) )
          {
            cout << "Invalid input! Please Input the year! *\nPublished: " ;
            getline (cin,Book.published);
            }
			cout << "\nBook's ISBN: " ;
			cin.get();
			getline (cin,Book.isbn);
			while (Book.isbn.find_first_of("1234567890") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nISBN: " ;
				getline (cin,Book.isbn);
			}
			
			cout << "\nPrice Of Book (RM) : " ;
			cin.get();
			getline (cin,Book.price);
			while (Book.price.find_first_of("0123456789") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nPrice: " ;
				getline (cin,Book.price);
			}
			cout << "\nRequestor : " ;
			cin.get();
			getline (cin,Book.requestor);
			while (Book.requestor.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nRequestor : " ;
				getline (cin,Book.requestor);
			}
			cout << "\nBook in Collum no : " ;
			cin.get();
			getline (cin,Book.call);
			while (Book.call.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nCollum : " ;
				getline (cin,Book.call);
			}
			cout << "\nInvoice no :" ;
			cin.get();
			getline (cin,Book.invoice);
			while (Book.invoice.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nInvoice no : " ;
				getline (cin,Book.invoice);
			}

			cout << "\nSupplier: " ;
			cin.get();
			getline (cin,Book.supplier);
			while (Book.supplier.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << "\n *Invalid input!please enter again! *\nSupplier : " ;
				getline (cin,Book.supplier);
			}

			cout << "\nHow Many Copies of Book(example 033): " ;
			cin.get();
			getline (cin,Book.copies);
			while (Book.copies.find_first_of("1234567890") == string::npos)
			{
				cout << "\n*Invalid input!please enter again! *\nCopies(example 033): " ;
				getline (cin,Book.copies);
			}
		char date [10];
		_strdate(date);
		Book.date = date;
			
	cout << "\nSave record?\n<1=yes, 0=no>" << endl;
	cin>>confirm;

	if (confirm == 1)
	{
		fs = new fstream (BOOK_FILE_NAME, ios::out | ios::app | ios::binary);
		if (!fs)
		{
			cout<<"\n Can't open or create'"<<BOOK_FILE_NAME<<"'file"<<endl;
			system("pause");
			break;
		}
		fs->write ( (char *) &Book, sizeof(Book));
		closeFile(fs);
	
		cout << "\nRecord Added." << endl;
		system("pause");
		goto MainPage;
		}
		cout << "\nRecord rolled back." << endl;
		system ("pause");

		goto MainPage;
	
	break;
	
	
	case '2':
	cout << "\n\n\n";
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***                Search Books                         ***" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;

		
		cout << "\n\nplease enter Book's ID: " ;
		cin >>id;

		fs= new fstream ( BOOK_FILE_NAME, ios::in | ios::out | ios::binary);
		
		if (!fs)
		{
			cout<<"\nCan not open or create "<<BOOK_FILE_NAME<<"File"<<endl;
			system("pause");
			break;
		}
		recs_num= -1;

				while (fs->read( (char*) &Book, sizeof(Book) ))
		{
			recs_num++;

			if ( Book.ID == id && !Book.deleted)
				break;
		}
		if (fs->eof())
		{
			cout << "\nBook Does not Exist in the Librery" << endl;
			closeFile(fs);
			system("pause");
			goto MainPage;
		}
					cout << "\n\n\n";
					cout << "Book's ID\t\t: " << Book.ID << '\n';
					cout << "Book's Name\t\t: " << Book.name << '\n';
					cout << "Book's Author\t\t: " << Book.author << '\n';
					cout << "Book's Publisher\t: " << Book.publisher << '\n';
					cout << "Book's Category\t\t: " << Book.category << '\n';
					cout << "Year Published\t\t: " << Book.published << '\n';
					cout << "Book's ISBN\t\

OMG I hope you don't have all that code in one function main() :icon_eek:

You could post it as an attachment, but don't post something that you may regret later because DaniWeb won't delete it.

Why allocate with new the stream objects? Waste of space and time. Just declare the object ifstream stream; The object is just a few bytes so there's no point in calling new to allocate it.

Remove those gotos -- they are not needed and you instructor may give you a lower grade for using them.

i nailed it rite. i have all the code in int main(). just one main.

You need to learn to organize your programs a lot better than that. What will you do with a program that contains millions of lines of code? Put them all in main()??? Of course not. You need to learn to write functions that perform specific tasks. For example if you have a switch statement that contains 5 cases then you will want to write 5 functions, one for each case, and then call those functions from within the switch statement.

I briefly looked at the code you posted and, to be honest, it needs a major overhaul so that its more understandable and better formatted. I can see why you are having so much trouble with that program.

You need to learn to organize your programs a lot better than that. What will you do with a program that contains millions of lines of code? Put them all in main()??? Of course not. You need to learn to write functions that perform specific tasks. For example if you have a switch statement that contains 5 cases then you will want to write 5 functions, one for each case, and then call those functions from within the switch statement.

I briefly looked at the code you posted and, to be honest, it needs a major overhaul so that its more understandable and better formatted. I can see why you are having so much trouble with that program.

emp e;

void addEmp(); //adds new record to data file
void disEmp(); //displays all records from file
void delEmp(); //asks empNo and removes that record from file
void updEmp(); //asks empno and lets you reenter that data
void cls(); //just clear screen

int main()
{
	
	string dummy;
	int choice = -1;

	while(choice != 0)
	{
		cls();
		cout<<"*********************"<<endl;
		cout<<"===== Main Menu ====="<<endl;
		cout<<"*********************"<<endl;
		cout<<endl<<endl;
		cout<<"1 -> Add Record "<<endl;
		cout<<"2 -> Display Record"<<endl;
		cout<<"3 -> Delete Record"<<endl;
		cout<<"4 -> Update Record"<<endl;
		cout<<endl;
		cout<<"0 -> Exit"<<endl;
		cout<<endl<<endl<<"Selection ";
		cin>>choice;
		if(choice<0 || choice>4)
		{
			cls();
		}
		switch(choice)
		{
		case 0:    break;
		case 1:    addEmp();    break;
		case 2:    disEmp();    break;
		case 3:    delEmp();    break;
		case 4:    updEmp();    break;
		}

		cout<<"\nPress any key to continue...";
		getch();
	}
}

u mean like this. but my problem is if i input this one i have to go through the whole program and i donot want to do that.

now can u tell me that my code can take only upto few letters then if it goes over that letter the program closes.
can u tell me do i have any problem with the display and search one . because when i input a long data . my program takes the data but when i want to display it makes the error.

>>but my problem is if i input this one i have to go through the whole program and i donot want to do that

Yes, I mean like the code you posted. If you don't want to do that then I don't want to help you. I'm not going to waste my time trying to figure out a program that has 550+ lines all in the same function.

Edited 5 Years Ago by Ancient Dragon: n/a

>>but my problem is if i input this one i have to go through the whole program and i donot want to do that

Yes, I mean like the code you posted. If you don't want to do that then I don't want to help you. I'm not going to waste my time trying to figure out a program that has 550+ lines all in the same function.

i mean now my program got only problem that i cannot input some data thats long.

and now the program has 950+ lines ( i figured how to update one by one) :D

can u tell me whats the code name of calculation all the books in the program ?

Add the code as an attachment to your post. We don't want to scroll through all 1,000 lines of code. Just use the Advanced Editor, scroll down until you see the Manage Attachments button then from that you can upload the *.cpp file.

this one getting all over me now. i was looking at the code for hours and hours still cannot get anything. even if i add restriction size at 50 ( for name it cannot show ).when i add a record no matter how big it is it can show. then when i close the program and run again it cannot show a large entry.

please any one tell me whats wrong .

and am really a new programer when i want to do the case code as function i cannot add the 2nd menu . so please tell me what can make my program go smooth with large entrys

Attachments
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <fstream>
#include <string>
#include <ctime>
#include <sys/stat.h>
using namespace std;

struct Book_Record
{
	int ID;
	string price,requestor,call,invoice,supplier,copies,total,published;							
	string date,isbn;					
	string name,author,category,publisher;
	bool deleted;
	};

#define BOOK_FILE_NAME "Books.txt"
Book_Record Book;
int Auto = 100;
fstream *fs = NULL, *fs1 = NULL;// file streams for files : fs -> 'add', fs1 -> 'temp'
bool deletion = false;

void closeFile(fstream *); // closes a file with its pointer, then deletes the file pointer
bool isFileExist(const char *); // check if a file exists
 

int main()	
{
	ifstream input;
	input.open("id.txt"),ios::in;
	input >> Auto;
	ofstream output;
	int tries = 3;
	string acc,password,MainPage,dummy;

	cout << "\n\n\n";
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***                                             ***" <<endl;
	cout << "\t\t***                  Welcome                    ***" <<endl;
	cout << "\t\t***                    To                       ***" <<endl;
	cout << "\t\t***          Library Book Registration          ***" <<endl;
	cout << "\t\t***                  Program                    ***" <<endl;
	cout << "\t\t***                                             ***" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\n\n\n";
	{
	while (tries)
	{
	cout <<"Please Enter ID: \a";
	cin >> acc;
	cout <<"Please Enter Password: ";
	cin >> password;

	system("CLS");

	if (acc == ("abc") && password == ("123"))
	{
		goto MainPage;
	}
	else 
	{
		cout << "Invalid Id or Password. Please enter again" <<endl;
		cout << "The Program Will terminate After (" <<tries<< "\n" << ") Unsuccessful Try" <<endl;
		tries--;
	}
	}
	return -1;
}
	
	
	
	
	return 0;
MainPage:
	system("CLS");

	int num;
{
		enum num { Add = 1, Search = 2, Update = 3, Delete = 4, Display = 5, Total = 6, Exit = 7};
}
		do
	{
	system( "CLS" );
	cout << "\n\n\n";
	cout << "\t***********************************************************" <<endl;
	cout << "\t***********************************************************" <<endl;
	cout << "\t***                                                     ***" <<endl; 
	cout << "\t***    Welcome to Librery Book Register System          ***" <<endl;
	cout << "\t***                                                     ***" <<endl;
	cout << "\t***          1. Add Record                              ***" <<endl; 
	cout << "\t***          2. Search Record                           ***" <<endl;
	cout << "\t***          3. Update Record                           ***" <<endl;
	cout << "\t***          4. Delete Record                           ***" <<endl;
	cout << "\t***          5. Display Records                         ***" <<endl;
	cout << "\t***          6. Exit Program                            ***" <<endl;
	cout << "\t***                                                     ***" <<endl; 
	cout << "\t***********************************************************" <<endl;
	cout << "\t***********************************************************" <<endl;
	cout <<"\n\n\n\n"<<flush;
	cout<<"please select one: ";
	num= _getch();
	}

	while ( num < '1' || num > '6' );

	system ( "CLS" );

	if (num == '2' || num == '3' || num == '4' || num == '5' )

	{ 
		if (!isFileExist (BOOK_FILE_NAME))

		{
			cout<<"Can't open or create '"<<BOOK_FILE_NAME<<"'file"<<endl;
			system("pause");
			goto MainPage;
		}
	}

switch(num)

	{
		int confirm;
		int recs_num;
		int id;
		
	case '1':		
	cout << "\n\n\n";
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***                Add new records                      ***" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;

			cout << "\n\nBook's ID<AutoGenerate>: ";
			Book.ID = Auto++;
			cout << Book.ID << endl;

			if (output.open("id.txt"),ios::out)
			{
				output<<Auto;
			}
			else 
				cout << "The file is empty.";
				output.close();

			cout << "Enter Book's NAME: ";
			getline(cin,dummy);
			getline (cin,Book.name);
			while (Book.name.find_first_of("qwertyuiopasdfghjklzxcvbnm1234567890-+") == string::npos)
			{
				cout << "Invalid input! Please enter again! NAME: ";
				getline (cin,Book.name);
			}

			cout << "Book Author: ";
			getline (cin,Book.author);
			while (Book.author.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos) 
			{
				cout << "Invalid input!please enter again! Author: ";
				getline (cin,Book.author);
			}
			cout << "Book's Publisher: ";
			getline (cin,Book.publisher);
			while (Book.publisher.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << " Invalid input!please enter again! Publisher: ";
				getline (cin,Book.publisher);
			}
			
			cout << "Book's Category: ";
			getline (cin,Book.category);
			while (Book.category.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << " Invalid input! Please enter again! Category: ";
				getline (cin,Book.category);  
			}


			cout << "Requestor : ";
			getline (cin,Book.requestor);
			while (Book.requestor.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << " Invalid input!please enter again! Requestor : ";
				getline (cin,Book.requestor);
			}

			cout << "Book in Call no : ";
			getline (cin,Book.call);
			while (Book.call.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << " Invalid input!please enter again! Call No : ";
				getline (cin,Book.call);
			}

			cout << "Invoice no : ";
			getline (cin,Book.invoice);
			while (Book.invoice.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << " Invalid input!please enter again! Invoice no : ";
				getline (cin,Book.invoice);
			}

			cout << "Supplier: ";
			getline (cin,Book.supplier);
			while (Book.supplier.find_first_of("qwertyuiopasdfghjklzxcvbnm") == string::npos)
			{
				cout << " Invalid input!please enter again! Supplier : ";
				getline (cin,Book.supplier);
			}
						
			cout << "Book's ISBN: ";
			getline (cin,Book.isbn);
			while (Book.isbn.find_first_of("1234567890") == string::npos)
			{
				cout << " Invalid input!please enter again! ISBN: ";
				getline (cin,Book.isbn);
			}

			cout << "Price Of Book (RM) : ";
			getline (cin,Book.price);
			while (Book.price.find_first_of("1234567890") == string::npos)
			{
				cout << " Invalid input!please enter again! Price: ";
				getline (cin,Book.price);
			}

			cout << "Year Book was Published: ";
			getline (cin,Book.published);
			while ( Book.published.size() !=4 || (Book.published.find_first_of( "1234567890" ) == string::npos))
          {
            cout << " Invalid input! Please Input the year! Published: ";
            getline (cin,Book.published);
            }

			cout << "How Many Copies of Book(example 033): ";
			getline (cin,Book.copies);
			while (Book.copies.find_first_of("1234567890") == string::npos)
			{
				cout << " Invalid input!please enter again! Copies(example 033): ";
				getline (cin,Book.copies);
			}
		char date [10];
		_strdate(date);
		Book.date = date;
			
	cout << "\nSave record?\n<1=yes, 0=no>" << endl;
	cin>>confirm;

	if (confirm == 1)
	{
		fs = new fstream (BOOK_FILE_NAME, ios::out | ios::app | ios::binary);
		if (!fs)
		{
			cout<<"\nCan't open or create'"<<BOOK_FILE_NAME<<"'file"<<endl;
			system("pause");
			break;
		}
		fs->write ( (char *) &Book, sizeof(Book));
		closeFile(fs);
	
		cout << "\nRecord Added." << endl;
		system("pause");
		goto MainPage;
		}
		cout << "\nRecord rolled back." << endl;
		system ("pause");

		goto MainPage;
	
	break;
	
	
	case '2':
	cout << "\n\n\n";
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***                Search Books                         ***" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;

		
		cout << "\n\nplease enter Book's ID: " ;
		cin >>id;

		fs= new fstream ( BOOK_FILE_NAME, ios::in | ios::out | ios::binary);
		
		if (!fs)
		{
			cout<<"\nCan not open or create "<<BOOK_FILE_NAME<<"File"<<endl;
			system("pause");
			break;
		}
		recs_num= -1;

				while (fs->read( (char*) &Book, sizeof(Book) ))
		{
			recs_num++;

			if ( Book.ID == id && !Book.deleted)
				break;
		}
		if (fs->eof())
		{
			cout << "\nBook Does not Exist in the Librery" << endl;
			closeFile(fs);
			system("pause");
			goto MainPage;
		}
		cout << "\n\n\n";
		cout << "Book's ID\t\t: " << Book.ID << '\n';
		cout << "Book's Name\t\t: " << Book.name << '\n';
		cout << "Book's Author\t\t: " << Book.author << '\n';
		cout << "Book's Publisher\t: " << Book.publisher << '\n';
		cout << "Book's Category\t\t: " << Book.category << '\n';
		cout << "Requstor of Book\t: " << Book.requestor << '\n';
		cout << "Book in Call No\t\t: " << Book.call << '\n';
		cout << "Book's Invoice No\t: " << Book.invoice << '\n';
		cout << "Supplier for The Book\t: " <<

The problem is that the structure contains std::string objects, which can not be written to binary files like that. You need to either change the structure by replacing std::string with fixed-length char arrays or change the way the files are written, which can be somewhat tricky with std::string. IMO the simplest way to solve the problem is to change the structure

struct Book_Record
{
int ID;
float price;
char requestor[80];
char call[4];
char invoice[80];
// etc etc for all the rest of the structure members
bool deleted;
};

The next thing you will have to do is make minor changes in the rest of the code to call strcpy() in order to copy the strings into the structure. For example, instead of getline (cin,Book.name); you will have to do something like this:

getline(cin,dummy); // where dummy is std::string
strcpy(Book.name,dummy.c_str());

There are some pitfalls in using strcpy() like that -- you have to make sure the length of dummy does not exceed the size of the character array. In the example above, you need to make sure dummy.size() < sizeof(Book.name). That means more error checking in the Add function of the program.

Edited 5 Years Ago by Ancient Dragon: n/a

now you are taking in mars language to me :S
after doing like that it does not work at all :S i do not know whats wrong.can u tell
me how i change the way file is saved ?


i really not getting the strcpy thing. i need to put header <cstring>?

sorry for the trouble am causing. i need to submit next week and still i have nothing :S

Here's a hint how to do it

Here is how to code the structure

struct Book_Record
{
    int ID;
    float price;
    char requestor[80];
    char call[4];
    char invoice[20];
    char supplier[40];
    int copies;
    float total;
    char published[25];
    char date[20];
    char isbn[20];	
    char name[40];
    char author[40];
    char category[20];
    char publisher[40];
    bool deleted;
};

Below is the first part of the Add fucntion. You should be able to use this as a pattern for coding the rest of that.


Don't use getline(cin,dummy) to flush the input stream. That will make you enter the same string twice if there's nothing in the input stream. See this thread how to flush cin.

case '1':		
	cout << "\n\n\n";
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***                Add new records                      ***" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;

			cout << "\n\nBook's ID<AutoGenerate>: ";
			Book.ID = Auto++;
			cout << Book.ID << endl;

			if (output.open("id.txt"),ios::out)
			{
				output<<Auto;
			}
			else 
				cout << "The file is empty.";
				output.close();

			cout << "Enter Book's NAME: ";
			//getline(cin,dummy);
			cin.getline (Book.name, sizeof(Book.name));
			while (strchr(Book.name,"qwertyuiopasdfghjklzxcvbnm1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! NAME: ";
				cin.getline (Book.name, sizeof(Book.name));
			}

Edited 5 Years Ago by Ancient Dragon: n/a

i cannot get any where with the code . my one says that
error C2665: 'strchr' : none of the 2 overloads could convert all the argument types
and i do not know how to fix this .
and the input stream post is really big even thought i scheme through few pages i could not get anything .

You are right -- should have been strcspn, not strchr

>>and the input stream post is really big even thought i scheme through few pages i could not get anything

What don't you understand about it?

Edited 5 Years Ago by Ancient Dragon: n/a

:S
i do not know what wrong with me now my auto generate date does not work.
Error 1 error C2106: '=' : left operand must be l-value . and i cannot get it working it lefts a empty space

:S
i do not know what wrong with me now my auto generate date does not work.
Error 1 error C2106: '=' : left operand must be l-value . and i cannot get it working it lefts a empty space

here is the new code......
the date aint working and


( i am trying to hide my password as (*) whats the function called for that hash or encryption ?)

Attachments
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <fstream>
#include <string>
#include <ctime>
#include <sys/stat.h>
using namespace std;

struct Book_Record
{
    int ID;
    char price[10];
    char requestor[80];
    char call[30];
    char invoice[20];
    char supplier[40];
    char copies[10];
	char total[10];
    char published[4];
    char date[10];
    char isbn[20];	
    char name[40];
    char author[40];
    char category[20];
    char publisher[40];
    bool deleted;
};

#define BOOK_FILE_NAME "Books.txt"
Book_Record Book;
int Auto = 100;
fstream *fs = NULL, *fs1 = NULL;// file streams for files : fs -> 'add', fs1 -> 'temp'
bool deletion = false;

void closeFile(fstream *); // closes a file with its pointer, then deletes the file pointer
bool isFileExist(const char *); // check if a file exists
 

int main()	
{
	ifstream input;
	input.open("id.txt"),ios::in;
	input >> Auto;
	ofstream output;
	int tries = 3;
	string acc,password,MainPage,dummy;

	cout << "\n\n\n";
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***                                             ***" <<endl;
	cout << "\t\t***                  Welcome                    ***" <<endl;
	cout << "\t\t***                    To                       ***" <<endl;
	cout << "\t\t***          Library Book Registration          ***" <<endl;
	cout << "\t\t***                  Program                    ***" <<endl;
	cout << "\t\t***                                             ***" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\t\t***************************************************" <<endl;
	cout << "\n\n\n";
	{
	while (tries)
	{
	cout <<"Please Enter ID: \a";
	cin >> acc;
	cout <<"Please Enter Password: ";
	cin >> password;

	system("CLS");

	if (acc == ("abc") && password == ("123"))
	{
		goto MainPage;
	}
	else 
	{
		cout << "Invalid Id or Password. Please enter again" <<endl;
		cout << "The Program Will terminate After (" <<tries<< "\n" << ") Unsuccessful Try" <<endl;
		tries--;
	}
	}
	return -1;
}
	
	
	
	
	return 0;
MainPage:
	system("CLS");

	int num;
{
		enum num { Add = 1, Search = 2, Update = 3, Delete = 4, Display = 5, Total = 6, Exit = 7};
}
		do
	{
	system( "CLS" );
	cout << "\n\n\n";
	cout << "\t***********************************************************" <<endl;
	cout << "\t***********************************************************" <<endl;
	cout << "\t***                                                     ***" <<endl; 
	cout << "\t***    Welcome to Librery Book Register System          ***" <<endl;
	cout << "\t***                                                     ***" <<endl;
	cout << "\t***          1. Add Record                              ***" <<endl; 
	cout << "\t***          2. Search Record                           ***" <<endl;
	cout << "\t***          3. Update Record                           ***" <<endl;
	cout << "\t***          4. Delete Record                           ***" <<endl;
	cout << "\t***          5. Display Records                         ***" <<endl;
	cout << "\t***          6. Exit Program                            ***" <<endl;
	cout << "\t***                                                     ***" <<endl; 
	cout << "\t***********************************************************" <<endl;
	cout << "\t***********************************************************" <<endl;
	cout <<"\n\n\n\n"<<flush;
	cout<<"please select one: ";
	num= _getch();
	}

	while ( num < '1' || num > '6' );

	system ( "CLS" );

	if (num == '2' || num == '3' || num == '4' || num == '5' )

	{ 
		if (!isFileExist (BOOK_FILE_NAME))

		{
			cout<<"Can't open or create '"<<BOOK_FILE_NAME<<"'file"<<endl;
			system("pause");
			goto MainPage;
		}
	}

switch(num)

	{
		int confirm;
		int recs_num;
		int id;
		
	case '1':		
	cout << "\n\n\n";
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***                Add new records                      ***" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;

			cout << "\n\nBook's ID<AutoGenerate>: ";
			Book.ID = Auto++;
			cout << Book.ID << endl;

			if (output.open("id.txt"),ios::out)
			{
				output<<Auto;
			}
			else 
				cout << "The file is empty.";
				output.close();

			cout << "Enter Book's NAME: ";
			getline(cin,dummy);
			cin.getline (Book.name, sizeof(Book.name));
			while (strcspn(Book.name,"qwertyuiopasdfghjklzxcvbnm1234567890") != NULL)
			{
				cout << "Invalid input! Please enter again! NAME: ";
				cin.getline (Book.name, sizeof(Book.name));
			}
			cout << "Book Author: ";
			//getline(cin,dummy);
			cin.getline (Book.author, sizeof(Book.author));
			while (strcspn(Book.author,"qwertyuiopasdfghjklzxcvbnm1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Author: ";
				cin.getline (Book.author, sizeof(Book.author));
			}
			cout << "Book's Publisher: ";
			//getline(cin,dummy);
			cin.getline (Book.publisher, sizeof(Book.publisher));
			while (strcspn(Book.publisher,"qwertyuiopasdfghjklzxcvbnm1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Publisher: ";
				cin.getline (Book.publisher, sizeof(Book.publisher));
			}
			
			cout << "Book's Category: ";
			//getline(cin,dummy);
			cin.getline (Book.category, sizeof(Book.category));
			while (strcspn(Book.category,"qwertyuiopasdfghjklzxcvbnm1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Category: ";
				cin.getline (Book.category, sizeof(Book.category));
			}


			cout << "Requestor : ";
			//getline(cin,dummy);
			cin.getline (Book.requestor, sizeof(Book.requestor));
			while (strcspn(Book.requestor,"qwertyuiopasdfghjklzxcvbnm1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Requestor: ";
				cin.getline (Book.requestor, sizeof(Book.requestor));
			}

			cout << "Book in Call no : ";
			//getline(cin,dummy);
			cin.getline (Book.call, sizeof(Book.call));
			while (strcspn(Book.call,"qwertyuiopasdfghjklzxcvbnm1234567890-.+") != NULL)
			{
				cout << "Invalid input! Please enter again! Call No.: ";
				cin.getline (Book.call, sizeof(Book.call));
			}

			cout << "Invoice no : ";
			//getline(cin,dummy);
			cin.getline (Book.invoice, sizeof(Book.invoice));
			while (strcspn(Book.invoice,"qwertyuiopasdfghjklzxcvbnm1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Invoice No.: ";
				cin.getline (Book.invoice, sizeof(Book.invoice));
			}

			cout << "Supplier: ";
			//getline(cin,dummy);
			cin.getline (Book.supplier, sizeof(Book.supplier));
			while (strcspn(Book.supplier,"qwertyuiopasdfghjklzxcvbnm1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Supplier: ";
				cin.getline (Book.supplier, sizeof(Book.supplier));
			}
						
			cout << "Book's ISBN: ";
			//getline(cin,dummy);
			cin.getline (Book.isbn, sizeof(Book.isbn));
			while (strcspn(Book.isbn,"1234567890-.+") != NULL)
			{
				cout << "Invalid input! Please enter again! ISBN: ";
				cin.getline (Book.isbn, sizeof(Book.isbn));
			}

			cout << "Price Of Book (RM) : ";
			//getline(cin,dummy);
			cin.getline (Book.price, sizeof(Book.price));
			while (strcspn(Book.price,"1234567890-.+") != NULL)
			{
				cout << "Invalid input! Please enter again! Price: ";
				cin.getline (Book.price, sizeof(Book.price));
			}

			cout << "Year Book was Published: ";
			//getline(cin,dummy);
			cin.getline (Book.published, sizeof(Book.name));
			while (strcspn(Book.published,"1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Published: ";
				cin.getline (Book.published, sizeof(Book.published));
			}

			cout << "How Many Copies of Book(example 033): ";
			//getline(cin,dummy);
			cin.getline (Book.copies, sizeof(Book.copies));
			while (strcspn(Book.copies,"1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Copies: ";
				cin.getline (Book.copies, sizeof(Book.copies));
			}
		char date [10];
		_strdate_s (date);
		Book.date == date;
			
	cout << "\nSave record?\n<1=yes, 0=no>" << endl;
	cin>>confirm;

	if (confirm == 1)
	{
		fs = new fstream (BOOK_FILE_NAME, ios::out | ios::app | ios::binary);
		if (!fs)
		{
			cout<<"\nCan't open or create'"<<BOOK_FILE_NAME<<"'file"<<endl;
			system("pause");
			break;
		}
		fs->write ( (char *) &Book, sizeof(Book));
		closeFile(fs);
	
		cout << "\nRecord Added." << endl;
		system("pause");
		goto MainPage;
		}
	else
		cout << "\nRecord rolled back." << endl;
		system ("pause");

		goto MainPage;
	
	break;
	
	
	case '2':
	cout << "\n\n\n";
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***                Search Books                         ***" << endl;
	cout << "\t***                                                     ***" << endl; 
	cout << "\t***********************************************************" << endl;
	cout << "\t***********************************************************" << endl;

		
		cout << "\n\nplease enter Book's ID: " ;
		cin >>id;

		fs= new fstream ( BOOK_FILE_NAME, ios::in | ios::out | ios::binary);
		
		if (!fs)
		{
			cout<<"\nCan not open or create "<<BOOK_FILE_NAME<<"File"<<endl;
			system("pause");
			break;
		}
		recs
char date [10];
		_strdate_s (date);
		Book.date == date;

You can not assign c style chracter arrays like that. You have to call strcpy() to do that. But your program doesn't need to do that anyway, in the clal to _strdate_s just use Book.date instead of date variable _strdte_s(Book.date, sizeof(Book.date));

wow
you are really life saver man.
thank you a lot. :D

now i want to bother u more.

cout << "Year Book was Published: ";
			//getline(cin,dummy);
			cin.getline (Book.published, sizeof(Book.name));
			while (strcspn(Book.published,"1234567890-+") != NULL)
			{
				cout << "Invalid input! Please enter again! Published: ";
				cin.getline (Book.published, sizeof(Book.published));
			}

how can i restrict this one with 4 size . i only know how to restrict in a stucrt.(Book.published.size() !=4 ) but like this will not work . :D

and what is the code called where my password will show as "*". i mean when i type the password it will input the password but will show as *

>>how can i restrict this one with 4 size
Just declare Book.published in the structure as char published[5]; There are no standard C or C++ functions that just display * for passwords. Here's a thread with some suggestions for you to think about.

all rite thank you again.

do u have any suggestion to make this program better ?
am out of idea and just now i show my lecturer the program and he is saying to get more things in it.

>>how can i restrict this one with 4 size
Just declare Book.published in the structure as char published[5]; There are no standard C or C++ functions that just display * for passwords. Here's a thread with some suggestions for you to think about.

the problem now is i can restrict the maximum number of the char . i need to restrict them also at the minimum number. :S

The only way to do that is to just check the length of the string after input. If it is less than the minimum then display an error message and loop back to get new keyboard input.

mars code .
i just deleted the strings .
so now i have to do make a add if in the while loops i have. and put

        cout << "Book's ISBN: ";
        //getline(cin,dummy);
        cin.getline (Book.isbn, sizeof(Book.isbn));
        while (strcspn(Book.isbn,"1234567890-.+") != NULL)
        {
            if (( Book.isbn.size()<=3 ) ||( Book.isbn.size()>4) )

            {
            cout << "Invalid input! Please enter again! ISBN: ";
            cin.getline (Book.isbn, sizeof(Book.isbn));
        }}

am really sorry i only know this restriction code . and this one cannot work because i donot have string i guess well this is the error am getting

Error   2   error C2228: left of '.size' must have class/struct/union

Edited 3 Years Ago by mike_2000_17: Fixed formatting

character arrays do not have methods like c++ classes. You have to treat them as if you are writing a C program. Just call strlen() to get the length of the string. if( strlen(Book.isbn) != 4 )

CLS and cls are not distinguished in C and secondly your code is error free.You do need to modify the strings section a bit.

Edited 5 Years Ago by Ezzaral: Snipped advertising link.

This article has been dead for over six months. Start a new discussion instead.