Hello, I'm having trouble with this program. What I have to do is open a file(in this case link.dat) and then the user chooses what they want to do, take something out of it, add somethng to it, print it. It opens the file fine, but then I cant get it to print correctly. It prints a large number instead of the numbers it needs to print. I think where I am having a problem is I dont know how to make the file be sent to the class that changes or prints it.
heres the code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include "Inventory.cpp"
using namespace std;
int main()
{
Inventory link;
Part part;
ifstream partin;
char filename[25];
bool exit = false;
int remove;
int choice;
int addPart;
int addPrice;
do
{
cout << "Menu: " << endl;
cout << "1) Create an ascending order list from a file" << endl;
cout << "2) Add an item" << endl;
cout << "3) Delete an item" << endl;
cout << "4) Print the list in ascending order" << endl;
cout << "5) Print list in descending order" << endl;
cout << "6) End program" << endl;
cin >> choice;
switch(choice)
{
case 1:
partin.open("link.dat");
if(partin.fail())
{
cout << "Error: can't find the file. Please try again!" << endl;
partin.clear();
}
else
link.link(part);
break;
case 2:
cout << "Enter the part number you want to add:" << endl;
cin >> addPart;
cout << "Enter the price for " << addPart << ":" << endl;
cin >> addPrice;
break;
case 3:
cout << "Enter the item number you want to remove:" << endl;
cin >> remove;
link.remove(remove);
break;
case 4:
link.printParts();
break;
case 6:
exit = true;
break;
}
}
while(exit == false);
system("pause");
return EXIT_SUCCESS;
}
/*link.dat
123 19.95
46 7.63
271 29.99
17 .85
65 2.45
32 49.50
128 8.25*/
and the Inventory class:
#include <iomanip>
#include <iostream>
using namespace std;
struct Part
{
int number;
float price;
Part *next, *before;
};
class Inventory
{
protected:
Part *start;
public:
Inventory(void);
void link(Part);
void remove(int);
void printParts(void);
};
Inventory::Inventory(void)
{
start = NULL;
}
void
Inventory::link(Part item)
{
Part *p, *last, *here;
p = new Part;
p->number = item.number;
cout << item.number;
p->price = item.price;
if(start == NULL)
{
start = p;
start->next = NULL;
}
else
{
here = start;
if(p->number < here->number)
{
p->next = here;
p->before - last;
start = p;
}
else
{
while(p->number > here->number && here->next != NULL)
{
last = here;
here = here->next;
}
if(p->number < here->number)
{
last->next = p;
p->next = here;
p->before = last;
}
else
{
here->next = p;
p->next = NULL;
}
}
}
}
void
Inventory::remove(int partNum)
{
Part *here, *follow;
here = start;
while(here->number != partNum && here ->next != NULL)
{
follow = here;
here = here->next;
}
if(here->number == partNum)
{
if(here == start)
start = here->next;
else
{
follow->next = here->next;
follow->before = here->before;
}
delete here;
}
}
void
Inventory::printParts(void)
{
Part *travel;
travel = start;
cout.setf(ios::fixed);
cout.precision(2);
if(travel != NULL)
{
cout << "\nPart # " << setw(13) << "Price" << endl;
}
while(travel != NULL)
{
cout << setw(5) << travel->number;
cout << setw(8) << '$' << setw(6) << travel->price << endl;
travel = travel->next;
}
}