You are here:

C++/file handling

Advertisement


Question
QUESTION: Hi there sir.

I just want to ask some help with regarding file handling in c++. I am using bloodshed dev and been stocked with my final project. The problem is a simple inventory system that can append and delete stocks with the text file  it is stored. Anyway, I just wanted to have a good samples on appending and deleting text from files and I think I could finish it. I'm not quite sure if this will be read by you or the others so I'll still not post my partial codes here. Kind of shy, you know. :D hope to hear from you soon.

ANSWER: Hello Steve

Don't be shy to show me your code. In fact, I always give more effort to answering questions when I see the effort that the student has made. If you are concerned, make your question private.

I recommend that you use an ofstream for writing and an ifstream for reading from the file, instead of trying to use one stream for both. You can open the ofstream with ios::app to cause appending to the file. However of you are needing to delete elements from the middle of the file, then I recommend that you read all the elements into memory when the program starts. Do you operations on the elements in memory, then write them to the file all at once as the program finishes. I can give you a quick sample of how to use the fstream classes but I'll can give you more help only if I know more details about what you are doing.

Here is a sample that appends stuff into a file, then reads it back.

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

using namespace std;

int main(void)
{
   // open file for appending, and create if it doesn't exist
   ofstream outs("stocks.txt", ios::app);
   if ( ! outs.good())
   {
       cout << "Cannot open or create file\n";
       return 0;
   }

   ifstream ins("stocks.txt");
   if ( ! ins.good())
   {
       cout << "Cannot open input file\n";
       return 0;
   }

   for(int line=0; line < 10; ++line)
   {
       outs << "Line " << line << endl;
   }

   string input;
   while(getline(ins, input))
   {
       cout << "Read text: " << input << endl;
   }

   return 0;
}

---------- FOLLOW-UP ----------

QUESTION: ...
:D I really appreciate your quick response sir. Actually, I already made to append and display the contents from the text file in my program. I just find it difficult now on how to delete things from it. Okay here is my partial codes...


#include<iostream.h>
#include<string.h>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
#define max 20
using namespace std;


void proManagement();

struct product
{
      long int stockNo;
      char product[20];
      char supply[20];
      long int quantity;
      int dateArrived;
};
int num;
product pro[max], temppro[max], sortpro[max], sortpro1[max];

struct supplier
{
      char supplier2[20];
      long int ctrlNo;
      char address[20];
      long int noProducts;
};
supplier sup[max], tempsup[max], sortsup[max], sortsup1[max];

struct inven
{
      int in;
      int out;
};
inven temp[max], temp2[max], sort[max], sort2[max];

main ()
{
    system("cls");
    
    void supManagement();
    void inventory();
    void listsup();
    void addsup();
    void delsup();
    void menu();
    void promenu();
    void supmenu();
    void exit();
    
    char option;
    
    menu();
    
    while ((option = cin.get())!='x')
    {
         switch(option)
         {
         case 'a':
         promenu();
         break;
         case 'b':
         supmenu();
         break;
         case 'c':
         inventory();
         break;
         
         default:
         getch();
         }
    }
    menu();
}

void exit()
{
    getch();
}

void menu()
{
    system("cls");
    
    cout << "\t   *********************************************************\n";
    cout << "\t   *********************************************************\n";
    cout << "\t   **-----------------------------------------------------**\n";
    cout << "\t   **--OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO--**\n";
    cout << "\t   **--OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO--**\n";
    cout << "\t   **-----------------------------OOO---------------------**\n";
    cout << "\t   **--O-O---O-O---O-OOOOO-O---O--OOO---OOO--OOOO--O---O--**\n";
    cout << "\t   **--O-OO--O-O---O-O-----OO--O--OOO--O---O-O---O-O---O--**\n";
    cout << "\t   **--O-O-O-O-O---O-OOOO--O-O-O--OOO--O---O-O---O--O-O---**\n";
    cout << "\t   **--O-O--OO-O---O-O-----O--OO--OOO--O---O-OOOO----O----**\n";
    cout << "\t   **--O-O---O--O-O--O-----O---O--OOO--O---O-O---O---O----**\n";
    cout << "\t   **--O-O---O---O---OOOOO-O---O--OOO---OOO--O---O---O----**\n";
    cout << "\t   **-----------------------------OOO---------------------**\n";
    cout << "\t   **-----------OOO--O---O--OOO---OOO--OOOOO-O---O--------**\n";
    cout << "\t   **----------O-----O---O-O------OOO--O-----OO-OO--------**\n";
    cout << "\t   **-----------OOO---O-O---OOO---OOO--OOOO--O-O-O--------**\n";
    cout << "\t   **--------------O---O-------O--OOO--O-----O-O-O--------**\n";
    cout << "\t   **----------O---O---o---O---O--OOO--O-----O---O--------**\n";
    cout << "\t   **-----------OOO----o----OOO---OOO--OOOOO-O---O--------**\n";
    cout << "\t   **-----------------------------------------------------**\n";
    cout << "\t   *********************************************************\n";
    cout << "\t   *********************************************************\n\n\n";
    
    cout << "SELECT COMMAND:\n";
    cout << "\ta. PRODUCT MANAGEMENT\n";
    cout << "\tb. SUPPLIER MANAGEMENT\n";
    cout << "\tc. INVENTORY\n\n";
    cout << "\tx. EXIT PROGRAM\n\n\n";
    
    cout << "ENTER SELECTION [a/b/c/d]: ";
}

