11 Replies - 1381 Views - Last Post: 28 April 2011 - 02:19 PM Rate Topic: -----

#1 bigbombc4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 11

Linked List Program - Crash on open file getline

Posted 18 April 2011 - 05:54 PM

The program crashes after I open file. I suspect its a problem with my open_file() function..can anyone please help and point out where Im going wrong.





//Link List using class
#include <iostream>
#include <conio>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

//Create class called linklist
class linklist
{     //declare private variables
     private:

             struct list_item //create structure list item
         {
              string content; //create private variable "content"
              string p_num; //create private variable "phone number"
            list_item *nxt; //create pointer "nxt"
         }*tmp1; //create object tmp1 (pointer because we ae interested in addresses not content)

   public:      //public variables

         linklist(); //constructor for class
         void p_result();      //print function declaration
         void add_new( string value, string phone );   //add function declaration
         void remove(string value );  //remove function declaration
         void add_b(string search, string value );//add before function declaration
         void sort_list(int);
         void open_file();
         void unique_list();
         ~linklist();     //destructor for class
};

linklist::linklist()       //constructor function definition
{
     tmp1=NULL;        //empty link list
}

void linklist::P/>_result()    //function definition to print results of linked list
{
     list_item *tmp2;

   for( tmp2 = tmp1 ; tmp2 != NULL ; tmp2 = tmp2->nxt ) //print all content inside each node until a
// node "next" vale points to NULL (last node)
        cout<<endl<<tmp2->content;
        //cout<<endl<<tmp2->p_num;
}


void linklist::open_file()
{

cout<<endl<<"Please enter the name of file you wish to open: ";
char filename[50];
ifstream bucky;
//cin.getline(filename, 50);
cin>>filename;
bucky.open(filename);

if (!bucky.good()){
cout<<endl<<"Problem opening File...";
exit(EXIT_FAILURE);
}
cout<<endl<<"File open :)/>";
char word[150];
bucky >> word;
cout<<endl;

char test;
test = *word;
string tmpsave ("");
list_item *tmp2,*t;         //create pointers of structure list_item

while(bucky.good())
{
	if(*word == '|')
   {//cout<<" ";
    //tmpsave += (" ");
    ///////add node

   cout<<endl<<"tmp1: "<<tmp1->content;
   if( tmp1 == NULL )            //test if list empty
   {
        tmp1 = new list_item;       //create new node with tmp1 pointing to it
      tmp1->content = word;   //pass value into content of node that tmp1 is pointing to
      cout<<endl<<"TEST "<<tmp1->content;
   //   tmp1->p_num = phone;   //pass value into content of node that tmp1 is pointing to
      tmp1->nxt = NULL;        //node that tmp1 is pointing to is now the last node
   }
   	else
   	{
        tmp2 = tmp1;          //address of tmp1 is copied to tmp2
         while( tmp2->nxt != NULL )//loop while the next pointer in node of tmp2 is not equal to NULL//(while it isnt the last node)
         {  tmp2 = tmp2->nxt; }   //tmp2 is now pointing to the node its next pointer was previously
//pointing to

      t = new list_item;     //create node with pointer t pointing to it
  //    t->content = word;
   //   cout<<endl<<"TEST "<<t->content;
      //t->p_num = phone;    //pass value from main function
      t->nxt = NULL;         //pointer in this node points to NULL indicating that it is the last node
      tmp2->nxt = t;         //previous node now has a pointer pointing to the node of pointer t
      }
       //finish add node


   }
   else
	{t->content += word;
   //cout << word;
   }
   bucky >> word;
}
//cout<<endl<<"test out:"<<tmpsave;
cout<<endl;

system ("pause");
//return 0;
getch();
}


void linklist::sort_list(int sum)
//Sort( )
{

    list_item *c,*tmp2,*temp;
    c= tmp1;

    while(c!=NULL){
    tmp2=c->nxt;

        while(tmp2!=NULL){
            if(c->content > tmp2->content){
                temp = new list_item;
                temp->content=c->content;
                c->content=tmp2->content;
                tmp2->content=temp->content;
                delete temp;
            }

        tmp2=tmp2->nxt;
        }

    c=c->nxt;
    }

}


