I was given an assignment to complete on database, i have created on but crashes after executing once, check it friends and please help, if you can modify or make it way, i'll be graceful.

Please find the coding in attachment section. thanks hope will get good response

Comments
Why is this a poll? Please don't abuse the forum features like this.
Attachments
#ifndef  _FUNCTION_PRO_H
#define  _FUNCTION_PRO_H

#include "include files.cpp"
#include "struct.cpp"


//============================================//
//	Function Prototypes
//============================================//

void MenuOperation( char );

void AddNode( char *, int, int, short int, short int, int );
void AddTo_Front( Node * );
void AddTo_End( Node * );
void AddBefore_InBetween( Node *, Node * );
void AddAfter_InBetween( Node *, Node * );
void AddNode_Menu( Node * );
Node* Allocate();

void SearchMenu();
Node* SearchBy_ID( int );
Node* SearchBy_Name( char* );

void ModifyMenu();

void DeleteMenu();
void DeleteFrom_Start();
void DeleteFrom_End();
void DeleteIn_Between( Node * );

void Display();
void clrscr();

void SaveToFile(Node *);
void loadFile();

#endif
#include "functions prototype.h"
#include "include files.cpp"


using namespace std;


struct Node *Head=NULL;





//=========================================//
//clear screen
void clrscr()
{
     system("cls");
}
//=========================================//




//==========================================//
//		Add Node Methods
//==========================================//

Node* Allocate()
{
	Node * Ptr = new Node ;
   return Ptr;
}

//===========================================//



void AddNode( char *tName, int tID, int tQty, short int tDay, short int tMonth, int tYear)
{
	Node * Ptr = Allocate();
   
   
   
   	Ptr->Name = new char[strlen(tName)];
   	strcpy(Ptr->Name,tName);
   	Ptr->ID = tID;
   	Ptr->Quantity = tQty;
   	Ptr->Receive_Date.year = tYear;
   	Ptr->Receive_Date.month = tMonth;
   	Ptr->Receive_Date.day = tDay;
   	Ptr->Next = NULL;

   if(Head)
   	AddNode_Menu(Ptr);

   else
   	Head=Ptr;
   	
   	SaveToFile(Ptr); //save record to a file 
}

//=========================================//

void AddTo_Front( Node * NewNode )
{
	NewNode->Next = Head ;
   Head = NewNode ;
}

//=========================================//

void AddTo_End( Node * NewNode )
{
   Node * i = Head ;
	for( ; i->Next!=NULL; i=i->Next )
   {;}
	i->Next = NewNode ;
}

//=======================================//

void AddBefore_InBetween( Node * Flag , Node * NewNode )
{
	Node * i = Head ;
   for( ; i->Next != Flag; i=i->Next )
   {;}
   i->Next = NewNode ;
   NewNode->Next = Flag ;
}

//=========================================//

void AddAfter_InBetween( Node * Flag , Node * NewNode )
{
   NewNode->Next=Flag->Next;
   Flag->Next = NewNode ;
}

//=========================================//





//=========================================//
//			Search Node Methods
//=========================================//

Node* SearchBy_Name( char* tName )
{
   Node * i = Head ;
   for(  ; i!=NULL ; i = i->Next )
   	if(!strcmp(tName,i->Name))
         break;

   return i;
}

//========================================//

Node* SearchBy_ID( int tID )
{
   Node * i = Head ;
   for( ; i!=NULL ; i = i->Next )
      if(tID == i->ID)
        break;

   return i;
}

//==========================================//





//=========================================//
//	Delete Node Methods
//=========================================//

void DeleteFrom_Start()
{
   if(Head->Next)
	{
      Node * Temp = Head;
   	Head = Head -> Next;
      delete Temp;
   }

   else
   {
   	delete Head;
      Head = NULL;
   }
}

//=========================================//

void DeleteFrom_End()
{
   if( !Head->Next )
	{
      delete Head;
      Head = NULL;
   }

   else
   {
   	Node * i = Head;
      Node * j = NULL;
		for( ; i->Next!=NULL ; i = i->Next )
      {j=i;}
      j->Next = NULL;
      delete i;
   }
}

//========================================//

void DeleteIn_Between( Node * Current )
{
   Node * j = NULL;
   Node * i = Head;
	for( ; i!=Current ; i=i->Next)
   {j=i;}

   j->Next = i->Next;
   delete Current;
}

//========================================//






//======================================//
//	        Display
//======================================//