void promenu()
{
    system("cls");
    
    void addpro();
    void delpro();
    void listpro();
    char optionpro;
    
    proManagement();
    
    while ((optionpro = cin.get())!='x')
    {
         switch(optionpro)
         {
         case 'a':
         addpro();
         break;
         case 'b':
         delpro();
         break;
         case 'c':
         listpro();
         break;
         case 'd':
         main();
         break;
         default:
         getch();
         }
    }
}

void proManagement()
{
    system("cls");
    
    cout << "\t   *************************************************************\n";
    cout << "\t   **-OOO-OO---O--OO--O-O-OOO-OOO-----------------------------**\n";
    cout << "\t   **-O-O-O-O-O-O-O-O-O-O-O----O------------------------------**\n";
    cout << "\t   **-OOO-OO--O-O-O-O-O-O-O----O------------------------------**\n";
    cout << "\t   **-O---O-O--O--OO--OOO-OOO--O------------------------------**\n";
    cout << "\t   **-----------O---O--O--O--O--O--OOO-OOO-O---O-OOO-O--O-OOO-**\n";
    cout << "\t   **-----------OO-OO-O-O-OO-O-O-O-O---OO--OO-OO-OO--OO-O--O--**\n";
    cout << "\t   **-----------O-O-O-OOO-O-OO-OOO-O-O-O---O-O-O-O---O-OO--O--**\n";
    cout << "\t   **-----------O---O-O-O-O--O-O-O-OOO-OOO-O---O-OOO-O--O--O--**\n";
    cout << "\t   *************************************************************\n\n";
   
    cout << "SELECT COMMAND:\n";
    cout << "\ta. ADD PRODUCT\n";
    cout << "\tb. DELETE PRODUCT\n";
    cout << "\tc. LIST OF PRODUCTS\n";
    cout << "\td. BACK FROM PREVIOUS MENU...\n\n";
    cout << "\tx. EXIT PROGRAM\n\n\n";
    
    cout << "ENTER SELECTION [a/b/c/d/x]: ";   
}


void listpro()
{
    system("cls");

    string line;
   ifstream x ("products.txt");

   if (x.is_open())
   {
   while (getline(x, line))
   {

   cout<<line << endl;

   }
   x.close();
   }
   else
   cout<<"can't open file."<<endl;
   
    system("pause");
    promenu();
}

void addpro()
{
    system("cls");
    
    cout << "**************************************************\n";
    cout << "*--O--OO--OO------OOO-OO---O--OO--O-O-OOO-OOO----*\n";
    cout << "*-O-O-O-O-O-O-----O-O-O-O-O-O-O-O-O-O-O----O-----*\n";
    cout << "*-OOO-O-O-O-O-----OOO-OO--O-O-O-O-O-O-O----O-----*\n";
    cout << "*-O-O-OO--OO------O---O-O--O--OO--OOO-OOO--O-----*\n";
    cout << "**************************************************\n\n\n";
    
    int i=num;
    num+=1;
    ofstream fout;
    fout.open("products.txt",ios::app);
         
    cout << "Enter the following items: \n";
    cout << "\t Stock No.\t\t: ";
    cin >> pro[i].stockNo;
    cout << "\t Product\t\t: ";
    cin >> pro[i].product;
    cout << "\t Quantity\t\t: ";
    cin >> pro[i].quantity;
    cout << "\t Supplier\t\t: ";
    cin >> pro[i].supply;
    cout << "\t Date Arrived (mmddyy)\t: ";
    cin >> pro[i].dateArrived;
    cout << endl<<endl;
    
    fout << endl <<setw(1) << "*"
    <<setw(8) <<pro[i].stockNo << setw(2) << "*"
    <<setw(20)<<pro[i].product << setw(2) << "*"
    <<setw(10)<<pro[i].quantity << setw(2) << "*"
    <<setw(20)<<pro[i].supply << setw(2) << "*"
    <<setw(13)<<pro[i].dateArrived << setw(2) << "*" << endl
    <<"**********************************************************************************" << endl;
    
    fout.close();
    ;
    promenu();     
}

void delpro()
 {
  system("cls");

  int check;
  cout << "***********************************************************\n";
  cout << "*-OO--OOO-O---OOO-OOO-OOO-----OOO-OO---O--OO--O-O-OOO-OOO-*\n";
  cout << "*-O-O-OO--O---OO---O--OO------O-O-O-O-O-O-O-O-O-O-O----O--*\n";
  cout << "*-O-O-O---O---O----O--O-------OOO-OO--O-O-O-O-O-O-O----O--*\n";
  cout << "*-OO--OOO-OOO-OOO--O--OOO-----O---O-O--O--OO--OOO-OOO--O--*\n";
  cout << "***********************************************************\n\n\n";
}

void supmenu()
{
    system("cls");
    
    void supManagement();
    void addsup();
    void delsup();
    void listsup();
    char optionsup;
    
    supManagement();
    
    while ((optionsup = cin.get())!='x')
    {
         switch(optionsup)
         {
         case 'a':
         addsup();
         break;
         case 'b':
         delsup();
         break;
         case 'c':
         listsup();
         break;
         
         default:
         getch();
         }
    }
    supManagement();
}

