#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??

OK..i tried to run it..
first...your case 1-5...remove the "single " ' "

then put bracket on each case

case 1:
{
//code here
}
break;

then..your codes still buggy though..but no more error

Edited 6 Years Ago by Anyzen: n/a

yeah..
bugs everywhere...
errr.. can i know how to restrict the user's input??
eg: when entering name, user can't enter integer value as the name..

Jackie,

If you wish a simple method for checking if the user has entered any numbers instead of just characters you can use the isalpha() function.

it accepts a single character as input so you could make a check function something like,..

bool checkForNoNumbers(string &str)
{
    char* temp;// temp holder for string
    temp = new char[str.length()];//allocate enough space for the string
    strcpy(temp, x.c_str()); //copy the string into character array
    
    for(int y = 0; y<x.length(); y++) //loop through the string
   {
	if(!isalpha(*(temp+y))) //if the character is not a letter (this picks up spaces as well and counts them as not letters)
	{
		cout<<"Character: "<<*(temp+y)<<" is not a character"<<endl; 
               //print the character thats not a letters (just for checks)

		return(false); //return false the string is not all letters
	}
   }
    
	return(true);//return true the string is all letters
}

Something like that code above will check if the user enters any non alpha characters (not A-Z, a-z) and if they have not used any non alpha's then it will return true.

Im sure there is a proper c++ way of doing this using iterators and all those crazy things but im quite new to c++ too so the c way is still all i know :(.

Hope this helps solve the user input issue and you can solve the rest too.

edit:

I looked at your code and the issue is that you are createing variables inside the case stament and it does not like that, so by moving them to just before the switch statement as shown below the error is resolved , however running your code i added a student with id of 1, and then when i ask to edit and provide the id number i am told it does not exist so, now you got some bugs but the code will compile. Code fix follows:

int recs_num;
            int id;
            string name;// cannot create a variable in a case statement so put it here
            switch ( choice )
            {

            case '1' :

			
                  cout << "\n\t\t < Entering a new record > ";
                  cout << "\n   Enter the following informations for the new record : " << endl;

additional edit:

OK..i tried to run it..
first...your case 1-5...remove the "single " ' "

then put bracket on each case

case 1:
{
//code here
}
break;

then..your codes still buggy though..but no more error

This is not the case, (ignoring the pun) the '1' tells the switch statment to look for the ascii character '1' which as the input is a character, this is what you will get, looking for integer 1 which is ascii start of headding character, i dont think people have this on thier keyboard and as the value read in is not converted to an integer by atoi() then the character that is needed is indeed '1' and not integer 1, the curly braces are not necccessary on a case statement as all consecutive statements are executed from the successful entry from a case label untill a break command;

Edited 6 Years Ago by Kanoisa: n/a

....
This is not the case, (ignoring the pun) the '1' tells the switch statment to look for the ascii character '1' which as the input is a character, this is what you will get, looking for integer 1 which is ascii start of headding character, i dont think people have this on thier keyboard and as the value read in is not converted to an integer by atoi() then the character that is needed is indeed '1' and not integer 1, the curly braces are not necccessary on a case statement as all consecutive statements are executed from the successful entry from a case label untill a break command;

Well spoken. He is indeed correct, there is no need to put curly braces {} on a switch but it is recommended. It is easier to spot when you are outside of the switch as well as outside the case if you have curly braces enclosing each one.

I'd also suggest you clean your code a little.

By that I mean you could:

  • add comments, explaining what each case does, what purpose for the variables etc. a little more information for both you and perhaps an outsider of what's going on in the line of execution
  • put each case statement in its own function since they are so big, separating it from the main chunk of the code makes it easy to edit later, check for errors or for bugs
  • even better is to give them each their own source file for a more managed project

Edited 6 Years Ago by ShadowScripter: n/a

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