void linklist::unique_list()
//Sort( )
{
    //int count;
    list_item *c,*tmp2,*temp;
    c= tmp1;
    string *lastname;   //create pointer A
    //int lastname[10];   //create pointer A
    int count,i,s_val;
    s_val=0;
    count = 0;
    lastname = new string[count]; //create variable length arra

    while(c!=NULL){
    tmp2=c->nxt;
    s_val=0;
        while(tmp2!=NULL){
            if(c->content == tmp2->content)
            {  s_val = 1;
               cout<<endl<<"multiple occurence";
            }

        tmp2=tmp2->nxt;
        }

    if(s_val == 0)      //copy content if it does not occur again in list
    {
    cout<<endl<<"copy";
    lastname[count]=c->content;
    count++;
    }
    else
    {c=c->nxt;  //skip next node
    }
    cout<<endl<<"unique";

    c=c->nxt;


    }

  for (i=0; i<(count); i++) //do as long as increment value is less than number of characters
  {cout<<lastname[i]; //output array
   cout<<endl;
  }

}


void linklist::add_new(string value,string phone)    //function definition to add a value at end of list
{
     list_item *tmp2,*t;         //create pointers of structure list_item

   if( tmp1 == NULL )            //test if list empty
   {
        tmp1 = new list_item;       //create new node with tmp1 pointing to it
      tmp1->content = value;   //pass value into content of node that tmp1 is pointing to
      tmp1->p_num = phone;   //pass value into content of node that tmp1 is pointing to
      tmp1->nxt = NULL;        //node that tmp1 is pointing to is now the last node
   }
   else
   {
        tmp2 = tmp1;          //address of tmp1 is copied to tmp2
      while( tmp2->nxt != NULL )//loop while the next pointer in node of tmp2 is not equal to NULL//(while it isnt the last node)
           tmp2 = tmp2->nxt;    //tmp2 is now pointing to the node its next pointer was previously
//pointing to

      t = new list_item;     //create node with pointer t pointing to it
      t->content = value;    //pass value from main function
      t->p_num = phone;    //pass value from main function
      t->nxt = NULL;         //pointer in this node points to NULL indicating that it is the last node
      tmp2->nxt = t;         //previous node now has a pointer pointing to the node of pointer t
   }
}

void linklist::remove(string value ) //function definition for delete
{
     list_item *tmp2,*tmp3;
   tmp2 = tmp1;
   if( tmp2->content == value )       //If tmp2 is pointing to first is first node
   {
        tmp1 = tmp2->nxt;
      delete tmp2;                         //remove node that tmp2 is pointing to
      return;                           //stops execution and returns to calling function
   }

   tmp3 = tmp2;
   while( tmp2!=NULL )          //search through nodes to end
   {
        if( tmp2->content == value )  //if content is equal to value
      {
           tmp3->nxt = tmp2->nxt;    //previous node is now linked to node after the one that tmp was
//pointing to
       cout<<endl<<"FOUND STudeNT ID: "<<tmp2->content;
       cout<<endl<<"STudeNT phone#: "<<tmp2->p_num ;
       //  delete tmp2;
         return;
      }

      tmp3 = tmp2;                //both tmp3 and tmp2 points to same node
      tmp2 = tmp2->nxt;           //tmp2 now points to the node that its "next" pointer was pointing to
   }
   cout<<endl << " Data not in List ";  //List searched and data input by user is not present
}

void linklist::add_b(string search, string value )
{
     list_item *tmp2,*tmp3,*tmp4;

   if( tmp1->content == search )   //test if the search value is in first node
   {
        tmp4 = new list_item;  //create new node in list_item
      tmp4->content = value;

      tmp4->nxt = tmp1;
      tmp1 = tmp4;
      return;
   }
   //tmp3 = tmp1;
   tmp2 = tmp1->nxt;
   tmp4 = tmp1;
   while( tmp2!=NULL )
   {
           if( tmp2->content == search ) //test if search value is in current node
             {
           		tmp3 = new list_item; //if true, new node is created
           		tmp3->content = value;  //value is passed from main function and placed in new node
           		tmp3->nxt = tmp2;    //new node is linked to another node by pointing to it
           		tmp4->nxt = tmp3;    //node that was before the other node now points to new node
           		}
   tmp4 = tmp2;
   tmp2 = tmp2->nxt; //moves poiniter from its current node to the node that its "next" pointer was
//ponting to
  }

cout<<endl << " Data not in List ";
}

linklist::~linklist()       //destructor function definition
{
     list_item *tmp2;
   if( tmp1 == NULL )
        return;

   while( tmp1 != NULL )
   {
        tmp2 = tmp1->nxt;
      delete tmp1;
      tmp1 = tmp2;
   }
}