void supManagement()
{
    system("cls");
    
    cout << "\t   *************************************************************\n";
    cout << "\t   **-OOO-O-O-OOO-OOO-O---OOO-OOO-OO--------------------------**\n";
    cout << "\t   **-O---O-O-O-O-O-O-O----O--OO--O-O-------------------------**\n";
    cout << "\t   **--OO-O-O-OOO-OOO-O----O--O---OO--------------------------**\n";
    cout << "\t   **-OOO-OOO-O---O---OOO-OOO-OOO-O-O-------------------------**\n";
    cout << "\t   **-----------O---O--O--O--O--O--OOO-OOO-O---O-OOO-O--O-OOO-**\n";
    cout << "\t   **-----------OO-OO-O-O-OO-O-O-O-O---OO--OO-OO-OO--OO-O--O--**\n";
    cout << "\t   **-----------O-O-O-OOO-O-OO-OOO-O-O-O---O-O-O-O---O-OO--O--**\n";
    cout << "\t   **-----------O---O-O-O-O--O-O-O-OOO-OOO-O---O-OOO-O--O--O--**\n";
    cout << "\t   *************************************************************\n\n";
   
    cout << "SELECT COMMAND:\n";
    cout << "\ta. ADD SUPPLIER\n";
    cout << "\tb. DELETE SUPPLIER\n";
    cout << "\tc. LIST OF SUPPLIER\n\n";
    cout << "\tx. EXIT PROGRAM\n\n\n";
    
    cout << "ENTER SELECTION [a/b/c/d]: ";}
    


void listsup()
{
    system("cls");
    
   string line;
   ifstream x ("supplier.txt");

   if (x.is_open())
   {
   while(getline(x,line))
   {
   cout<<line<<endl;
   }
   x.close();
   }
   else
   cout<<"can't open file."<<endl;
    
    system("pause");
    supmenu();
}

void addsup()
{
    system("cls");
    
    cout << "****************************************************\n";
    cout << "*--O--OO--OO------OOO-O-O-OOO-OOO-O---OOO-OOO-OO---*\n";
    cout << "*-O-O-O-O-O-O-----O---O-O-O-O-O-O-O----O--OO--O-O--*\n";
    cout << "*-OOO-O-O-O-O------OO-O-O-OOO-OOO-O----O--O---OO---*\n";
    cout << "*-O-O-OO--OO------OOO-OOO-O---O---OOO-OOO-OOO-O-O--*\n";
    cout << "****************************************************\n\n\n";
    
    int i=num;
    num+=1;
    ofstream fout;
    fout.open("supplier.txt",ios::app);
         
    cout << "Enter the following items: \n";
    cout << "\t Ctrl No.\t: ";
    cin >> sup[i].ctrlNo;
    cout << "\t Supplier\t: ";
    cin >> sup[i].supplier2;
    cout << "\t Address\t: ";
    cin >> sup[i].address;
    cout << "\t No. of Products\t: ";
    cin >> sup[i].noProducts;
    cout << endl<<endl;
    
    fout <<   endl <<   setw(1) << "*"
    <<setw(8) <<sup[i].ctrlNo << setw(2) << "*"
    <<setw(20)<<sup[i].supplier2 << setw(2) << "*"
    <<setw(29)<<sup[i].address << setw(2) << "*"
    <<setw(16)<<sup[i].noProducts << setw(2) << "*"
    <<endl;
    fout.close();
    ;
    system("pause");
    supmenu();     
}

void delsup()
 {
  system("cls");
}     

void inventory()
{
    system("cls");
    
    void addinven();
    void delinven();
    void showinven();
    void invenmenu();
    char optioninven;
    
    invenmenu();
    
         while ((optioninven = cin.get())!='x')
    {
         switch(optioninven)
         {
         case 'a':
         addinven();
         break;
         case 'b':
         delinven();
         break;
         case 'c':
         showinven();
         break;
         case 'd':
         main();
         break;
         default:
         exit();
         }
    invenmenu();
    }
}

void invenmenu()
{
    system("cls");
    
    cout << "\t   *********************************************************\n";
    cout << "\t   **-----------------------------------------------------**\n";
    cout << "\t   **-----------------------------------------------------**\n";
    cout << "\t   **--O-O---O-O---O-OOOOO-O---O-OOOOO--OOO--OOOO--O---O--**\n";
    cout << "\t   **--O-OO--O-O---O-O-----OO--O---O---O---O-O---O-O---O--**\n";
    cout << "\t   **--O-O-O-O-O---O-OOOO--O-O-O---O---O---O-O---O--O-O---**\n";
    cout << "\t   **--O-O--OO-O---O-O-----O--OO---O---O---O-OOOO----O----**\n";
    cout << "\t   **--O-O---O--O-O--O-----O---O---O---O---O-O---O---O----**\n";
    cout << "\t   **--O-O---O---O---OOOOO-O---O---O----OOO--O---O---O----**\n";
    cout << "\t   **-----------------------------------------------------**\n";
    cout << "\t   *********************************************************\n\n";
    
    cout << "SELECT COMMAND:\n";
    cout << "\ta. ADD INVENTORY\n";
    cout << "\tb. DELETE INVENTORY\n";
    cout << "\tc. SHOW INVENTORY\n\n";
    cout << "\td. BACK TO PREVIOUS MENU...\n\n";
    cout << "\tx. EXIT PROGRAM\n\n\n";
    
    cout << "ENTER SELECTION [a/b/c/d]: ";
}
    