void Display()
{
    int records=1; 
	for(Node *i=Head;i!=NULL;i=i->Next)	
   {
      cout << "Records: " << records << endl;       
      cout << "Book ID: " << i->ID << endl;
      cout << "Book Name: " << i->Name << endl;
      cout << "Number Of Books: " << i->Quantity << endl;
      
      //display the date and add a zero if number less than 10
      if(i->Receive_Date.day < 10 && i->Receive_Date.month >= 10)
      {
        cout << "Received On: 0" << i->Receive_Date.day << "/" << i->Receive_Date.month << "/"  << i->Receive_Date.year << endl;
      }
      
      else if(i->Receive_Date.month < 10 && i->Receive_Date.day >= 10)
      {
        cout << "Received On: " << i->Receive_Date.day << "/0" << i->Receive_Date.month << "/"  << i->Receive_Date.year << endl;
      } 
       
      else if(i->Receive_Date.month < 10 && i->Receive_Date.day < 10)
      {
        cout << "Received On: 0" << i->Receive_Date.day << "/0" << i->Receive_Date.month << "/"  << i->Receive_Date.year << endl;
      }  
      
      else 
      {
           cout << "Received On: " << i->Receive_Date.day << "/" << i->Receive_Date.month << "/"  << i->Receive_Date.year << endl;
      }
      
      //print "----------------" after each book rcord 
      for(int j=0; j<=79; ++j)
              cout << "-";
              
      cout << endl;
      
      ++records;
      
      //cout<<i->Name<<":"<<i->ID<<endl;
      
   }
}

//=======================================//
// End of Re-usable functions, below are Menu Operation Launchers...
// End of Functionality




void MenuOperation( char Action )
{
   if(Head||Action=='1')
	switch( Action )
   {
   	case '1':
      {
         char tName[25];
         int tID, tQty, tYear;
         short int tDay, tMonth;
         
         cout<<"\n Enter book Details\n\n";

         cout<<"Enter Book ID: ";
         cin >> tID;
         
         //clear any error from input
         cin.ignore();
         cin.ignore(0);
         cin.clear();
         cin.sync();

         cout<<"Enter book title: ";
         cin.getline(tName, 25, '\n');
         
         cout<<"Enter Book Quantity: ";
         cin >> tQty;
         
         cin.ignore();
         cin.ignore(0);
         cin.clear();
         cin.sync();
         
         cout<<"Enter Year Received: ";
         cin >> tYear;
         
         cin.ignore();
         cin.ignore(0);
         cin.clear();
         cin.sync();
         
         cout<<"Enter Month Received(1-12): ";
         cin >> tMonth;
         
         cin.ignore();
         cin.ignore(0);
         cin.clear();
         cin.sync();
         
         cout<<"Enter Day Received(1-max(31)): ";
         cin >> tDay;
         
         cin.ignore();
         cin.ignore(0);
         cin.clear();
         cin.sync();
         
         
         
      	AddNode(tName,tID, tQty, tDay, tMonth, tYear);
         break;
      }

      case '2':
      {
         SearchMenu();
         break;
      }

      case '3':
      {
      	ModifyMenu();
         break;
      }

      case '4':
      {
         clrscr();
      	Display();
         getch();
         break;
      }

      case '5':
      {
      	DeleteMenu();
         break;
      }
   }

   else
   {
   	cout<<"Unable to Perfom operation on [Empty List]"<<endl;
      getch();
   }

}








//======================================//
//	Add Menu Operation Launcher
//======================================//

void AddNode_Menu( Node * NewNode )
{

		clrscr();
      cout<<"[1] Add to Front\n[2] Add to End\n[3] Add By Search_Before\n[4] Add By Search_After\n[0] ..Back"<<endl;
      char Choice = getch();

      Node* Flag = NULL;

      if(Choice!='0')
      {
      	switch(Choice)
      	{
      		case '1':
         	{
            	AddTo_Front(NewNode);
               break;
         	}

         	case '2':
         	{
            	AddTo_End(NewNode);
               break;
         	}

            case '3':
            case '4':
         	{
               clrscr();
               cout<<"[1] Add Before/After by Searching Name \n[2] Add Before/After by Searching ID\n";
               char SChoice = getch();

               switch(SChoice)
               {
                  case '1':
            		{
                  	char tName[25];
               		cout<<"Enter Name to Search Node : ";
               		gets(tName);
               		Flag = SearchBy_Name(tName);
                  	break;
               	}

                  case '2':
            		{
                  	int tID;
                     cout<<"Enter ID to Search Node : ";
               		cin>>tID;
               		Flag = SearchBy_ID(tID);
                  	break;
               	}
                  default:
                  	cout<<"\a";

               }

               if(Choice=='3')
               {
               	if(Flag)
               	{
               		if( Flag == Head )
                  	AddTo_Front(NewNode);

                  	else
                  	AddBefore_InBetween(Flag,NewNode);
               	}
         		}

               else
               {
                  if( Flag == Head || !Flag->Next )
                  {
                     NewNode->Next = Flag->Next;
                  	Flag->Next = NewNode;
                  }
                  else
               	AddAfter_InBetween(Flag,NewNode);
               }

               break;
               }

            default:
            	cout<<"\a";
      	}

         cout<<(Flag?"Added":"Couldn't Add");
      	getch();
      }


}