int main()             //main function
{
     linklist mylist;      //create object for class linklist



  string mynum;         //create variable
  string myphone;         //create variable
  int cnt=0;        //create and initialize varaibles
  int sum=0;
  int option_con = 1;

while (option_con == 1)
{cnt++;
 //cout<< "Enter value ";  //print message


  while (cnt>0)      //get values for 10 numbers
  { cout<< endl <<"Please enter value: ";
    cin>> mynum;
    /////cout<< endl <<"Please enter phone number: ";
    ////cin>> myphone;
    sum++;
    mylist.add_new(mynum,myphone);    //pass values to function mylist.add_new
    cnt--;                   //decrement cnt variable by 1
  }
 cout<<"Do you wish to enter another value YES (1) or NO (2)?";
 cin>>option_con;
 //cout<<option_con;
 //getch();
}
   mylist.p_result();      //call function print result

   mylist.open_file();


   mylist.sort_list(sum);

   mylist.p_result();

    mylist.unique_list();

   //cout<< "\n"<< "The sum of all values: " <<sum;  //print sum of variables entered

   getch();       //pause output until user presses a key




 cout<<endl<<"Enter the ID# of student you wish to search for";        //@@@@change from delete/remove
 cin>> mynum;
 mylist.remove(mynum); //pass input to function in object "mylist" in class "linklist"

// cout<<endl<<"Enter the value in the node of which you wish to add new content before ";
 //cin>> cnt;

 //cout<<endl<<"Enter the content of this new node ";
 //cin>> mynum;
// mylist.add_b(cnt,mynum);     //  "pass values to add_b function

 //mylist.p_result();

 getch();

}


edit by ishkabible: please use code tags when posting code, thank you.

This post has been edited by ishkabible: 18 April 2011 - 06:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Linked List Program - Crash on open file getline

#2 jimblumberg   User is online

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,258
  • Joined: 25-December 09

Re: Linked List Program - Crash on open file getline

Posted 18 April 2011 - 07:35 PM

Quote

The program crashes after I open file. I suspect its a problem with my open_file() function..can anyone please help and point out where Im going wrong.

I don't see how the above code could crash, since it won't compile. Of course there is a problem with your open_file() function, it does not exist in the posted code.

The errors I get when I compile your code:

Quote

main.cpp|13|warning: ‘class linklist’ has pointer data members|
main.cpp|13|warning: but does not override ‘linklist(const linklist&)’|
main.cpp|13|warning: or ‘operator=(const linklist&)’|
main.cpp|41|error: no ‘void linklist::P_result()’ member function declared in class ‘linklist’|
main.cpp||In member function ‘void linklist::open_file()’:|
main.cpp|125|warning: unused parameter ‘sum’|
main.cpp||In member function ‘void linklist::unique_list()’:|
main.cpp|157|warning: unused variable ‘temp’|


Jim

This post has been edited by jimblumberg: 18 April 2011 - 07:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 bigbombc4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 11

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 07:56 AM

oh thanks..ok..I changed some lines of code and tried a different approach...however, now When I search for Lastname..it wont find any that is imported...here is the database. Any help would be deeply appreciated



My code:
//Link List using class
#include <iostream>
#include <conio>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <cstring>
//#include <limits>


//using namespace std;

//Create class called linklist
class linklist
{     //declare private variables
     private:

             struct list_item //create structure list item
         {
              string idnum; //create private variable "idnum"
              string l_name; //create private variable "last name"
              string f_name; //create private variable "first name"
              string c_name; //create private variable "course name"
              string par_name; //create private variable "parish name"
            list_item *nxt; //create pointer "nxt"
         }*tmp1; //create object tmp1 (pointer because we ae interested in addresses not idnum)

   public:      //public variables

         linklist(); //constructor for class
         void p_result();      //print function declaration
         void add_new(string id_num,string lname, string fname, string cname, string parname);//add before function declaration
         void sort_list(int);
         void sort_list_ln(int);
         void open_file();
         void searchls(string);
         void searchid(string);
         void unique_list();
         void parish_list(string parish_select);
         ~linklist();     //destructor for class
};

linklist::linklist()       //constructor function definition
{
     tmp1=NULL;        //empty link list
}