void addinven()
{
    system("cls");
    int stockcode;
    int tempquantity;
    
    cout << "******************************************************\n";
    cout << "*--O--OO--OO-------O-O-O-O-O-OOO-O-O-OOO--O--OO--O-O-*\n";
    cout << "*-O-O-O-O-O-O------O-OOO-O-O-OO--OOO--O--O-O-O-O-O-O-*\n";
    cout << "*-OOO-O-O-O-O------O-OOO-O-O-O---OOO--O--O-O-OO---O--*\n";
    cout << "*-O-O-OO--OO-------O-O-O--O--OOO-O-O--O---O--O-O--O--*\n";
    cout << "******************************************************\n\n\n";
    
    cout << "Enter Stock No. of the Product: ";
    cin >> stockcode;
    
    for(int i=0;i<=num-1;i++)
    {
        if(pro[i].stockNo==stockcode)
        {
        cout << "PRODUCT FOUND!\n\n";
        cout << pro[i].product << endl;
        cout << "Enter quantity to add: ";
        cin >> tempquantity;
         
        if (temp[i].in != '\0')
        {
        temp[i].in = temp[i].in + tempquantity;
        }
        else
        {
         temp[i].in = tempquantity;
        }
        pro[i].quantity = pro[i].quantity + tempquantity;
        }
        
        else
        {
        cout << "PRODUCT NOT FOUND!";
        inventory();
        }
    }
}

void delinven()
{
    system("cls");
}

void showinven()
{
    system("cls");
    
    cout << "******************************************************\n";
    cout << "*-O-O-O-O-O-OOO-O-O-OOO--O--OO--O-O----O---O-OOO-OOO-*\n";
    cout << "*-O-OOO-O-O-OO--OOO--O--O-O-O-O-O-O----O---O-O----O--*\n";
    cout << "*-O-OOO-O-O-O---OOO--O--O-O-OO---O-----O---O--OO--O--*\n";
    cout << "*-O-O-O--O--OOO-O-O--O---O--O-O--O-----OOO-O-OOO--O--*\n";
    cout << "******************************************************\n\n\n";
    
    cout << "*********************************************************************\n";
    cout << "* STOCK NO.  PRODUCT          QUANTITY       IN         OUT    *\n";
    cout << "*********************************************************************\n";
    cout << "*         *          *          *          *          *\n";
    
    for (int i=0; i <= num-1; i++)
    {
    cout<<setw(1) << "*";
    cout<<setw(8) <<pro[i].stockNo << setw(2) << "*";
    cout<<setw(20)<<pro[i].product << setw(2) << "*";
    cout<<setw(10)<<pro[i].quantity << setw(2) << "*";
    cout<<setw(10)<<temp[i].in << setw(2) << "*";
    cout<<setw(10)<<temp[i].out << setw(2) << "*";
    cout<<endl;
    }
    cout << "*         *          *          *          *          *\n";
    cout << "*********************************************************************\n";
    
    system("pause");
}

..I'll check out later your pseudocode sir and try to implement it. Many thanks btw. :D

ANSWER: Hello Steve.

Your program is certainly very slick. I have some ideas for you to consider. First, you should decide what it means to delete a product. It could mean to decrease the product's quantity by one in the file and delete the record from the file if the quantity is 0 but I'm guessing that it means to delete the record from the database regardless of the existing quantity.

You have a number of options when deleting from a file. Earlier I suggested you keep the entire inventory in memory, do your insertions and deletions in memory, and then write the inventory to disk just before the program exits. I see now that your design is not like that and I guess you would like to minimize changes to your existing design. The problem when deleting a record from a file is that it leaves a hole in the file. The records don't reorganize themselves automatically. You have to do it. There are a number of options.

Option 1
------------
This is the option you will probably like the best because it will minimize the amount of extra work you have to do. In your delete routine open a new temporary file. Read in all the records from your product file and write them out to the temporary file, except for the one record you want to delete. Once that is done, close both the input and temporary files. Delete the product file using the standard C unlink function, and rename the temporary file to the the product file name using the standard C rename function. For this to be done easily, I recommend that you don't store the blank lines and lines of stars in the products file. Instead of this in products.txt
<blank line>
*     111 *          p111 *         1 *          aaa *       111111 *
**********************************************************************************
<blank line>
*     222 *          p222 *         2 *          sss *       121212 *
**********************************************************************************

You should have this:
*     111 *          p111 *         1 *          aaa *       111111 *
*     222 *          p222 *         2 *          sss *       121212 *

You can add the lines of stars as you are displaying the records to the screen but you don't need them in the file.

Make sure to unlink the original file before attempting the rename.
For information on unlink, see
http://msdn.microsoft.com/en-us/library/1c3tczd6.aspx

For information on rename, see
See http://msdn.microsoft.com/en-us/library/zw5t957f%28v=vs.71%29.aspx

Don't forget to check the return value from the functions to see if they succeeded.

Other options
---------------
The other options are all tricky. I do not recommend them for beginners. They rely on being able to move around a file using file pointers.

Option 2
--------------
Scan through the file for the record to delete. When you find the record, overwrite the record with some special character that your program recognizes as indicating an empty record. When listing your products, simply ignore the records filled with your special character. Records with the special character can be reused the next time you want to add records to the file.

Option 3
-------------
Scan through the file for the record to delete. Lets call the record D. When you find D, copy the last record of the file into the location where D is. Then reduce the file size by the size of one record.

Option 4
------------
Option 4 is to choose option 1. You already know how to do it.

Good luck.


---------- FOLLOW-UP ----------