//======================================//
//    Search Menu Operation Launcher
//======================================//

void SearchMenu()
{
   char Choice = -1;
	while(Choice)
   {
   	clrscr();
      cout<<" 1. Search by Name\n   2. Search by ID\n[0] ..Back"<<endl;
      char Choice = getch();

      Node* Flag = NULL;

      if(Choice!='0')
      {
      	switch(Choice)
      	{
      		case '1':
         	{
            	char tName[25];
            	cout<<"Enter Name to Search: ";
            	gets(tName);
         		Flag = SearchBy_Name(tName);
               break;
         	}

         	case '2':
         	{
            	int tID;
            	cout<<"Enter ID to Search: ";
            	cin>>tID;
         		Flag = SearchBy_ID(tID);
            	break;
         	}

            default:
            	cout<<"\a";
      	}

         cout<<(Flag?"Found":"Not Found");
      	getch();
      }
      else
      	break;

   }
}




//==========================================//
//
#ifndef _INCLUDE_CPP
#define _INCLUDE_CPP


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <cstdlib>
#include <ctype.h>
#include <cstring>
#include <fstream>


#endif
#include "include files.cpp"
#include "functions prototype.h"



using namespace std;




int main()
{
    loadFile();
    

   	while(1)
      {
      	clrscr();

      	cout<<"Enter your choice:\n\n";

         cout<<"  1. ADD BOOK \n\n  2. Search BOOK\n\n  3. Modify\n\n  4. Display\n\n  5. Delete\n\n  0. Exit\n\n";

         char Choice = getch();

         switch(Choice)
         {
         	case '1':
         	case '2':
         	case '3':
         	case '4':
         	case '5':

               clrscr();
               MenuOperation(Choice);
               break;

         	case '0':
         		exit(1);

         	default:
         		cout<<"\a";
         }
         
         
      }
}
#ifndef _STRUCT_CPP
#define _STRUCT_CPP

#include "include files.cpp"

//struct Node *Head = NULL;
struct date_rec	//	record to get date
   {
      short int day;
      short int month;
      int year;
    };


struct Node{

   char *Name;
   int ID;
   int Quantity;
   
   date_rec Receive_Date;
   
   Node *Next;
   
   

};


#endif

Line 48 of function_definition.cpp: When you allocate new storage for a C-style string (char*), you need to add the null-termination character. This means you need to allocate one more char than what is given by strlen(tName) . The same goes for line 663 (in function SaveToFile). As so:

Ptr->Name = new char[strlen(tName)+1];
strcpy(Ptr->Name, tName);
Ptr->Name[strlen(tName)] = '\0'; //set the null-termination character.

In function AddTo_End(), make sure that the Next pointer in NewNode is NULL (better be cautious when there are unknown bugs, even if setting NewNode->Next = NULL; might be redundant after all). The same pretty much holds for AddBefore_InBetween() (but I'm not sure what this function does, similarly, AddAfter_InBetween() is weird, it doesn't seem to actually add a node to the linked-list). Remember that you have no constructor for the Node struct, so it means that the value of Next in a newly created Node could be anything (a "garbage value"), so it needs to be set to NULL explicitly (at least somewhere).

Be const-correct. This usually helps in pointing out silent bugs (like unintentional changes in values). For instance, in function Node* SearchBy_Name(char* tName) , you should take the parameter as const char*. There is no bug there, but making everything const-correct usually helps overall, as a good coding practice.

Although it is probably not a problem, I would usually be a bit more confortable with if(strcmp(tName,i->Name) == 0) as opposed to if(!strcmp(tName,i->Name)) , it is also more idiomatic.

In function DeleteFrom_Start(), you do not check if Head is valid (not NULL), that could crash your application (when checking Head->Next) if you do one call too many. Just take advantage of the short-circuit && operator:

if((Head) && (Head->Next)) //the second check will only occur if the first one passed.

The same goes for DeleteFrom_End().

In function DeleteIn_Between(), you don't check whether the "i" Node is NULL. Have you considered that maybe the Current Node is not part of the list, and thus, the iteration would reach the end before finding it. That would cause a crash with your implementation.

In MenuOperation(), you do a heck of a lot of cin.ignore(); cin.ignore(0); cin.clear(); cin.sync();. Doing that will not hurt, but those calls are so redundant. Just one cin.ignore() in between inputs should do the trick just fine.

There might be other stuff, but I can't go through everything. This should give you a pretty good start at wielding out the bug.


I'm guessing you are not allowed to do so, but if you are, I would highly recommend you use the C++ standard libraries for what you are trying to do. C++ has a std::string class (in #include <string>) which is much nicer to use than C-style strings. And C++ also provides an implementation of a linked-list, called std::list (in #include <list>). This will make your life so much easier, if you are allowed to use those.

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