void linklist::P_result()    //function definition to print results of linked list
{
     list_item *tmp2;

   for( tmp2 = tmp1 ; tmp2 != NULL ; tmp2 = tmp2->nxt ) //print all idnum inside each node until a
// node "next" vale points to NULL (last node)
      {  cout<<endl<<"ID NUMBER: "<<tmp2->idnum;
        cout<<endl<<"LAST NAME: "<<tmp2->l_name;
        cout<<endl<<"FIRST NAME: "<<tmp2->f_name;
        cout<<endl<<"COURSE NAME: "<<tmp2->c_name;
        cout<<endl<<"PARISH: "<<tmp2->par_name;
        }
}








void linklist::open_file()

{
list_item *tmp2,*t;
string extraction;
   int ans =0;


   	cout <<endl<< "########################READING FROM FILE###########################";
   	ifstream Readfile ("mydatabase.txt");
   	cout<<"\n\n";

   	if (Readfile.good())
   	{
   		while(!Readfile.eof())
      	{



///////////////////////////////////////////////////////////

              if( tmp1 == NULL )            //test if list empty
               {
                  tmp1 = new list_item;       //create new node with tmp1 pointing to it
                  getline (Readfile,tmp1->idnum);   //pass value into idnum of node that tmp1 is pointing to
                  getline (Readfile,tmp1->l_name);   //pass value into lastname of node that tmp1 is pointing to
                  getline (Readfile,tmp1->f_name);   //pass value into firstname of node that tmp1 is pointing to
                  getline (Readfile,tmp1->c_name);   //pass value into coursename of node that tmp1 is pointing to
                  getline (Readfile,tmp1->par_name);   //pass value into idnum of node that tmp1 is pointing to

                    tmp1->nxt = NULL;        //node that tmp1 is pointing to is now the last node
                 }
              	else
            	{
               tmp2 = tmp1;          //address of tmp1 is copied to tmp2
               while( tmp2->nxt != NULL )//loop while the next pointer in node of tmp2 is not equal to NULL//(while it isnt the last node)
                     {  tmp2 = tmp2->nxt; }   //tmp2 is now pointing to the node its next pointer was previously
//pointing to

                t = new list_item;     //create node with pointer t pointing to it
                getline (Readfile,t->idnum);   //pass value into idnum of node that tmp1 is pointing to
                  getline (Readfile,t->l_name);   //pass value into lastname of node that tmp1 is pointing to
                  getline (Readfile,t->f_name);   //pass value into firstname of node that tmp1 is pointing to
                  getline (Readfile,t->c_name);   //pass value into coursename of node that tmp1 is pointing to
                  getline (Readfile,t->par_name);   //pass value into idnum of node that tmp1 is pointing to

                t->nxt = NULL;         //pointer in this node points to NULL indicating that it is the last node
                tmp2->nxt = t;         //previous node now has a pointer pointing to the node of pointer t
               }
       //finish add node


         }



    }

      	if (Readfile.eof())
      	{ cout<<endl<<"End of Records";	}
   	   else
   	   {cout<<"\nERROR~:Unable to open file!";	}
    Readfile.clear();

getch();
}










void linklist::sort_list_ln(int sum)
//Sort( )
{ cout<<endl<<"List Sorted by Lastname: ";

    list_item *c,*tmp2,*temp;
    c= tmp1;

    while(c!=NULL){
    tmp2=c->nxt;

        while(tmp2!=NULL){
            if(c->l_name > tmp2->l_name){
                temp = new list_item;
                temp->idnum=c->idnum;
                temp->l_name=c->l_name;
                temp->f_name=c->f_name;
                temp->c_name=c->c_name;
                temp->par_name=c->par_name;

                c->idnum=tmp2->idnum;
                c->l_name=tmp2->l_name;
                c->f_name=tmp2->f_name;
                c->c_name=tmp2->c_name;
                c->par_name=tmp2->par_name;

                tmp2->idnum=temp->idnum;
                tmp2->l_name=temp->l_name;
                tmp2->f_name=temp->f_name;
                tmp2->c_name=temp->c_name;
                tmp2->par_name=temp->par_name;


                delete temp;
            }

        tmp2=tmp2->nxt;
        }

    c=c->nxt;
    }

}