QUESTION: :c i really didn't know how to use it sir. but, i found this on the net and have tried it in the program. trunc function of file stream. i managed to delete the product i want in the program itself and actually wanted to just just truncate the text file with the new class array without the deleted file. here's the code...

void delpro()
{ int code;
int check;

cout<<"Enter Stock No.: ";
  cin>>code;
  int i;
  for(i=0;i<=num-1;i++)
  {
   if(pro[i].stockNo==code)
   {
     check=i;
   }
  }
  
  for(i=0;i<=num-1;i++)
  {
   if(i==check)
   {
   continue;
   }
   else
   {
   if(i>check)
   {
    temppro[i-1]=pro[i];
   }
   else
   {
    temppro[i]=pro[i];
   }
    }
  }
 num--;

 for(i=0;i<=num-1;i++)
 {
  pro[i]=temppro[i];
 }
ofstream f1("supplier.txt", ios::trunc);
  f1.close();

  ofstream fout;
  fout.open("supplier.txt",ios::app);

  fout << "**********************************************************************************" << endl
       << "*          PRODUCT LIST          *" << endl
       << "*          -------------          *" << endl
       << "**********************************************************************************" << endl
       << "* STOCK NO.  PRODUCT          QUANTITY   SUPPLIER          DATE ARRIVED *" << endl
       << "**********************************************************************************" << endl;
       
      for (int i=0; i <= num-1; i++)
    {
    fout<<setw(1) << "*"
    <<setw(8) <<pro[i].stockNo << setw(2) << "*"
    <<setw(20)<<pro[i].product << setw(2) << "*"
    <<setw(10)<<pro[i].quantity << setw(2) << "*"
    <<setw(20)<<pro[i].supply << setw(2) << "*"
    <<setw(13)<<pro[i].dateArrived << setw(2) << "*"
    <<endl;
    }
  fout.close();
}






why it doesn't work well? as i run it, after deleting 1 certain item, sometimes it will only store 1 product on the list and delete the rest. hmmm...

Answer
Hello Steve

I took a closer look at your program. Now I see that you are actually storing products and suppliers in memory, not just on disk. That is a good approach. Your idea for deletion is good, but it can be made shorter. I have shown you how. I am posting your entire program back with my comments. My comments start with ZM. Please read them because they also point out some logic errors and some more work for you to do.

Best regards
Zlatko

#include<iostream> //ZM removed .h
#include<string> //ZM removed .h
#include<iomanip>
#include<fstream>
#include<sstream>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
#define max 20
using namespace std;

/*ZM use defines to prevent accidental misspellings */
#define PRODUCTS_FILENAME "products.txt"
#define SUPPLIERS_FILENAME "supplier.txt"

void proManagement();

struct Product //ZM changed to upper case P
{
   long int stockNo;
   char product[20];
   char supply[20];
   long int quantity;
   int dateArrived;
};
/* ZM you must use separate counters for number of products and number of
suppliers. Otherwise adding a product will make it seem like you
have added a supplier too
*/
int numProd = 0; // ZM changed from num to numProd
Product pro[max], temppro[max], sortpro[max], sortpro1[max];

struct supplier
{
   char supplier2[20];
   long int ctrlNo;
   char address[20];
   long int noProducts;
};
supplier sup[max], tempsup[max], sortsup[max], sortsup1[max];
/* ZM you must use separate counters for number of products and number of
suppliers. Otherwise adding a product will make it seem like you
have added a supplier too
*/
int numSup = 0; // ZM changed from num to numSup

struct inven
{
   int in;
   int out;
};
inven temp[max], temp2[max], sort[max], sort2[max];

int main ()
{
   system("cls");

   /*ZM
   The first thing you need to do here is to read in the
   products and suppliers from files if the files exist
   When you do that, also make sure that you set the
   numProd and numSup counters. Otherwise the program
   is not consistent. If I start the program with
   the files present, I can list the products, but the
   products are not in memory, so I cannot delete them.
   The memory should be consistent with what is on the disk.
   */
   void supManagement();
   void inventory();
   void listsup();
   void addsup();
   void delsup();
   void menu();
   void promenu();
   void supmenu();
   void exit();

   char option;

   menu();

   while ((option = cin.get())!='x')
   {
       switch(option)
       {
       case 'a':
         promenu();
         break;
       case 'b':
         supmenu();
         break;
       case 'c':
         inventory();
         break;

       default:
         getch();
       }
   }
   menu();
}

void exit()
{
   getch();
}

