When I run my switch, choose option 3, my program crashes. Would anyone know how to solve this? I want option 3 to delete node from my linked list . I do not know how to do classes. Please don't suggest a fix using classes.
---------------------------------------------------------------------------------------------
my text file looks similar to this

1234 jack hammer 20.00 .45 10
4321 nails 5.00 .20 10
6543 daffy drill 30.00 .50 5
9875 jig blade 317.00 .60 8
---------------------------------------------------------------------------------------------

#include<iostream>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;

//******************************************************************************

struct acmetype{
       int pid;
       char description[25];
       float wholesale;
       float markup;
       int quantity;
       acmetype *link;
       };
       
// *******************************
// *   Function Prototypes       *
// *******************************
ifstream fin; //global variable

void headerfn();
void inputfn(acmetype *&head);
int addfn(acmetype *&head);
int delfn(acmetype *&head);
void printfn(acmetype *head);


//*******************
//* Begin of  Main  *
//*******************

 int main(){
      system("color F0");
      fin.open("tools.txt");
      if(!fin)return 1;
      headerfn();
  
      acmetype *head;
      inputfn(head);
      printfn(head);
   
   cout<<endl<<endl;
   cout <<" Press 1 to add node to start and end of  list" <<endl;        
   cout <<" Press 2 to add to the middle of the list" <<endl;
   cout <<" Press 3 to delete a product." <<endl<<endl;
   cout <<" Please enter in the number from the list above:   ";
   int choice;
   cin>>choice;

  switch (choice)
  {
    case 1:
     return addfn(head); 
     
      break;
         
    case 2: 
         //return midfn();  
         break;
    case 3: 
          return delfn(head); 
         break;
          }
cout<<endl;
cout<<endl<<endl;
system("PAUSE");

}//end of main

//**************************************************************************** 
int delfn(acmetype *&head)
{
     
     acmetype *pTemp, *nnptr, *currptr; //pTemp temporary node pointer


   if( nnptr->link == NULL )
     return 0;
   else
   {
     pTemp = nnptr->link->link;
     delete nnptr->link;
     nnptr->link = pTemp;
     if( nnptr->link == NULL )
       currptr = nnptr;
   }
     
     
     }//end of del fn
//****************************************************************************

Recommended Answers

All 7 Replies

Please use code tags to make your code easier to read.

Your function prototype .. int delfn(acmetype *&head) .. doesn't make any sense.

You probably need something like .. I am surprised your code compiles :?: int delfn(acmetype *head) In your statement "if( nnptr->link == NULL )" .. nnptr does not point to anything valid .. so it will crash right there.

Which node are you supposed to delete ? Is it the first node in the list ?
if so your code will be something like

int delfn(acmetype *head){
    acmetype *pTemp;

    if (head == NULL){
     return 0;
   }
   else{
     pTemp = head;
     head = head->link;
     delete pTemp;
   }

 return 0;
}

Your function prototype .. int delfn(acmetype *&head) .. doesn't make any sense.

That makes perfect sense to me -- it's a reference to a pointer. In C language it would be the same as int delfn(acmetype** head);

hmm .. but isn't *(&head) basically trying to de-reference the address of head ?

So it shouldn't be ** .. I don't think I am getting it

hmm .. but isn't *(&head) basically trying to de-reference the address of head ?

If you are talking about line 74 -- no. int*& head is a reference to a pointer -- much like a reference to any other object. The equivalent in C is the double star. But in C++ only a single star is used and the & refernece operator and makes the dereferencing easier

// C code
void foo(int ** head)
{
    if( *head == NULL)
}

// c++
void foo(int *& head)
{
   if( head == NULL)
}

ok.. I think I get it now. Is this a correct understanding ?

void foo(int*& d){

 cout << "d = "<< *d << endl;

}

int main(){
  int   a  = 5;
  int&  d  = a;
  int*  b  = &d;

  foo(b);

  return 0;
}

not quite

void foo(int*& d){

 cout << "d = "<< *d << endl;

}

int main(){
  int   a  = 5;
  int*  d  = &a;

  foo(d);

  return 0;
}

> pTemp = nnptr->link->link;
This will break if you only have 1 node in the list.

> acmetype *head;
> inputfn(head);
Does input initialise the list?
Or is the end of the list whatever junk your uninitialised variable was pointing to.

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.