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;
   }
}

I havent written the add part code yet. Right now I'm just trying to get listing it to work, and the teacher gave us the code for everything but adding it.

I don't see anywhere where the program reads from the file. item.number is never initialized so it is printing whatever was in that memory location, which could be anything, but has nothing to do with the contents of your file, since the file is opened, but never read from.

Ya, I thought there was a problem with the file not beind read from. How do I read from the file?
Thanks!

Ya, I thought there was a problem with the file not beind read from. How do I read from the file?
Thanks!

Here's a decent tutorial.

http://www.cplusplus.com/doc/tutorial/files.html

Here's a sample that may help you.

#include <iostream>
#include <fstream>
using namespace std;

void ReadFile (ifstream& ins);

int main ()
{
    ifstream ins;
    ins.open ("link.dat");
    ReadFile (ins);
    ins.close ();
    cin.get ();
    return 0;
}


void ReadFile (ifstream& ins)
{
    int a;
    double b;
    while (ins >> a)
    {
          ins >> b;
          cout << a << '\t' << b << endl;
    }
}

So, I got the file to read but now I'm having a new problem. Its reading it, but when I have it output all of the contents the prices are off and its leaving out one of the items and posting the last item twice. I'm also having problems figuring out how to add an item and print in descending order. I got removing an item to work fine.

Here's my code now:

main:
#include <iostream>
#include <fstream>
#include <iomanip>
#include "Inventory.cpp"
//#include "ordrlink.h"

using namespace std;

int main()
{
    int i, j, count;
    Inventory link;
    Part part[20], temp;
    ifstream partin;
    char file;
    bool exit = false;
    int remove;
    int choice;
    int addPart;
    int addPrice;
    int size = 5;
    
    
    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();
                     }
                     
                     cout.setf(ios::fixed);
                     cout.precision(2);
                     
                     i = -1;
                     
                     partin >> temp.number; 
                     //part[i].number = temp.number;
                     //cout << "0 " << part[0].number << endl;
                     while(!partin.eof())
                     {
                        i++;
                        part[i].number = temp.number;
                        link.link(part[i]);
                        for(j = 0; j < 11; j++)
                        {
                           //i++;
                           partin >> part[i].price;
                           partin >> temp.number;
                           part[i].number = temp.number;
                           part[i].price;
                           //cout << j << " " << part[j].number << endl;
                           link.link(part[i]);
                        }
                        partin >> part[i].price;
                        //cout << i << " " << part[i].number << endl;
                        partin >> temp.number;
                        //link.link(part[i]);
                        
                        //part[i].number = temp.number;
                        link.link(part[i]);
                     }
                     break;
                case 2:
                     cout << "Enter the part number you want to add:" << endl;
                     cin >> addPart;
                     cout << "Enter the price for " << addPart << ":" << endl;
                     cin >> addPrice;
                     //size++;
                     break;
                case 3:
                     cout << "Enter the item number you want to remove:" << endl;
                     cin >> remove;
                     link.remove(remove);
                     //size--;
                     break;
                case 4:
                     link.printParts();
                     break;
                case 5:
                     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*/

Inventory.cpp:
#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);
        void add(int, int);
        void printDescending(void);
};

Inventory::Inventory(void)
{
      start = NULL;
}

void
Inventory::add(int partNum, int price)
{
     Part *p, *here, *last, *before;
     p = new Part;
     
     //p->number = partNum;
     //p->price = price;
     
     if(start == NULL)
     {
       start = p;
       start->next = NULL;
       before = NULL;
     }
     
     else
     {
        here = start;
        if(partNum  < here->number)
        {
           p->number = partNum;
           p->price = price;
           p->next = start;
           p->before = NULL;
           /*p->next = here;  
           p->before = last; 
           start = p; 
           last = p->next;*/     
        }
        else
        {
            while(p->number > here->number && here->next != NULL)
            {
                last = here;
                here = here->next;
                p->before = last;
                last = p->next;
            }
            if(p->number < here->number)
          {
             last->next = p;
             p->next = here;
             p->before = last;
          }
          else
          {
              here->next = p;
              p->next = NULL;
          }
        }
     }                                                    
}

void
Inventory::link(Part item)
{
   Part *p, *last, *here;
   p = new Part;
   
   p->number = 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;
   }
}

void Inventory::printDescending(void)
{
   Part *travel;
   travel = start;
   cout.setf(ios::fixed);
   cout.precision(2);
   
   if(travel != NULL)
   {
     cout << "\n # " << setw(13) << "Price" << endl;          
   }    
   
   while(travel != null)
   {
    for(int i = 0; i < 7; i++)
    {
        for(int j = 0; j < 7; j++)
        {
                  
        }
    }            
   }
}

Here's a ex. that may help you..

// add this method void Inventory::Push(Part **p_part,int _num,float _price) { Part *p=new Part; if (p==NULL) return; p->number=_num; p->price=_price; p->next=*p_part; *p_part=p; }

void Inventory::printDescending(void) { Part *travel,*dummy; dummy=start; while(dummy!=NULL) { int n=dummy->number; float p=dummy->price; Push(&travel,n,p); dummy=dummy->next; }

while(travel!=NULL) { std::cout<<travel->number<<'\t'<<travel->price<<std::endl; travel=travel->next; } }[code=c++]
// add this method
void Inventory::Push(Part **p_part,int _num,float _price)
{
Part *p=new Part;
if (p==NULL)
return;
p->number=_num;
p->price=_price;
p->next=*p_part;
*p_part=p;
}

void Inventory::printDescending(void)
{
Part *travel,*dummy;
dummy=start;
while(dummy!=NULL)
{
int n=dummy->number;
float p=dummy->price;
Push(&travel,n,p);
dummy=dummy->next;
}

while(travel!=NULL)
{
std::cout<<travel->number<<'\t'<<travel->price<<std::endl;
travel=travel->next;
}
}

Here's a ex. that may help you..

// add this method
void Inventory::Push(Part **p_part,int _num,float _price)
{
   Part *p=new Part;
   if (p==NULL)
      return;
   p->number=_num;
   p->price=_price;
   p->next=*p_part;
   *p_part=p;
}

void Inventory::printDescending(void)
{
   Part *travel,*dummy;
   dummy=start;
   while(dummy!=NULL)
   {
      int n=dummy->number;
      float p=dummy->price;
      Push(&travel,n,p);
      dummy=dummy->next;
   }

   while(travel!=NULL)
   {
      std::cout<<travel->number<<'\t'<<travel->price<<std::endl; 
      travel=travel->next;
   }
}
This article has been dead for over six months. Start a new discussion instead.