void menu()
{
   system("cls");

   cout << "\t   *********************************************************\n";
   cout << "\t   *********************************************************\n";
   cout << "\t   **-----------------------------------------------------**\n";
   cout << "\t   **--OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO--**\n";
   cout << "\t   **--OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO--**\n";
   cout << "\t   **-----------------------------OOO---------------------**\n";
   cout << "\t   **--O-O---O-O---O-OOOOO-O---O--OOO---OOO--OOOO--O---O--**\n";
   cout << "\t   **--O-OO--O-O---O-O-----OO--O--OOO--O---O-O---O-O---O--**\n";
   cout << "\t   **--O-O-O-O-O---O-OOOO--O-O-O--OOO--O---O-O---O--O-O---**\n";
   cout << "\t   **--O-O--OO-O---O-O-----O--OO--OOO--O---O-OOOO----O----**\n";
   cout << "\t   **--O-O---O--O-O--O-----O---O--OOO--O---O-O---O---O----**\n";
   cout << "\t   **--O-O---O---O---OOOOO-O---O--OOO---OOO--O---O---O----**\n";
   cout << "\t   **-----------------------------OOO---------------------**\n";
   cout << "\t   **-----------OOO--O---O--OOO---OOO--OOOOO-O---O--------**\n";
   cout << "\t   **----------O-----O---O-O------OOO--O-----OO-OO--------**\n";
   cout << "\t   **-----------OOO---O-O---OOO---OOO--OOOO--O-O-O--------**\n";
   cout << "\t   **--------------O---O-------O--OOO--O-----O-O-O--------**\n";
   cout << "\t   **----------O---O---o---O---O--OOO--O-----O---O--------**\n";
   cout << "\t   **-----------OOO----o----OOO---OOO--OOOOO-O---O--------**\n";
   cout << "\t   **-----------------------------------------------------**\n";
   cout << "\t   *********************************************************\n";
   cout << "\t   *********************************************************\n\n\n";

   cout << "SELECT COMMAND:\n";
   cout << "\ta. PRODUCT MANAGEMENT\n";
   cout << "\tb. SUPPLIER MANAGEMENT\n";
   cout << "\tc. INVENTORY\n\n";
   cout << "\tx. EXIT PROGRAM\n\n\n";

   cout << "ENTER SELECTION [a/b/c/d]: ";
}

void promenu()
{
   system("cls");

   void addpro();
   void delpro();
   void listpro();
   char optionpro;

   proManagement();

   while ((optionpro = cin.get())!='x')
   {
       switch(optionpro)
       {
       case 'a':
         addpro();
         break;
       case 'b':
         delpro();
         break;
       case 'c':
         listpro();
         break;
       case 'd':
         main();
         break;
       default:
         getch();
       }
   }
}

void proManagement()
{
   system("cls");

   cout << "\t   *************************************************************\n";
   cout << "\t   **-OOO-OO---O--OO--O-O-OOO-OOO-----------------------------**\n";
   cout << "\t   **-O-O-O-O-O-O-O-O-O-O-O----O------------------------------**\n";
   cout << "\t   **-OOO-OO--O-O-O-O-O-O-O----O------------------------------**\n";
   cout << "\t   **-O---O-O--O--OO--OOO-OOO--O------------------------------**\n";
   cout << "\t   **-----------O---O--O--O--O--O--OOO-OOO-O---O-OOO-O--O-OOO-**\n";
   cout << "\t   **-----------OO-OO-O-O-OO-O-O-O-O---OO--OO-OO-OO--OO-O--O--**\n";
   cout << "\t   **-----------O-O-O-OOO-O-OO-OOO-O-O-O---O-O-O-O---O-OO--O--**\n";
   cout << "\t   **-----------O---O-O-O-O--O-O-O-OOO-OOO-O---O-OOO-O--O--O--**\n";
   cout << "\t   *************************************************************\n\n";

   cout << "SELECT COMMAND:\n";
   cout << "\ta. ADD PRODUCT\n";
   cout << "\tb. DELETE PRODUCT\n";
   cout << "\tc. LIST OF PRODUCTS\n";
   cout << "\td. BACK FROM PREVIOUS MENU...\n\n";
   cout << "\tx. EXIT PROGRAM\n\n\n";

   cout << "ENTER SELECTION [a/b/c/d/x]: ";   
}


void listpro()
{
   system("cls");

   string line;
   ifstream x (PRODUCTS_FILENAME);

   if (x.is_open())
   {
       while (getline(x, line))
       {

         cout<<line << endl;

       }
       x.close();
   }
   else
       cout<<"can't open file."<<endl;

   system("pause");
   promenu();
}

void addpro()
{
   system("cls");

   cout << "**************************************************\n";
   cout << "*--O--OO--OO------OOO-OO---O--OO--O-O-OOO-OOO----*\n";
   cout << "*-O-O-O-O-O-O-----O-O-O-O-O-O-O-O-O-O-O----O-----*\n";
   cout << "*-OOO-O-O-O-O-----OOO-OO--O-O-O-O-O-O-O----O-----*\n";
   cout << "*-O-O-OO--OO------O---O-O--O--OO--OOO-OOO--O-----*\n";
   cout << "**************************************************\n\n\n";

   int i=numProd;
   numProd+=1;
   ofstream fout;
   fout.open(PRODUCTS_FILENAME,ios::app);

   cout << "Enter the following items: \n";
   cout << "\t Stock No.\t\t: ";
   cin >> pro[i].stockNo;
   cout << "\t Product\t\t: ";
   cin >> pro[i].product;
   cout << "\t Quantity\t\t: ";
   cin >> pro[i].quantity;
   cout << "\t Supplier\t\t: ";
   cin >> pro[i].supply;
   cout << "\t Date Arrived (mmddyy)\t: ";
   cin >> pro[i].dateArrived;
   cout << endl<<endl;

   fout << endl <<setw(1) << "*"
       <<setw(8) <<pro[i].stockNo << setw(2) << "*"
       <<setw(20)<<pro[i].product << setw(2) << "*"
       <<setw(10)<<pro[i].quantity << setw(2) << "*"
       <<setw(20)<<pro[i].supply << setw(2) << "*"
       <<setw(13)<<pro[i].dateArrived << setw(2) << "*" << endl
       <<"**********************************************************************************" << endl;

   fout.close();
   ;
   promenu();     
}

