Problem with Double Linked Lists

2012-03-19  Source: Original Site  Category:C++  Views:0 

Advertisement

I have an assignment where I need to create an inventory program where I can add information about a product to a text file, and shown in the file like shown:

1;Hammer;20;13.50

Where 1 is the record number, Hammer is the description, 20 is the quantity, and 13.50 is the cost

In the program, there need to be a menu where I can choose to do the following:
- Add a new record (to the end of the text file)
- Delete a record (by record #)
- Modify a record (change anything except the record #)
- List all records
- Sort Records by COST
- Quit and SAVE to a text file

The problem I'm having (so far), is that once I exit out of the program, and restart it again, I can not modify by record #. On top of that, I can only display the list once, after that the text file is not able to be opened. Haven't even started on the Sorting by COST, but I'm not worried about that. Here's by attempt:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct tool
{//open struct tool

tool *prev;
int record;
char toolname[35];
int quantity;
double cost;
tool *next;

};//close struct tool

class tool_dll
{//open class

private:
int max;
int qty;
tool *head;
tool *curr_rec;
bool addRecord2(int, char[35], int, double);
tool *templist;

public:

tool_dll()
{//open constructor
max = 100;
qty =0;
head = curr_rec = templist = NULL;
};//close constructor

bool addRecord(int, char[35],int, double);
bool modifyRecord(int);
bool displayNext(int);
bool deleteRecord(int);
bool isFull();
bool isEmpty();
bool sortByCost();
bool QuitandSave();

~tool_dll()
{//open destructor
QuitandSave();
};//close destructor

};//close class

///////////////////////////////////////////////////////////////////////////
/////////////////////////////MEMBER FUNCTIONS//////////////////////////////
///////////////////////////////////////////////////////////////////////////

bool tool_dll:: addRecord(int r, char t[35],int q, double c)
{
if(isFull())
return false;

if((head == NULL) || (head -> record > r)) //first tool
{
qty++;
tool *temp = new tool;
temp -> record = r;
strcpy(temp ->toolname, t);
temp -> quantity = q;
temp -> cost = c;

temp -> prev = NULL;
temp -> next = head;
head = temp;

if(temp -> next != NULL)
temp ->next -> prev = temp;
curr_rec = temp;
return true;
}

for(tool *scan = head; scan != NULL; scan = scan ->next)
{

if(scan->record == r)
return false;

if(scan->next == NULL)
{
qty++;
tool *temp = new tool;
temp -> record = r;
strcpy(temp ->toolname, t);
temp -> quantity = q;
temp -> cost = c;

temp -> next = scan -> next;
if(temp -> next != NULL)
temp ->next -> prev = temp;

temp -> prev = scan;
scan -> next = temp;
return true;
}

if(scan->next->record > r)
{
qty++;
tool *temp = new tool;
temp -> record = r;
strcpy(temp ->toolname, t);
temp -> quantity = q;
temp -> cost = c;

temp -> next = scan -> next;
if(temp -> next != NULL)
temp ->next -> prev = temp;

temp -> prev = scan;
scan -> next = temp;
return true;

}

}

return false;
};

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

bool tool_dll:: addRecord2(int r, char t[35],int q, double c)
{
if(isFull())
return false;

if((templist == NULL) || (templist -> cost > c)) //first tool
{
qty++;
tool *temp = new tool;
temp -> record = r;
strcpy(temp ->toolname, t);
temp -> quantity = q;
temp -> cost = c;

temp -> prev = NULL;
temp -> next = templist;
templist = temp;

if(temp -> next != NULL)
temp ->next -> prev = temp;
curr_rec = temp;
return true;
}

for(tool *scan = templist; scan != NULL; scan = scan ->next)
{
if(scan -> cost == c)
return false;

if((scan ->next == NULL) || (scan -> next -> cost > c))
{
qty++;
tool *temp = new tool;
temp -> cost = c;
strcpy(temp ->toolname, t);
temp -> quantity = q;
temp -> record = r;

temp -> next = scan -> next;
if(temp -> next != NULL)
temp ->next -> prev = temp;

temp -> prev = scan;
scan -> next = temp;
return true;
}

}

return false;

};

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

bool tool_dll:: modifyRecord(int r)
{

displayNext(r);
cout << endl;

tool *scan = head;

while(true)
{
// if(scan == NULL)
// return false;

if(scan->record == r)
break;

scan = scan->next;
}

int choice=0;
char name[35];
int amount;
double price;
int ref;
int i;

do
{

cout << "What Would You Like to Edit for this Tool?" << endl;
cout << "1) Tool Name" << endl << "2) Quantity" << endl;
cout << "3) Cost" << endl << "4) Exit" << endl;
cout << "What is your choice(1,2,3,4): ";
cin >> choice;

switch(choice)
{
case 1:
cout << "What Is The New Tool Name: ";
cin>>name;

ref = scan->record;
amount = scan ->quantity;
price = scan->cost;
deleteRecord(scan->record);
addRecord(ref,name,amount,price);
break;

case 2:
cout << "What Is The New Quantiy: ";
cin >> amount;

ref = scan->record;
price = scan->cost;
for(i = 0; i < 35; i++)
{

name[i] = scan->toolname[i];

}
deleteRecord(scan->record);
addRecord(ref,name,amount,price);
break;

case 3:

cout << "What Is The New Cost: ";
cin >> price;

ref = scan->record;
amount = scan ->quantity;
for(i = 0; i < 35; i++)
{

name[i] = scan->toolname[i];

}
deleteRecord(scan->record);
addRecord(ref,name,amount,price);
break;

default:
break;
}

}while(choice != 4);

return false;
};

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

bool tool_dll:: displayNext(int r)
{

if(curr_rec != NULL)
{
cout << endl;
cout << curr_rec -> record << ";" << curr_rec->toolname << ";";
cout << curr_rec -> quantity << ";" << curr_rec -> cost;
curr_rec = curr_rec->next;
return true;
}
cout << endl;
return false;
};

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

bool tool_dll:: deleteRecord(int r)
{
if(isEmpty())
return false;

tool *scan= head;

if(head->record == r)//first tool
{
qty--;
head = scan->next;
if(scan->next != NULL)
{
scan->next->prev = NULL;
}
delete scan;
return true;
}

for(scan = scan->next; scan != NULL; scan = scan->next)
{
if(scan->record == r)
{
if(scan->next == NULL)//last node
{
scan->prev->next= NULL;
}
else
{
scan->prev->next = scan->next;
scan->next->prev = scan->prev;
}

delete scan;
qty--;
return true;
}
}

return false;
};

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

bool tool_dll:: isFull()
{
if(qty == max)
return true;
return false;
};

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

bool tool_dll:: isEmpty()
{
if(qty == 0)
return true;
return false;
};

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

bool tool_dll:: sortByCost()
{
templist = NULL;

while(head != NULL)
{
addRecord2(head->record,head->toolname,head->quantity,head->cost);
deleteRecord(head->record);
}

head = templist;
curr_rec = head;
return true;

};

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

bool tool_dll:: QuitandSave()
{
ofstream outFile;

outFile.open("inventory.txt", ios::app);

if (!outFile)
{
cout << "Can't open file inventory.txt" << endl;
exit(1);
return false;
}

while(head != NULL)
{
outFile << head -> record << ";" << head->toolname << ";"
<< head -> quantity << ";" << head -> cost << endl;

deleteRecord(head->record);
}

return true;
outFile.close();

};

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

int main()
{
tool_dll database;

int choice=1;
int rec_num, qty;
double cost;
char desc[35];
ifstream fin;

do
{
cout <<"Inventory Control Program\n\nPlease Select an Option From the Menu:\n\n";
cout <<"1: Add New Record\n2: Delete Record by Record Number\n3: Modify A Record";
cout <<"\n4. List All Records\n5. Sort All Records by Cost\n6: Save and Quit program\n\nChoice: ";
cin >>choice;

switch (choice)
{
case 1:
cout <<"\nEnter A Record Number: ";
cin >> rec_num;
cout <<"\nEnter A Product Name: ";
cin >>desc;
cout <<"\nEnter the Quantity of "<<desc<<" Needed: ";
cin >>qty;
cout <<"\nEnter the Cost of "<<desc<<" per ONE piece: ";
cin >>cost;

database.addRecord(rec_num,desc,qty,cost);
cout <<"\n\nRecord Added.";
// database.QuitandSave();
break;
case 2:
cout <<"\nEnter Record Number to Be Deleted: ";
cin>>rec_num;
database.deleteRecord(rec_num);
cout <<"\n\nRecord Deleted.";
break;
case 3:
cout <<"\nEnter Record Number to Be Modified: ";
cin >>rec_num;
database.modifyRecord(rec_num);
cout <<"\n\nRecord Modified.";
break;
case 4:
char c;
fin.open("inventory.txt", ios::in);
if(fin.fail())
{
cout << "Error: Unable To Open File\n";
exit(1);
}

fin.get(c);

while(!fin.fail() && !fin.eof())
{
cout << c;
fin.get(c);
}

fin.close();

case 5:
// database.addRecord2(rec_num,desc,qty,cost);
break;
case 6:
database.QuitandSave();
break;
default: "Invalid Choice";
}

}while (choice!=6);

return 0;
}

ANY help at all would be appreciated, especially with being able to access and modify the records after I exit the program and run it again.

Related articles
  • Problem with Double Linked Lists 2012-03-19

    I have an assignment where I need to create an inventory program where I can add information about a product to a text file, and shown in the file like shown: 1;Hammer;20;13.50 Where 1 is the record number, Hammer is the description, 20 is the quanti

  • Problems with double linked list??? 2012-08-20

    guys/gals for some reason in my main function my first while loop will not take in more than two values. any reasons why that wont happen? and just to be on the safe side, did I implement my remove function properly? #ifndef SORTEDLIST_H#define SORTE

  • Beginner with C++ and goofy run time problem with DBL Linked List 2012-10-18

    Greetings To all! To whomever, an advanced thank you if you help me. I am a Java programmer initially, but I have begun learning C++ and am doing something with a Double Linked List (Error occurs whether it's double or single, I teste it). Filling th

  • Issues with double linked list 2013-09-21

    All I am having issues with my code and it has something to do with the way my double linked list is working. I cant figure out if its not setting the m_prev pointer if at all and what is going on with my m_next pointer. Any help would be greatly app

  • help with double link list 2013-02-15

    I have a double link list program that compiles ok, and when I run it, I get a message box saying that windows needs to stop the program. Here is the code: #include <iostream>#include <conio.h> using namespace std; class linklist{private:

  • trouble with double linked list 2015-06-15

    Hello, I am having trouble creating a double linked list within an array of pointers. Right now actually, I am just trying to make a linked list. Line 267 is where I get a complier error. I understand the error (cannot convert `std::string' to `wordL

  • Structure with double linked list 2014-01-13

    I need to make a program which adds a person to a structure (it's done), displays all added people (also done) but I would also like it to display a first person (it's done) and then in the same option let me choose whether to see a previous person o

  • problem with singly linked list 2012-01-23

    Ok i'm trying to build a header file which then i can include it for programs that use singly linked lists but somthing is fishy here...: here's the code #include <iostream.h> struct nod{int info;nod* next_adr;}; void add(nod* &v, nod*

  • Problem with a Linked list 2013-01-21

    been struggling with implementing a linked list. I'm using this function AddToList to add elements. I'm clearly doing something wrong because no new element is added. Although when Im using a breakpoint inside the function i see that it seems right.

  • Major Problem with Doubly linked list program 2014-07-16

    For the most parts I believe my program is correct, when compiled it doesn't show any errors. The problem that I do have is that I cannot the information on my linked list to print. When the print out comes out it's suppose to show a list of names in

  • Problem with doubly linked lists 2014-12-03

    So this is my first time on here asking for help but I do visit this forum every now and again when I am taking a programming class. I am trying to get my doubly linked list to work correctly. Also I am having issues with parsing an input file. The f

  • Ubuntu Corrupted double-linked list with nano after upgrading to breezy 2012-05-14

    I ran into a slight problem shortly after upgrading to breezy. Everything else seems to work just fine and dandy, but when I attempt to do a specific search and replace operation with nano to my httpd.conf, i get either of the following error message

  • Double linked list problem 2012-10-10

    I have this program that suppost to ask user to select option: [option] a. Start a new list b. Add a Node to the end of the list. c. Add a Node to the beginning of the list. d. Add a Node at a specified position in the list. e. Walk the linked list,

  • HELP ME DOUBLE LINKED LIST: something wrong with the code by terminal 2014-05-27

    Please help me with the double linked list, when I write the command in terminal to test main class by my source code the cmd.exe throw me:Exception in thread "main" java.lang.NullPointerException at homework.List.add(Homework2.java:19) at homework.H

  • Double linked list search problem =( 2014-09-25

    I'm using double linked list to store dynamic data. As my program executes, the linked list gets larger and searching for a particular item in the list takes a long time...as it goes sequentially from Head to Tail :( Does anyone has any idea to speed

  • Double Linked List...Delete function() 2012-02-05

    everyone, I am having a problem with a delete function I'm trying to create for a double linked list class. Here's the code I was able to compile with no errors, but for some reason that I can't see....I haven't been able to get the code to do what I

  • traversing backwards thru double linked list 2013-05-18

    Hello, I am having some trouble traversing backwards through a double linked list. Basically this program pulls words from a text file (I have attached the file I am using), corrects the words and stores them in an array of pointers (one pointer per

  • Double linked list - possible memory error 2013-08-05

    Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would try my own small program. The problem is, I seem to be

  • Help!!! single to double link lists!! 2014-02-19

    I have this assignemt to make a double link list for a student database. I have tried to make it in single link list so that i can understand the implementation. the problem is that i can't make it a double link list i am so confused! plz someone hel

  • Ubuntu Straight Insertion Sort and Double-Linked List? 2012-03-17

    I have to sort Double-Linked List via Straight Insertion Sort. The Problem is I seem to do it somehow wrong. I got Segmentation Fault after the third time cycle starts, using numbers: 3 5 4 2 7 6 8 9 1 Anyone could help. I'm more looking for hint of