[prog] C++ - linked list and segmentation fault
Jennifer Davis
davi0302 at algonquinc.on.ca
Tue Apr 8 13:10:56 EST 2003
I was wondering if I could get some advice on a program I am trying to
complete for class. It is a killer as I am new to linked lists. I write
the program and it compiles. However, when it runs, it segfaults. I'm
sure it happens when I am creating the list. All help would be very much
appreciated.
Jennifer S. Davis
Computer Programming, 1st year
Algonquin College
davi0302 at algonquinc.on.ca
#include<iostream.h> // for cin & cout
#include<stdio.h> // for I don't know what
#include<stdlib.h> // for I don't know what
#include<conio.h> // for getch()
#include<fstream.h> // for file i/o
struct INVENTORY_ITEM{
INVENTORY_ITEM *nextItem;
char itemCode[7];
int quantity;
float price;
}; // end struct INVENTORY_ITEM
void createFile (INVENTORY_ITEM *pStart);
void createOutput(INVENTORY_ITEM *pStart);
void deAllocate (INVENTORY_ITEM *pStart);
int main(){
INVENTORY_ITEM* pStart;
createFile (pStart);
createOutput (pStart);
deAllocate (pStart);
}//end main
void createFile(INVENTORY_ITEM *pStart){
ifstream in;
in.open("infile.txt");
INVENTORY_ITEM temp;
INVENTORY_ITEM* p;
INVENTORY_ITEM* current;
in >> temp.itemCode;
while (!in.eof()){
in >> temp.quantity;
in >> temp.price;
current=new INVENTORY_ITEM;
*current=temp;
current->nextItem=NULL;
p=pStart;
while ((p!=NULL)&&(strcmp(current->itemCode,p->itemCode)<0))
p=p->nextItem;
p->nextItem=current;
in >> temp.itemCode;
} // end while
in.close();
}// end createFile
void createOutput(INVENTORY_ITEM *pStart){
ofstream out;
out.open("outfile.txt");
INVENTORY_ITEM* current;
current=pStart;
while(current!=NULL){
out << current->itemCode << "\t"
<< current->quantity << "\t"
<< current->price << "\n";
current=current->nextItem;
} // end while
out.close();
} // end createOutput
void deAllocate(INVENTORY_ITEM *pStart){
INVENTORY_ITEM* current;
INVENTORY_ITEM* temp;
current=pStart;
while (current !=NULL){
temp->nextItem=current;
delete current;
current=temp;
} // end while
} // end deAllocate
More information about the Programming
mailing list