Hey everyone,

I am trying to get my school program on pointer variable linked lists to work, but I have incountered a runtime error. When I try to print my data to the screen or a file, it does not work. I am not sure if my program is not reading correctly, or just not printing. I have attached the code and the datafile below.

CODE:

//Assignment 8
//Linked Lists
//Pointer Variable Implementation
//Devang Joshi

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <fstream>
using namespace std;

//*************************************************************************************************  
      				                   /* Global Variables */    
//*************************************************************************************************
 
    const int NAMESIZE=25;
    const int FILENAME=51;
    const int MAXLISTSIZE=200;       
  
    typedef char Name_t[NAMESIZE];
    typedef char Filename_t[FILENAME];
    typedef int ID_t;
    typedef double Bal_t;
    typedef fstream Infile_t;
    typedef fstream Outfile_t;
    int NIL=-1; 
 
      
//*************************************************************************************************
      				               /* Class Declerations */   
//*************************************************************************************************

    class cAccount
    {
		private:
			
			Name_t fname;
      		Name_t lname;
      		ID_t aID;
      		Bal_t aBal;       

      	public:       

      		cAccount();                       
      		void Read (Infile_t &Infile);     
      		void Print (Outfile_t &Outfile);  
      		void Display();                   
      	    int compID2ID(cAccount NewAccount);      		   

      };// end cAccount       
//*************************************************************************************************
      				              /* Struct Declerations */
//*************************************************************************************************
 
      struct LLEle_t
      {
	      cAccount Account;
      	  LLEle_t * Link; 

      };// end ListEle_t     
//*************************************************************************************************
				                   /* Link Decleration */ 
//*************************************************************************************************

      typedef LLEle_t * Link_t;      

//*************************************************************************************************
      			             /* Class Function Implementations */       
//*************************************************************************************************
      cAccount::cAccount()
      {
		  strcpy(fname,"");
      	  strcpy(lname,"");                                                
      	  aID=0;
      	  aBal=0;  
	  
	  }// end cAccount::cAccount       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 
      void cAccount::Read(Infile_t &Infile)
	  { Infile>>aID>>lname>>fname>>aBal;}// end cAccount::Read              
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void cAccount::Print(Outfile_t &Outfile)  
      {
	      Outfile<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl; 
      	  Outfile<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;      
      	  Outfile<<setw(10)<<"ACCOUNT BALANCE: $"<<aBal<<endl; 
      	  Outfile<<"------------------------------------------------"<<endl;  
  
	  }// end cAccount::Print           
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void cAccount::Display()
      {
	      cout<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl;
      	  cout<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;        
      	  cout<<setw(10)<<"ACCOUNT BALANCE: $"<<aBal<<endl;
      	  cout<<"------------------------------------------------"<<endl;       
 
      }// end cAccount::Display       
 //_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      int cAccount::compID2ID(cAccount NewAccount)
      {
		  int count;
      	  count=0;       

      	  if(this->aID > NewAccount.aID)
      	  { count=-1;}// end if     

	      else if(this->aID < NewAccount.aID)       
      	  { count=1; }// end else if       

		  else
      	  { count=0;}//end else       

      	  return count;

      }// end cAccount::compID2ID    
//*************************************************************************************************
      				                   /* Function Prototypes */
//*************************************************************************************************
 
      void Insert(Link_t &Head, Link_t &Pred,Link_t &Start,cAccount NewAccount,LLEle_t * Link); 
      void Search4Pred(Link_t &Pred,Link_t List,Link_t Start, cAccount Account);  
      void Add(Link_t &Avail,Link_t &Start, cAccount NewAccount,LLEle_t *Link);  
      void Load(Link_t &Start,Link_t &Avail,LLEle_t * Link);  
      void Display(Link_t Start, Link_t Avail);
      void Print(Link_t Start,Link_t Avail);   
      
//*************************************************************************************************
      					                       /* MAIN */       
//*************************************************************************************************

      int main()
      {
		  int COUNT=0; 
      	  Link_t Start=0; 
      	  Link_t Avail=0;
		  LLEle_t * Link=NULL;
      	  cAccount Account;
      	  Infile_t Infile;

          /* FUNCTION CALLS */

      	  Load(Start,Avail,Link);
      	  Display(Start,Avail);
      	  Print(Start,Avail);
      	  
      	  return 0;

      }// end main 
//************************************************************************************************* 
      				               /* Function Implementations */   
