I had just started learning C++ for couple months
n this program is involved with pointer
there is a bug in my "doInsert" function
however, I dont seem to be able to figure out what is wrong with it
every time after I enter the "position" the program crashed
can someone kindly look over the code n tell me what is going on??
should be only the "doInsert"

ps.I decided to post the entire code for better understanding

// Joseph Chien, CS 536, Section #3132
// Lab 1, 9/20, McCollum

#include <iostream>
#include <stdlib.h>  
#include <assert.h>
#include <string.h>
#include <fstream>
using namespace std;

struct Listnode {
  char lastname[30];
  char firstname[30];
  int age;
};

struct Listtype {
  Listnode* person[30];
  int size;
};

//prototype
Listtype initialize(void);
int menu();
void fillList(Listtype &List);
void printList(Listtype List);
void doInsert(Listtype &List);
void doDelete(Listtype &List);
void deallocate(Listtype &List);

int main () {
  Listtype List;
  int choice;
  initialize();
  do
  {  system("cls");  //clears the screen
     choice = menu ();
     switch (choice)
     {  case 1: fillList(List); break;
        case 2: printList(List); break;
        case 3: doInsert(List); break;
        case 4: doDelete(List); break;
        case 5: break;
        default: cout<<"\nINVALID...1-5 ONLY!\n";
                 cin.ignore();  //flushes the input buffer
                 cin.ignore();  //holds the screen
     }
  }while(choice!=5);
  deallocate(List);
  return 0;
}

Listtype initialize(void)
{  Listtype L;
   for(int i=0; i<30; i++)
     L.person[i]=NULL;
   L.size=0;
   return L;        
}
int menu()
{  int result;
   cout<<"MENU FOR LAB1\n";
   cout<<"1. Fill the list from file LAB1DATA.TXT\n";
   cout<<"2. Print the list (include the size of the list)\n";
   cout<<"3. Insert a record into the list\n";
   cout<<"4. Delete a record from the list\n";
   cout<<"5. Quit\n\n";
   cout<<"ENTER YOUR CHOICE: ";
   cin>>result;
   return result;
}
void fillList(Listtype &List)
{  char lastname[30], firstname[30];
   int age, i=0;
   ifstream infile ("E:\lab1 data.txt", ios::in);
   assert (!infile.fail());  //stop program if file doesn't open
                
   while(infile>>lastname)
   {  infile>>firstname;
	  infile>>age;
      List.person[i]=new Listnode;
      strcpy(List.person[i]->lastname,lastname);
	  strcpy(List.person[i]->firstname,firstname);
      List.person[i]->age=age;
      i++;
   }
   List.size=i;
}
void printList(Listtype List)
{  system("cls");
   int i;
   for(i=0; i<List.size; i++)
     cout<<i+1<<" "<<List.person[i]->lastname<<" "<<List.person[i]->firstname<<" "<<List.person[i]->age<<endl;
   cout<<"\nList size = "<<List.size<<endl;
   system("PAUSE");
   return;
}
void doInsert(Listtype &List)
{  system("cls");
   Listnode* nd;
   char lastname[30], firstname[30];
   int pos, age;
   cout<<"Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE\n";
   cin>>lastname>>firstname>>age;
   cout<<"Insert this record at what position: ";
   cin>>pos;
   strcpy(nd->lastname, lastname);
   strcpy(nd->firstname, firstname);
   nd->age=age;
   List.size+=1;
   pos-=1;
   for(int i=List.size-1; i>pos; i--)   // might be this part??
     List.person[i]=List.person[i-1];
   List.person[pos]=nd;                 // or this??
}
void doDelete(Listtype &List)
{  system("cls");
   char lastname[30], firstname[30];
   int pos;
   bool sameL=0, sameF;
   cout<<"Enter the name of the record to delete: LASTNAME <SPACE> FIRSTNAME\n";
   cin>>lastname>>firstname;
   for(int i=0; i<List.size; i++)
   {  if(strcmp(lastname, List.person[i]->lastname)==0)  //if equal
	  {  sameL=1; 
         pos=i; } }
   for(int j=0; j<List.size; j++)
   {  if(strcmp(firstname, List.person[j]->firstname)==0)  //if equal
	    sameF=1; }
   if(sameL==0||sameF==0)
   {  cout<<"No such person found in data base.\n";
      cin.ignore();
      cin.ignore(); }
   else
   {  for(int j=pos; j<List.size-1; j++)
        List.person[j]=List.person[j+1];
   List.person[List.size]=NULL;
   List.size-=1; }
}
void deallocate(Listtype &List)
{  for(int i=0; i<List.size; i++)
     delete List.person[i];              
}

Edited 6 Years Ago by bigwhiteegg: n/a

On Line 100 you declare nd as a pointer to a Listnode, which is fine. Then you do nothing else with it until Line 107 where you try to dereference it to store information to the object it points to, which is definitely NOT fine.

The problem is you haven't actually allocated a Listnode object, you've only created a pointer to one. This is called a "dangling" pointer and, as you can see, they are very dangerous. You need to use a "new" statement to actually create a Listnode object. I suggest you read this.

Edited 6 Years Ago by Fbody: n/a

wow
I do not even know that stuff
and after adding the new into it
the code actually works!!
dear fbody thx alot for the help

This question has already been answered. Start a new discussion instead.