void delpro()
{
   int code;
   int check = -1; /*ZM set check to -1 */

   cout<<"Enter Stock No.: ";
   cin>>code;
   int i;
   for(i=0;i<=numProd-1;i++)
   {
       if(pro[i].stockNo==code)
       {
         check=i;
         break; /* once check is set, you can quit this loop */
       }
   }

   /*ZM Important to check if the product was
   actually found. If not found, then quit */
   if (check == -1 )
   {
       cout << "That stock number was not found\n";
       return;
   }

   /*ZM
   if the product to delete was found, you really
   just need to start the loop from check.
   pro[i] = pro[i+1] for all i where
   i starts at check and goes up to and
   including num-2
   */
   for(i = check; i <= numProd-2; ++i)
   {
       /*ZM don't need to use tempro
       here. Do the work directly in pro
       */
       pro[i]=pro[i+1];
   }
   numProd--;

   /*ZM don't need this because the
   work was done in pro
   for(i=0;i<=num-1;i++)
   {
       pro[i]=temppro[i];
   }
   */

   ofstream f1(PRODUCTS_FILENAME, ios::trunc);
   f1.close();

   ofstream fout;
   fout.open(PRODUCTS_FILENAME,ios::app);

   fout << "**********************************************************************************" << endl
       << "*  PRODUCT LIST          *" << endl
       << "*          -------------          *" << endl
       << "**********************************************************************************" << endl
       << "* STOCK NO.  PRODUCT          QUANTITY   SUPPLIER          DATE ARRIVED *" << endl
       << "**********************************************************************************" << endl;

   for (int i=0; i <= numProd-1; i++)
   {
       fout<<setw(1) << "*"
         <<setw(8) <<pro[i].stockNo << setw(2) << "*"
         <<setw(20)<<pro[i].product << setw(2) << "*"
         <<setw(10)<<pro[i].quantity << setw(2) << "*"
         <<setw(20)<<pro[i].supply << setw(2) << "*"
         <<setw(13)<<pro[i].dateArrived << setw(2) << "*"
         <<endl;
   }
   fout.close();
}


void supmenu()
{
   system("cls");

   void supManagement();
   void addsup();
   void delsup();
   void listsup();
   char optionsup;

   supManagement();

   while ((optionsup = cin.get())!='x')
   {
       switch(optionsup)
       {
       case 'a':
         addsup();
         break;
       case 'b':
         delsup();
         break;
       case 'c':
         listsup();
         break;

       default:
         getch();
       }
   }
   supManagement();
}

void supManagement()
{
   system("cls");

   cout << "\t   *************************************************************\n";
   cout << "\t   **-OOO-O-O-OOO-OOO-O---OOO-OOO-OO--------------------------**\n";
   cout << "\t   **-O---O-O-O-O-O-O-O----O--OO--O-O-------------------------**\n";
   cout << "\t   **--OO-O-O-OOO-OOO-O----O--O---OO--------------------------**\n";
   cout << "\t   **-OOO-OOO-O---O---OOO-OOO-OOO-O-O-------------------------**\n";
   cout << "\t   **-----------O---O--O--O--O--O--OOO-OOO-O---O-OOO-O--O-OOO-**\n";
   cout << "\t   **-----------OO-OO-O-O-OO-O-O-O-O---OO--OO-OO-OO--OO-O--O--**\n";
   cout << "\t   **-----------O-O-O-OOO-O-OO-OOO-O-O-O---O-O-O-O---O-OO--O--**\n";
   cout << "\t   **-----------O---O-O-O-O--O-O-O-OOO-OOO-O---O-OOO-O--O--O--**\n";
   cout << "\t   *************************************************************\n\n";

   cout << "SELECT COMMAND:\n";
   cout << "\ta. ADD SUPPLIER\n";
   cout << "\tb. DELETE SUPPLIER\n";
   cout << "\tc. LIST OF SUPPLIER\n\n";
   cout << "\tx. EXIT PROGRAM\n\n\n";

   cout << "ENTER SELECTION [a/b/c/d]: ";}



void listsup()
{
   system("cls");

   string line;
   ifstream x (SUPPLIERS_FILENAME);

   if (x.is_open())
   {
       while(getline(x,line))
       {
         cout<<line<<endl;
       }
       x.close();
   }
   else
       cout<<"can't open file."<<endl;

   system("pause");
   supmenu();
}

void addsup()
{
   system("cls");

   cout << "****************************************************\n";
   cout << "*--O--OO--OO------OOO-O-O-OOO-OOO-O---OOO-OOO-OO---*\n";
   cout << "*-O-O-O-O-O-O-----O---O-O-O-O-O-O-O----O--OO--O-O--*\n";
   cout << "*-OOO-O-O-O-O------OO-O-O-OOO-OOO-O----O--O---OO---*\n";
   cout << "*-O-O-OO--OO------OOO-OOO-O---O---OOO-OOO-OOO-O-O--*\n";
   cout << "****************************************************\n\n\n";

   int i=numSup;
   numSup+=1;
   ofstream fout;
   fout.open(SUPPLIERS_FILENAME,ios::app);

   cout << "Enter the following items: \n";
   cout << "\t Ctrl No.\t: ";
   cin >> sup[i].ctrlNo;
   cout << "\t Supplier\t: ";
   cin >> sup[i].supplier2;
   cout << "\t Address\t: ";
   cin >> sup[i].address;
   cout << "\t No. of Products\t: ";
   cin >> sup[i].noProducts;
   cout << endl<<endl;

   fout <<   endl <<   setw(1) << "*"
       <<setw(8) <<sup[i].ctrlNo << setw(2) << "*"
       <<setw(20)<<sup[i].supplier2 << setw(2) << "*"
       <<setw(29)<<sup[i].address << setw(2) << "*"
       <<setw(16)<<sup[i].noProducts << setw(2) << "*"
       <<endl;
   fout.close();
   ;
   system("pause");
   supmenu();     
}