//*************************************************************************************************
      void Insert(Link_t &Head, Link_t &Pred,Link_t &Start,cAccount NewAccount,LLEle_t * Link)
      {
          Link_t Curr;
      	  Curr=new(LLEle_t);	
      	  Curr->Account=NewAccount;//<-----------breaks here   
 
      	  if(Pred!=NULL)
      	  {
		      Curr->Link=Pred->Link;        
      		  Pred->Link=Curr;
      	  }    
	      else
      	  {
      		  Curr->Link = Start;
      		  Start = Curr;
      	  }

      };// end Insert       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Search4Pred(Link_t &Pred,Link_t Start, cAccount Account)
      {
	      bool Done=false; 
      	  Link_t Curr;
      	  Curr=Start;
      	  Pred=NULL;       

     	  while((Done!=true)&&(Curr!=NULL))
      	  {
			  if(Curr->Account.compID2ID(Account)>0)   
      		  {
			      Pred=Curr;
      			  Curr=Pred->Link;
      		  }
      		  else
      		  { Done=true;}
      	  }//end while
      }// end Search4Pred       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Add(Link_t &Avail,Link_t &Start, cAccount NewAccount,LLEle_t *Link)
      {
	      Link_t Pred;

      	  Search4Pred(Pred,Start,NewAccount);               
      	  Insert(Start,Pred,Avail,NewAccount,Link);

	  }// end Add       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Load(Link_t &Start,Link_t &Avail,LLEle_t * Link)
      {
	      Filename_t Filename;
      	  Infile_t Infile;
      	  Link_t Curr=0; 
      	  Link_t Pred=0; 
	      cout<<"Program Starting.........."<<endl;
      	  cout<<"Begining Load.........."<<endl<<endl;
      	  cout<<"Enter the name of the data file to be used:"<<endl;  
      	  cin>>Filename;
      	  Infile.open(Filename,ios::in);
      	  cAccount NewAccount; 
      	  NewAccount.Read(Infile);
      	  while(!Infile.eof())
      	  {
		      Add(Avail,Start,NewAccount,Link);
      		  NewAccount.Read(Infile);

      	  }//end while
 
      	  Infile.close();
      	  cout<<"Read Complete........."<<endl;
	      cout<<"Load Complete........."<<endl;

      };//end load
 //_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Display(Link_t Start, Link_t Avail)
      {
	      Link_t Curr;
      	  Curr=Start;
      	  Name_t choice;
      	  cout<<"Would you like to view the output file?"<<endl;
      	  cout<<"(YES or NO)"<<endl;
      	  cin>>choice;

      	  if(strcmp(choice,"YES")==0||strcmp(choice,"Yes")==0||strcmp(choice,"yes")==0) 
      	  {
			  cout<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;       

      		  while(Curr!=NULL)                                             
      		  {
			      Curr->Account.Display();
      			  Curr=Curr->Link;

      		  }//end while

			  cout<<endl;
			  cout<<"Moving to next process.........."<<endl<<endl;

      	  }//end if
      	  else
      	  {
		      cout<<"Very Well.........."<<endl;
		      cout<<"Moving to next process.........."<<endl<<endl;
      	  }

      };// end Display
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Print(Link_t Start,Link_t Avail)
	  {
	      Link_t Curr;
      	  Curr=Start; 
      	  Filename_t Filename;
      	  Outfile_t Outfile;
      	  cout<<"Enter the name of the output file: "<<endl;
      	  cin>>Filename; 
      	  Outfile.open(Filename,ios::out);
      	  Outfile<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;

      	  while(Curr!=NULL)
      	  {
			  Curr->Account.Print(Outfile);
      		  Curr=Curr->Link;

      	  }//end while       

      	  Outfile.close();
      	  cout<<"Write Sucessful.........."<<endl<<endl;

      };// end Print
//*************************************************************************************************
				                   /* END OF SOURCE CODE */
//*************************************************************************************************

DATA:

31951
Walker
John
1000.98
69537  
Patton  
George
876.07
28542
Colt
Samuel 
1234.56
28325
Bond
James
6798.00
68128 
Hamilton  
Alex
9000.00
44206
Lincoln
Abe
00.01 
13108
Armstrong
Louis  
750.00 
41727
Gotti
John
5500.60
21158
Lennon
John
6969.69 
52019
McCarthy
Joseph
742.02
37823 
Stewart  
Jon 
29.98 
70109  
Schrute  
Dwight 
00.00
74746  
Bob
Billy
50.50
18211
Pitt
Brad
2056.89
19724
Blitzer
Wolf
3009.10 
49926  
Dillinger 
John
862.43

And we're supposed to magically figure out which of the 283 lines might be your problem?

I am trying to get my school program on pointer variable linked lists to work, but I have incountered a runtime error.

Bummer. Since there are quite a few runtime errors, is there a particular one you're interested in?

When I try to print my data to the screen or a file, it does not work.

Like it prints the Declaration of Independence instead? Or prints garbage?

I am not sure if my program is not reading correctly, or just not printing.

There are very simple ways you can use to decide if it's a reading problem. After you read, display what is read...

I know I sound annoyed but posting 283 lines of code with no real explanation (details) of the problem, nor any direction where to look, is not helpful.

You seem bright enough to know that since we didn't write the code nor can we see your screen, you've given us nothing to work with. What's the first thing your mechanic asks when you tell him "my car won't work?" Same thing with programming....

First when you encounter a problem you should break it down it to small chunks. Therefore start with some very simple data.

is your code crashing or just not giving the output that you would expect?

As a rule it is not a good idea to use ofstream as a function parameter.

you should check is_open() before using the ofstream you have too many typedefs

It might be better to have

//#include <string>
std::string get_print_line();
bool print(std::string &file_dest, std::string &data);

I think your problem is Line 273
Link is still 0 you never set it anywhere it is tricky to follow some of your logic so I could well be missing other things.

another thing to look out for is that your pointers are not pointing to temporary variables.

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.