Start New Discussion within our Software Development Community

I was given an assignment to complete on database, i have created it and also works but crashes after executing once, check it friends and please help, if you can modify or make it works my time is really limited and am unable to find a solution, friends be graceful to share your knowledge.

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

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

1. Do not use *.cpp files as includes in other *.cpp files. rename those two *.cpp files (functions_prototype.cpp and struct.cpp) to *.h.

2. Add <string> to functions_prototype.h. Many compilers, such as vc++ 2010 do not automatically include that header file.

3. Several times you have forgotten to add 1 to strlen(tName) when allocating new memory.

4. in saveToFile(), calculate the length of tName only once and use that calculation in the loop. Its bad practice to call strlen() in a loop when the length never changes because its too time consuming.

//add "+" to join name with spaces  
        size_t len = strlen(tName);     // <<<< AD
        for (size_t j=0; j<len-1; ++j)  // <<<< AD
        {
             if(tName[j] == ' ') tName[j] = '+';
        }

5. MenuOperation(), case '1'. Here is the correct way to clear the cin input buffer. Read this thread for more detailed information.

//cin.ignore(); // <<< AD
         //cin.ignore(0);
         //cin.clear();
         //cin.sync();
         cin.ignore ( std::numeric_limits<std::streamsize>::max(), cin.widen ( '\n' ) );

There are other errors that I have not tried to find. Fix the above and you will be able to do more than just the first record that you mentioned. I don't know what compiler you are using, but you need to learn how to use your compiler's debugger so that you can easily single-step through the program and set breakpoints. I found all the above problems in about 15 minutes doing that with vc++ 2010 express.

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