void delsup()
{
   system("cls");
}     

void inventory()
{
   system("cls");

   void addinven();
   void delinven();
   void showinven();
   void invenmenu();
   char optioninven;

   invenmenu();

   while ((optioninven = cin.get())!='x')
   {
       switch(optioninven)
       {
       case 'a':
         addinven();
         break;
       case 'b':
         delinven();
         break;
       case 'c':
         showinven();
         break;
       case 'd':
         main();
         break;
       default:
         exit();
       }
       invenmenu();
   }
}

void invenmenu()
{
   system("cls");

   cout << "\t   *********************************************************\n";
   cout << "\t   **-----------------------------------------------------**\n";
   cout << "\t   **-----------------------------------------------------**\n";
   cout << "\t   **--O-O---O-O---O-OOOOO-O---O-OOOOO--OOO--OOOO--O---O--**\n";
   cout << "\t   **--O-OO--O-O---O-O-----OO--O---O---O---O-O---O-O---O--**\n";
   cout << "\t   **--O-O-O-O-O---O-OOOO--O-O-O---O---O---O-O---O--O-O---**\n";
   cout << "\t   **--O-O--OO-O---O-O-----O--OO---O---O---O-OOOO----O----**\n";
   cout << "\t   **--O-O---O--O-O--O-----O---O---O---O---O-O---O---O----**\n";
   cout << "\t   **--O-O---O---O---OOOOO-O---O---O----OOO--O---O---O----**\n";
   cout << "\t   **-----------------------------------------------------**\n";
   cout << "\t   *********************************************************\n\n";

   cout << "SELECT COMMAND:\n";
   cout << "\ta. ADD INVENTORY\n";
   cout << "\tb. DELETE INVENTORY\n";
   cout << "\tc. SHOW INVENTORY\n\n";
   cout << "\td. BACK TO PREVIOUS MENU...\n\n";
   cout << "\tx. EXIT PROGRAM\n\n\n";

   cout << "ENTER SELECTION [a/b/c/d]: ";
}

void addinven()
{
   system("cls");
   int stockcode;
   int tempquantity;

   cout << "******************************************************\n";
   cout << "*--O--OO--OO-------O-O-O-O-O-OOO-O-O-OOO--O--OO--O-O-*\n";
   cout << "*-O-O-O-O-O-O------O-OOO-O-O-OO--OOO--O--O-O-O-O-O-O-*\n";
   cout << "*-OOO-O-O-O-O------O-OOO-O-O-O---OOO--O--O-O-OO---O--*\n";
   cout << "*-O-O-OO--OO-------O-O-O--O--OOO-O-O--O---O--O-O--O--*\n";
   cout << "******************************************************\n\n\n";

   cout << "Enter Stock No. of the Product: ";
   cin >> stockcode;

   for(int i=0;i<=numSup-1;i++)
   {
       if(pro[i].stockNo==stockcode)
       {
         cout << "PRODUCT FOUND!\n\n";
         cout << pro[i].product << endl;
         cout << "Enter quantity to add: ";
         cin >> tempquantity;

         if (temp[i].in != '\0')
         {
         temp[i].in = temp[i].in + tempquantity;
         }
         else
         {
         temp[i].in = tempquantity;
         }
         pro[i].quantity = pro[i].quantity + tempquantity;
       }

       else
       {
         cout << "PRODUCT NOT FOUND!";
         inventory();
       }
   }
}

void delinven()
{
   system("cls");
}

void showinven()
{
   system("cls");

   cout << "******************************************************\n";
   cout << "*-O-O-O-O-O-OOO-O-O-OOO--O--OO--O-O----O---O-OOO-OOO-*\n";
   cout << "*-O-OOO-O-O-OO--OOO--O--O-O-O-O-O-O----O---O-O----O--*\n";
   cout << "*-O-OOO-O-O-O---OOO--O--O-O-OO---O-----O---O--OO--O--*\n";
   cout << "*-O-O-O--O--OOO-O-O--O---O--O-O--O-----OOO-O-OOO--O--*\n";
   cout << "******************************************************\n\n\n";

   cout << "*********************************************************************\n";
   cout << "* STOCK NO.  PRODUCT          QUANTITY       IN         OUT    *\n";
   cout << "*********************************************************************\n";
   cout << "*         *          *          *          *          *\n";

   for (int i=0; i <= numProd-1; i++)
   {
       cout<<setw(1) << "*";
       cout<<setw(8) <<pro[i].stockNo << setw(2) << "*";
       cout<<setw(20)<<pro[i].product << setw(2) << "*";
       cout<<setw(10)<<pro[i].quantity << setw(2) << "*";
       cout<<setw(10)<<temp[i].in << setw(2) << "*";
       cout<<setw(10)<<temp[i].out << setw(2) << "*";
       cout<<endl;
   }
   cout << "*         *          *          *          *          *\n";
   cout << "*********************************************************************\n";

   system("pause");
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.