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.
Thanks