void linklist::sort_list(int sum)
//Sort( )
{ cout<<endl<<"List Sorted by ID: ";

    list_item *c,*tmp2,*temp;
    c= tmp1;

    while(c!=NULL){
    tmp2=c->nxt;

        while(tmp2!=NULL){
            if(c->idnum > tmp2->idnum){
                temp = new list_item;
                temp->idnum=c->idnum;
                temp->l_name=c->l_name;
                temp->f_name=c->f_name;
                temp->c_name=c->c_name;
                temp->par_name=c->par_name;

                c->idnum=tmp2->idnum;
                c->l_name=tmp2->l_name;
                c->f_name=tmp2->f_name;
                c->c_name=tmp2->c_name;
                c->par_name=tmp2->par_name;

                tmp2->idnum=temp->idnum;
                tmp2->l_name=temp->l_name;
                tmp2->f_name=temp->f_name;
                tmp2->c_name=temp->c_name;
                tmp2->par_name=temp->par_name;


                delete temp;
            }

        tmp2=tmp2->nxt;
        }

    c=c->nxt;
    }

}


void linklist::unique_list()
//Sort( )
{   cout<<endl<<"Unique Lastnames: ";

    list_item *c,*tmp2,*temp;
    c= tmp1;
   //// string *lastname;   //create pointer A
    string lastname[150];   //create pointer A
    int count,i,s_val;
    s_val=0;
    count = 0;
   /// lastname = new string[count]; //create variable length arra

    while(c!=NULL)
    {
    tmp2=c->nxt;
    s_val=0;
        while(tmp2!=NULL){
            if(c->l_name == tmp2->l_name)
            {  s_val = 1;
              //// cout<<endl<<"multiple occurence";
            }

        tmp2=tmp2->nxt;
        }

    if(s_val == 0)      //copy idnum if it does not occur again in list
    {
   //// cout<<endl<<"copy";
    lastname[count]=c->l_name;
    count++;
    }
    else
    {c=c->nxt;  //skip next node
    }
//    cout<<endl<<"unique";

    c=c->nxt;


    }

  for (i=0; i<(count); i++) //do as long as increment value is less than number of characters
  {cout<<lastname[i]; //output array
   cout<<endl;
  }

}



void linklist::parish_list(string parish_select)
//Sort( )
{
    cout<<endl<<"Search PARISH: "<<parish_select;
    list_item *c,*temp;
    c = tmp1;
   //// string *lastname;   //create pointer A
    string parish[150];   //create pointer A
    ;

    while(c!=NULL)
    {



            if(c->par_name == (parish_select))
            {cout<<endl;
             ///////////////////////////
             cout<<endl<<"ID NUMBER: "<<c->idnum;
        		cout<<endl<<"LAST NAME: "<<c->l_name;
        		cout<<endl<<"FIRST NAME: "<<c->f_name;
            cout<<endl<<"COURSE NAME: "<<c->c_name;
        		cout<<endl<<"PARISH: "<<c->par_name;
            }


     c=c->nxt;

    }

 // for (i=0; i<(count); i++) //do as long as increment value is less than number of characters
 // {cout<<endl<<parish[i]; //output array
 //  cout<<endl;
  //}

}




void linklist::add_new(string id_num,string lname, string fname, string cname, string parname)    //function definition to add a value at end of list
{
     list_item *tmp2,*t;         //create pointers of structure list_item

   if( tmp1 == NULL )            //test if list empty
   {
        tmp1 = new list_item;       //create new node with tmp1 pointing to it
      tmp1->idnum = id_num;   //pass value into idnum of node that tmp1 is pointing to
      tmp1->l_name = lname;   //pass value into idnum of node that tmp1 is pointing to
      tmp1->f_name = fname;
      tmp1->c_name = cname;
      tmp1->par_name = parname;
      tmp1->nxt = NULL;        //node that tmp1 is pointing to is now the last node
   }
   else
   {
        tmp2 = tmp1;          //address of tmp1 is copied to tmp2
      while( tmp2->nxt != NULL )//loop while the next pointer in node of tmp2 is not equal to NULL//(while it isnt the last node)
           tmp2 = tmp2->nxt;    //tmp2 is now pointing to the node its next pointer was previously
//pointing to

      t = new list_item;     //create node with pointer t pointing to it
      t->idnum = id_num;    //pass value from main function
      t->l_name = lname;    //pass value from main function
      t->f_name = fname;
      t->c_name = cname;
      t->par_name = parname;
      t->nxt = NULL;         //pointer in this node points to NULL indicating that it is the last node
      tmp2->nxt = t;         //previous node now has a pointer pointing to the node of pointer t
   }
}

