hello
me and my friends were assigned to make a program that can add,delete,update and show information. we were able to make this code ( am sending u ) but we cannot solve some problem like the after adding or deleting or any other work the window does not goes back to front window. and if a court is booked its again became book by next information. like if student 1 books at 14:00 court A date 21/4/2011 . next person who wants the court at the same time just get the court ( needs this court to be booked for that time after it was booked by the 1st person).
hope we get help soon

#include <string>
#include <cstdlib> // system()
#include <conio.h> // getch()
#include <fstream>
#include <sys\stat.h> // stat(status of a file)
#include <iostream>
#include <iomanip>
using namespace std;
 
 
//Data types
struct Student_Record // Student record
{
	 string ID;
	 string time;
	 string program;
	 string age;
	 string date;
	 string court;
	 bool deleted;
};

//Variable Declarations & Constants
#define STUDENT_FILE_NAME "Student.txt" // name of the database file to store Students informations

Student_Record Student;
char     choice; // for choice in menu
fstream *fs = NULL, *fs1 = NULL;// file streams for files : fs -> 'Student', fs1 -> 'temp'
bool     deletion = false; // if any record has been deleted

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

//Main
int main()
{
	cout << "\n\n\n\n\n\n";
	cout << "             ******************************************************\n";
	cout << "             *  PPPPp   L            A         Y       Y    !!    *\n";
	cout << "             *  P    P  L           A A         Y     Y     !!    *\n";
	cout << "             *  PPPPP   L          A   A         Y   Y      !!    *\n";
	cout << "             *  P       L         AAAAAAA         Y Y       !!    *\n";
	cout << "             *  P       L        A       A         Y        !!    *\n";
	cout << "             *  P       LLLLLL  A         A        Y        OO    *\n";
	cout << "             *                                                    *\n";
	cout << "             ******************************************************\n\n";
	cout << "                        "; system("pause");
	
	while (true)
      {
            do
            {
                  system( "cls" );

				  cout << "\n                        < ##INTI International University ## >";
				  cout << "\n                       < ********Tennis Court Reservation System******** > \n\n\n";
                  cout << "                       1. Add a new Record \n";
                  cout << "                       2. Modify an existing Record\n";
                  cout << "                       3. Delete an existing Record\n";
				  cout << "                       4. Search an existing Record\n";
                  cout << "                       5. Display Records\n";
                  cout << "                       0. Exit \n\n";
                  cout << "                        Enter a choice (0-5) : " << flush;
                  choice = _getch();
            } while ( choice < '0' || choice > '5'); // choice not between 1 and 5, show menu again
 
            system( "cls" );

			   // to modify, delete or display records, database file should exist, then we have some records           
            if (choice == '2' || choice == '3' || choice == '4')
            {
                  if (!isFileExist(STUDENT_FILE_NAME)) // if database file doesn't exist
                  {
                        cout << "\nDatabase file ('" << STUDENT_FILE_NAME << "') doesn't exist, then there are no records." << endl;
                        system("pause");
                        continue; // show the menu again
                  }
            }

			int recs_num; // number of records before the record for modifying(deletion)
            string id;
 
 
            switch ( choice )
            {
      

			case '1' : //Add Record
 
					cout << "\n\t\t < Entering a new record > ";
					cout << "\n   Enter the following informations for the new record : ";
					cout<<"Please Enter ID";
                     cout << "\nID - Only Numbers \n(Ex: 1001)       : ";
					 getline(cin,Student.ID);

					 while (( Student.ID.size()<=3 ) || ( Student.ID.size()>4) || ( Student.ID.find_first_of( "0123456789" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter ID Again !!" << endl;
								cout << "Please Enter Your ID : " ;
								getline (cin, Student.ID);

							}
				

					 cout << "\n\nTIME(Ex: 1400 24Hrs System) : ";
					getline(cin, Student.time);
					while((Student.time.size() <4) || (Student.time.size() >4) || (Student.time.find_first_of ("0123456789")==string::npos ))
							{

								cout << "Invalid ! Please Re-Enter Your Time Again !!" << endl;
								cout << "Please Enter Time : ";
								getline(cin,Student.time);

							}


						cout << "\n" ;

					cout << "\nPROGRAM \n(Ex: CFPI): ";
					getline(cin, Student.program);
					while ((Student.program.size()<=0 ) || ( Student.program.size()>6) || ( Student.program.find_first_of( "QWERTYUIOPLKJHGFDSAZXCVBNM" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Your Program Again !! " << endl;
								cout << "Please Enter Your Program: " ;
								getline (cin,Student.program);
					}
					
					cout<<"\n";
					cout << "Enter Your Age\n (Ex.21) : ";
					getline (cin,Student.age);


					while ((Student.age.size()<=0 ) || ( Student.age.size()>2) || ( Student.age.find_first_of( "0123456789" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Your Age Again !! " << endl;
								cout << "Please Enter Your Age : " ;
								getline (cin, Student.age);

							}

						cout << "\n" ;

					
					cout << "\nDATE \n(Ex: 10/2/2010):";
					getline(cin,Student.date);
					while ((Student.date.size()<6) || ( Student.date.size()>10) || ( Student.date.find_first_of( "/0123456789" ) == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Your Date Again !! " << endl;
								cout << "Please Enter Your Date : " ;
								getline (cin, Student.date);

							}

						cout << "\n" ;

					cout << "\nCOURT \n(Ex: A):";
					getline(cin,Student.court);
					while ((Student.court.size()==11) || (Student.court.find_first_of("ABCDEFG") == string::npos))
							{

								cout << "Invalid ! Please Re-Enter Your Court Again !! " << endl;
								cout << "Please Enter Your Court : " ;
								getline (cin, Student.court);

							}

						cout << "\n" ;
					

					Student.deleted = 0;

					 fs = new fstream( STUDENT_FILE_NAME, ios::out | ios::app | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << STUDENT_FILE_NAME << "' file" << endl;
                        system("pause");
                        break;
                  }
                 
                  fs->write( (char *) &Student, sizeof(Student) );
                             
                  closeFile(fs);
 
                  cout << "\nRecord added." << endl;
                  system("pause");
                 
                  break;


				   case '2' : //Modify Record
 
                  cout << "\n Enter Student ID, that you want modify its information : ";
                  cin >> id;
				  cin.ignore();
 
                  fs = new fstream( STUDENT_FILE_NAME, ios::in | ios::out | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << STUDENT_FILE_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()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified Customer doesn't exist in file." << endl;
                        closeFile(fs);
                        system("pause");
                        break;
				  }

				   cout << "\n   Enter new informations for this record : ";

				    cout << "\nID - Only Numbers \n(Ex: 1001)       : ";
					 cin >> Student.ID;
					 cin.ignore();
					 cout << "\n\nTIME(Ex: 14.00 24Hrs System) : ";
					getline(cin, Student.time);
					cout << "\nPROGRAM \n(Ex: CFPI): ";
					getline(cin, Student.program);
					cout << "\nAGE \n(Ex: 21):";
					cin.ignore();
					cout << "\nDATE \n(Ex: 10/2/2010):";
					cin >> Student.date;
					cin.ignore();
					cout << "\nCOURT \n(Ex: A):";
					cin >> Student.court;
					cin.ignore();

				  fs->seekp ( sizeof(Student) * recs_num, ios::beg ); // go to the first of the record to be modified
                  fs->write( (char *) &Student, sizeof(Student) );
                 
                  closeFile(fs);
 
                  cout << "\nRecord is modified." << endl;
                  system("pause");
 
                  break;


				   case '3' : //Delete Record
 
                  cout << "\n Enter Student's ID, for deletion : ";
                  cin >> id;
				  cin.ignore();
 
                  fs = new fstream( STUDENT_FILE_NAME, ios::in | ios::out | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << STUDENT_FILE_NAME << "' file." << endl;
                        system("pause");
                        break;
                  }
                 
                  recs_num = -1;
 
                  while (fs->read( (char *) &Student, sizeof(Student) ))
                  {
                        recs_num++;
 
                        if ( Student.ID == id && !Student.deleted ) // if user deleted an Student then added another one with the same ID in the same instance of program runs, deleted Student is still there, then we should go through all the file
                              break;
                  }
 
                  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified Customer doesn't exist in database file." << endl;
                        closeFile(fs);
                        system("pause");                   
                        break;
                  }

				  case '4' : //Search Record
 
                  cout << "\n Enter Student.ID, that you want to search : ";
                  cin >> id;
				  cin.ignore();
				   
                  fs = new fstream( STUDENT_FILE_NAME, ios::in | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << STUDENT_FILE_NAME << "' file" << endl;
                        system("pause");
 
                        break;
                  }

				  recs_num = -1;
 
                  while (fs->read( (char *) &Student, sizeof(Student) ))
                  {
                        recs_num++;
 
                        if ( Student.ID == id && !Student.deleted ) // if user deleted an Customer then added another one with the same ID in the same instance of program runs, deleted Student is still there, then we should go through all the file
                              break;
                  }

				  if (fs->eof()) // if (the record is not in the file || it's there but it's deleted)
                  {
                        cout << "\nYour specified Student doesn't exist in file." << endl;
                        closeFile(fs);
                        system("pause");
                        break;
                  }

				  fs->seekp ( sizeof(Student) * recs_num, ios::beg ); // go to the searched record
				  fs->read( (char *) &Student, sizeof(Student) ); // display the searched record
                  
				  if ( !Student.deleted)
				  {

 
					  Student.deleted = 1;
 
                  fs->seekp ( sizeof(Student) * recs_num, ios::beg );
                  fs->write( (char *) &Student, sizeof(Student) );
                 
                  closeFile(fs);
 
                  deletion = true; // we have some deleted records
 
                  cout << "\nRecord is deleted." << endl;
                  system("pause");
 
                  break;

				    {
					  cout << "\n\n";
					  cout << "ID\t\t\t: " << Student.ID << '\n';
					  cout << "TIME\t\t\t: " << Student.time << '\n';
					  cout << "PROGRAM\t: " << Student.program << '\n';
					  cout << "AGE\t\t\t: " << Student.age<< '\n';
					  cout << "DATE\t\t\t: " << Student.date<< '\n';
					  cout << "COURT\t\t: " << Student.court << '\n';
					 
				  }
					  
					cout << "\nTo return to menu, "; system("pause");
 
                  closeFile(fs);
 
                  break;

				    case '5' : // Display Records
 
                  fs = new fstream( STUDENT_FILE_NAME, ios::in | ios::binary );
 
                  if (!fs)
                  {
                        cout << "\nCan't open or create '" << STUDENT_FILE_NAME << "' file." << endl;
                        system("pause");
                        break;
                  }

				   cout << "\n\t\t\t       < Student Records >\n\n";
                  cout << "ID\tTIME\t\tPROGRAM\t\tAGE\tDATE\tCOURT\n"
                         << "------------------------------------------------------------------------------\n";                                         
                                   
                  while (fs->read( (char *) &Student, sizeof(Student) )) // display records
                  {
                        if ( !Student.deleted )
                        {
							cout << Student.ID << "\t";
							cout << Student.time << '\t';
							cout << Student.program<< '\t';
							cout << Student.age << "  ";
							cout << Student.time<< "  ";
							cout <<Student.court << '\t';
							 
						}
                  }
 
                  cout << "\nTo return to menu, "; system("pause");
 
                  closeFile(fs);
 
                  break;
                 
				   case '0' : // Exit
 
                  if (deletion) // if there is any deletion, then update database file (create a new temp file that doesn't have deleted records, then remove the old database file and rename temp file to database file name)
                  {
                        cout << "\nUpdating '" << STUDENT_FILE_NAME << "' File..." << endl;
 
                        fs = new fstream( STUDENT_FILE_NAME, ios::in | ios::binary );
                        if (!fs)
                        {
                              cout << "\nCan't open '" << STUDENT_FILE_NAME << "' file, then Updating is incomplete." << endl;
                              system("pause");
 
                              system( "cls" );
                              return 1;
                        }
 
                        fs1 = new fstream( "temp", ios::out | ios::binary);
                        if (!fs1)
                        {
                              cout << "\nCan't create temp file, then Updating is incomplete." << endl;
                              system("pause");
 
                              closeFile(fs);
                              system( "cls" );
                              return 1;
                        }
 
                        // write nondeleted records to the temp file
                        while (fs->read( (char *) &Student, sizeof(Student) ))
                              if ( !Student.deleted )
                                    fs1->write( (char *) &Student, sizeof(Student) );
 
                        closeFile(fs);
                        closeFile(fs1);

						 if( remove( STUDENT_FILE_NAME ) == -1 ) // if there is an error
                        {
                            cout << "\nCan't delete '" <<STUDENT_FILE_NAME << "' file, then Updating is incomplete." << endl;
                              system("pause");
                             
                              system( "cls" );
                              return 1;
                        }

						   struct stat st; // to check size of the temp file
                        int res = stat( "temp", &st );
                       
                        if (st.st_size == 0) // if all of records are deleted then the temp file size is zero                        
                              remove( "temp" ); // we have no records, then no database file is needed, just delete the temp file
                        else
                              if ( rename ("temp", STUDENT_FILE_NAME) )
                              {
                                    cout << "\nCan't rename temp file, then Updating is incomplete." << endl;
                                    system("pause");
                                   
                                    system( "cls" );
                                    return 1;
                              }
 
                        cout << "\nUpdating database file completed." << endl;
                        system("pause");
                  }
 
                  system( "cls" );

				  
				  //Exit Splash Screen
				  cout << "\n\n\n\n\n\n";
				  cout << "                                                                              \n";
				  cout << "           GGGGGG    OOOOO  OOOOO  DDD       BBBB   Y      Y  EEEEE           \n";
				  cout << "           G         O   O  O   O  D   D     B   B   Y    Y   E               \n";
				  cout << "           G         O   O  O   O  D   D     B   B    Y  Y    E               \n";
				  cout << "           G    GGG  O   O  O   O  D   D     BBBB      YY     EEEE            \n";
				  cout << "           G     G   O   O  O   O  D   D     B   B     YY     E               \n";
				  cout << "           G     G   O   O  O   O  D   D     B   B     YY     E               \n";
				  cout << "           GGGGGG    OOOOO  OOOOO  DDD       BBBB      YY     EEEEE           \n";
				  cout << "                                                                              \n";
	
				  cout << " \n\n\n\n\n\n\nThank You,Sir.\n"; system("break");

                  return 0;
 
                  break;

            } // end 'switch'
      } // end 'while'
 
      return 0;
} // end 'main()'
 
		

				
}
//Function Modules
 void closeFile(fstream *fs)
{
      fs->close(); // close the file
      delete fs;
      fs = NULL;
}
 
bool isFileExist(const char * file_name)
{    
      struct stat st; // to check status of file
      int res = stat( file_name, &st );
      return (res == 0); // if file exists     
}

thnx in advance

That's a very long piece of code to ask someone to go through. Try to narrow down a specific region where you think the problem is occurring (you have your loop with the menu at the top, try to figure out why control is not reaching that point again). Try to think of ways to refactor some of the parts of main() into functions.

i need a code that can book the court and date and lock it . like if student 1 want a court at 14:00 at 21/04/02011 after inputing the data the program should lock that day and time, and when other one want the court it will show that the court is book.
thnx

I understand your requirement. I'm saying that rather than weeding through 500 lines of code, it would be preferable for you to say "On line X of function Z, I want to be able to enter Y, but the line fails to execute" rather than "here's my requirement."

all rite got u . thnx for reply. well my problem is the whole program . this is my 1st sem and i will never need C++ ever so i didnt concentrate on the class now i donno what to do . am saying this coz i really donno which of this functions are related with the problem i am facing. think i have to go through and do the program again :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.