void linklist::searchls(string value ) //function definition for delete
{    cout<<endl<<"Search by Lastname and Display all: "<<value;
     list_item *tmp3;
   //  cout<<endl<<"Test tmp1 lastname: "<<tmp1->l_name;
   tmp3 = tmp1;

   while( tmp3!=NULL )          //search through nodes to end
   {
        if( tmp3->l_name == value )  //if idnum is equal to value
      {

       cout<<endl<<"FOUND Student ID: "<<tmp3->idnum;
       cout<<endl<<"Student Lastname#: "<<tmp3->l_name ;
       cout<<endl<<"Student Firstname#: "<<tmp3->f_name ;
       cout<<endl<<"Student Coursename#: "<<tmp3->c_name ;
       cout<<endl<<"Student Parish#: "<<tmp3->par_name ;

         return;
      }

     // tmp3 = tmp2;                //both tmp3 and tmp2 points to same node
      tmp3 = tmp3->nxt;           //tmp2 now points to the node that its "next" pointer was pointing to
   }

   cout<<endl << " Data not in List ";  //List searched and data input by user is not present
}

void linklist::searchid(string value ) //function definition for delete
{
     list_item *tmp2,*tmp3;
   tmp2 = tmp1;

   while( tmp2!=NULL )          //search through nodes to end
   {
        if( tmp2->idnum == value )  //if idnum is equal to value
      {

       cout<<endl<<"FOUND Student ID: "<<tmp2->idnum;
       cout<<endl<<"Student Lastname#: "<<tmp2->l_name ;
       cout<<endl<<"Student Firstname#: "<<tmp2->f_name ;
       cout<<endl<<"Student Coursename#: "<<tmp2->c_name ;
       cout<<endl<<"Student Parish#: "<<tmp2->par_name ;

         return;
      }

     // tmp3 = tmp2;                //both tmp3 and tmp2 points to same node
      tmp2 = tmp2->nxt;           //tmp2 now points to the node that its "next" pointer was pointing to
   }
   cout<<endl << " Data not in List ";  //List searched and data input by user is not present
}











linklist::~linklist()       //destructor function definition
{
     list_item *tmp2;
   if( tmp1 == NULL )
        return;

   while( tmp1 != NULL )
   {
        tmp2 = tmp1->nxt;
      delete tmp1;
      tmp1 = tmp2;
   }
}

int main()             //main function
{
     linklist mylist;      //create object for class linklist



  string mynum;         //create variable
  string mylname;         //create variable
  string myfname;
  string mycname;
  string myp;
  int cnt=0;        //create and initialize varaibles
  int sum=0;
  int option_con = 1;

while (option_con == 1)
{cnt++;
cout<< "RECORD ENTRY ";  //print message


while (cnt>0)      //get values for 10 numbers
  { cout<< endl <<"Please enter ID#: ";
    cin>> mynum;
   cout<< endl <<"Please enter Last Name: ";
    cin>> mylname;
    cout<< endl <<"Please enter First Name: ";
    cin>> myfname;
    cout<< endl <<"Please enter Course Name: ";
    cin>> mycname;
    cout<< endl <<"Please enter Parish: ";
    cin>> myp;
    sum++;
    mylist.add_new(mynum,mylname,myfname,mycname,myp);    //pass values to function mylist.add_new
    cnt--;                   //decrement cnt variable by 1
  }


 cout<<"Do you wish to enter another record YES (1) or NO (2)?";
 cin>>option_con;
 //cout<<option_con;
 //getch();
}
  mylist.p_result();      //call function print result

   mylist.open_file();


   mylist.sort_list(sum);

   mylist.p_result();

   mylist.unique_list();

   //cout<< "\n"<< "The sum of all values: " <<sum;  //print sum of variables entered

   getch();       //pause output until user presses a key






  cout<<endl<<"Enter the Lastname of student you wish to search for: ";        //@@@@change from delete/searchls
 cin>> mylname;
 mylist.searchls(mylname); //pass input to function in object "mylist" in class "linklist"

 cout<<endl<<"Enter the ID# of student you wish to search for: ";        //@@@@change from delete/searchls
 cin>> mynum;
 mylist.searchid(mynum); //pass input to function in object "mylist" in class "linklist"

 cout<<endl<<"Enter the Parish of the records you wish to display: ";        //@@@@change from delete/searchls
 cin>> myp;
 mylist.parish_list(myp); //pass input to function in object "mylist" in class "linklist"

 getch();

}


MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 28 April 2011 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#4 bigbombc4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 11

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 08:07 AM

Database File

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is online

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,258
  • Joined: 25-December 09

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 08:20 AM

First use code tags!!!!

Does this program even compile?

If not post the complete error messages.

Note: These are the errors I receive when I compile your code.

Quote

main.cpp|21|error: ‘string’ does not name a type|
main.cpp|22|error: ‘string’ does not name a type|
main.cpp|23|error: ‘string’ does not name a type|
main.cpp|24|error: ‘string’ does not name a type|
main.cpp|25|error: ‘string’ does not name a type|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|37|error: ‘string’ has not been declared|
main.cpp|38|error: ‘string’ has not been declared|
main.cpp|40|error: ‘string’ has not been declared|
main.cpp|16|warning: ‘class linklist’ has pointer data members|
main.cpp|16|warning: but does not override ‘linklist(const linklist&)’|
main.cpp|16|warning: or ‘operator=(const linklist&)’|
main.cpp||In constructor ‘linklist::linklist()’:|
main.cpp|44|warning: ‘linklist::tmp1’ should be initialized in the member initialization list|
main.cpp|49|error: no ‘void linklist::P_result()’ member function declared in class ‘linklist’|
main.cpp||In member function ‘void linklist::open_file()’:|
main.cpp|74|error: ‘string’ was not declared in this scope|
main.cpp|74|error: expected ‘;’ before ‘extraction’|
main.cpp|78|error: ‘cout’ was not declared in this scope|
main.cpp|78|error: ‘endl’ was not declared in this scope|
main.cpp|79|error: ‘ifstream’ was not declared in this scope|
main.cpp|79|error: expected ‘;’ before ‘Readfile’|
main.cpp|82|error: ‘Readfile’ was not declared in this scope|
main.cpp|94|error: ‘struct linklist::list_item’ has no member named ‘idnum’|
main.cpp|95|error: ‘struct linklist::list_item’ has no member named ‘l_name’|
main.cpp|96|error: ‘struct linklist::list_item’ has no member named ‘f_name’|
main.cpp|97|error: ‘struct linklist::list_item’ has no member named ‘c_name’|
main.cpp|98|error: ‘struct linklist::list_item’ has no member named ‘par_name’|
main.cpp|110|error: ‘struct linklist::list_item’ has no member named ‘idnum’|
main.cpp|111|error: ‘struct linklist::list_item’ has no member named ‘l_name’|
main.cpp|112|error: ‘struct linklist::list_item’ has no member named ‘f_name’|
main.cpp|113|error: ‘struct linklist::list_item’ has no member named ‘c_name’|
main.cpp|114|error: ‘struct linklist::list_item’ has no member named ‘par_name’|
main.cpp|128|error: ‘Readfile’ was not declared in this scope|
main.cpp|132|error: ‘Readfile’ was not declared in this scope|
main.cpp|134|error: ‘getch’ was not declared in this scope|
main.cpp|75|warning: unused variable ‘ans’|
main.cpp||In member function ‘void linklist::sort_list_ln(int)’:|
main.cpp|148|error: ‘cout’ was not declared in this scope|
main.cpp|148|error: ‘endl’ was not declared in this scope|
main.cpp|157|error: ‘struct linklist::list_item’ has no member named ‘l_name’|
main.cpp|157|error: ‘struct linklist::list_item’ has no member named ‘l_name’|
main.cpp|159|error: ‘struct linklist::list_item’ has no member named ‘idnum’|
main.cpp|159|error: ‘struct linklist::list_item’ has no member named ‘idnum’|
main.cpp|160|error: ‘struct linklist::list_item’ has no member named ‘l_name’|
main.cpp|160|error: ‘struct linklist::list_item’ has no member named ‘l_name’|
main.cpp|161|error: ‘struct linklist::list_item’ has no member named ‘f_name’|
main.cpp|161|error: ‘struct linklist::list_item’ has no member named ‘f_name’|
main.cpp|162|error: ‘struct linklist::list_item’ has no member named ‘c_name’|
main.cpp|162|error: ‘struct linklist::list_item’ has no member named ‘c_name’|
main.cpp|163|error: ‘struct linklist::list_item’ has no member named ‘par_name’|
main.cpp|163|error: ‘struct linklist::list_item’ has no member named ‘par_name’|
main.cpp|165|error: ‘struct linklist::list_item’ has no member named ‘idnum’|
main.cpp|165|error: ‘struct linklist::list_item’ has no member named ‘idnum’|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 5 warnings ===|


Jim
Was This Post Helpful? 0
  • +
  • -

#6 Astraeus   User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 205
  • Joined: 17-November 09

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 08:38 AM

Quote

main.cpp|21|error: ‘string’ does not name a type|
main.cpp|22|error: ‘string’ does not name a type|
main.cpp|23|error: ‘string’ does not name a type|
main.cpp|24|error: ‘string’ does not name a type|
main.cpp|25|error: ‘string’ does not name a type|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|33|error: ‘string’ has not been declared|
main.cpp|37|error: ‘string’ has not been declared|
main.cpp|38|error: ‘string’ has not been declared|
main.cpp|40|error: ‘string’ has not been declared|


You commented out using namespace std; ^^
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is online

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,258
  • Joined: 25-December 09

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 08:42 AM

Quote

You commented out using namespace std; ^^


There is also no include file for the string class. And there will be other errors as well.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 Astraeus   User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 205
  • Joined: 17-November 09

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 09:04 AM

Quote

Quote

You commented out using namespace std;


There is also no include file for the string class. And there will be other errors as well.

Jim


There isn't. I've noticed, though that depending on the compiler you do not have to declare the string header for some reason.
And there are declaring the standard namespace and #include <string> will clear up all 13 or so of the string related errors. :)
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is online

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,258
  • Joined: 25-December 09

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 09:33 AM

Quote

There isn't. I've noticed, though that depending on the compiler you do not have to declare the string header for some reason.
And there are declaring the standard namespace and #include <string> will clear up all 13 or so of the string related errors. :)


True some compilers include string in another include file (iostream or others). But you should never rely on the compiler including the correct include files. If you use an item in a header file then include the header file.

Actually using namespace std; and including the proper header file will probably clear up most if not all of these errors. But there are more errors that will show up when these problems are fixed.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 bigbombc4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 11

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 04:43 PM

View Postjimblumberg, on 20 April 2011 - 09:33 AM, said:

Quote

There isn't. I've noticed, though that depending on the compiler you do not have to declare the string header for some reason.
And there are declaring the standard namespace and #include <string> will clear up all 13 or so of the string related errors. :)


True some compilers include string in another include file (iostream or others). But you should never rely on the compiler including the correct include files. If you use an item in a header file then include the header file.

Actually using namespace std; and including the proper header file will probably clear up most if not all of these errors. But there are more errors that will show up when these problems are fixed.

Jim







Thanks Guys!
I corrected the code as suggested..however I get an error when I include namespace..so I left it out..I am using Borland c++ 5. The code seems to run perfectly now....except I have a problem with string compare..I have tried many things Ive seen online but cant effectively compare two strings..the program compiles but it does not indicate that condition is met..instead it seems condition is never met..even when the test values indicate that they are...
Here is the section of code:
if ( !tmp3->l_name.compare(value) )//if lastname is equal to value

       {
       cout<<endl<<"FOUND Student ID: "<<tmp3->idnum;
       cout<<endl<<"Student Lastname#: oadpic.org/v.php?img=WuJRgNIMUm[/img]"<<tmp3->l_name ;
       cout<<endl<<"Student Firstname#: "<<tmp3->f_name ;
       cout<<endl<<"Student Coursename#: "<<tmp3->c_name ;
       cout<<endl<<"Student Parish#: "<<tmp3->par_name ;

        return;

}

output screenshot attached.
<a href="http://uploadpic.org/v.php?img=WuJRgNIMUm">view image</a>

This post has been edited by JackOfAllTrades: 28 April 2011 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is online

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,258
  • Joined: 25-December 09

Re: Linked List Program - Crash on open file getline

Posted 20 April 2011 - 05:10 PM

Please post your current code and use code tags.

Part of the problem is you are using a pre-standard compiler and one of there may be differences between Borland's strings and the standard strings. So I need to see the actual code.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 bigbombc4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 11

Re: Linked List Program - Crash on open file getline

Posted 28 April 2011 - 02:19 PM

View Postjimblumberg, on 20 April 2011 - 05:10 PM, said:

Please post your current code and use code tags.

Part of the problem is you are using a pre-standard compiler and one of there may be differences between Borland's strings and the standard strings. So I need to see the actual code.

Jim


THANK YOU FOR YOUR SUGGESTIONS..THEY HELPED ALOT! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1