Software & Finance





Visual C++ Console Application - Product Master File Creation (Menu Oriented)





I have given here the source code in Visual C++ product master file creation with menu oriented in text mode.


Source Code


#include <conio.h>

#include <process.h>

#include <dos.h>

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <windows.h>

 

using namespace std;

 

void gotoxy(int x, int y)

{

    COORD ord;

    ord.X = x;

    ord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);

}

 

 

#pragma pack(2)

struct CProduct

{

public:

    char name[128];

    char category[128];

    char manufacturer[128];

    float price;

};

 

struct CProducts

{

    #define MaxSz  500

    char m_fileName[1024];

    int m_nMaxProducts;

    CProduct m_prodList[MaxSz];

 

    CProducts(const char *filename);

 

    void AddRecord(const char *name, const char *category, const char *manufacturer, float price);

 

    void EditRecord(int pos, const char *name, const char *category, const char *manufacturer, float price);

    void DeleteRecord(int pos);

    int ReadRecords();

    int WriteRecords();

};

 

CProducts::CProducts(const char *filename)

{

      m_nMaxProducts = 0;

      strcpy(m_fileName, filename);

}

 

void CProducts::AddRecord(const char *name, const char *category, const char *manufacturer, float price)

{

      int pos = m_nMaxProducts;

      strcpy(m_prodList[pos].name,name);

      strcpy(m_prodList[pos].category,category);

    strcpy(m_prodList[pos].manufacturer,manufacturer);

      m_prodList[pos].price = price;

      m_nMaxProducts++;

      WriteRecords();

}

 

void CProducts::EditRecord(int pos, const char *name, const char *category, const char *manufacturer, float price)

{

      strcpy(m_prodList[pos].name,name);

      strcpy(m_prodList[pos].category,category);

    strcpy(m_prodList[pos].manufacturer,manufacturer);

      m_prodList[pos].price = price;

      WriteRecords();

}

 

void CProducts::DeleteRecord(int pos)

{

      m_nMaxProducts--;

 

      for(int i = pos; i < m_nMaxProducts; i++)

      {

          m_prodList[i] = m_prodList[i + 1];

      }

      WriteRecords();

}

 

int CProducts::ReadRecords()

{

        FILE *istream = fopen(m_fileName, "rb");

    

        if (istream == 0)

          return 0;

 

      char buf[4096];

        int nTotalRecordsRead = 0;

 

        for(int i = 0; i < MaxSz; i++)

        {

            if(feof(istream))

                break;

            int nBytesRead = fread(buf, 1, sizeof(CProduct), istream);

            if(nBytesRead < sizeof(CProduct))

                break;

          char *p = (char*)(&m_prodList[i]);

            memcpy(p, buf, sizeof(CProduct));

            nTotalRecordsRead++;

        }

 

      fclose(istream);

      m_nMaxProducts = nTotalRecordsRead;

 

      return nTotalRecordsRead;

}

 

int CProducts::WriteRecords()

{

      FILE *ostream = fopen(m_fileName, "wb");

 

        if (ostream == 0)

          return 0;

 

        int nTotalRecordsWritten = 0;

        char buf[4096];

        for(int i = 0; i < m_nMaxProducts; i++)

        {

            fwrite((char*)&m_prodList[i], 1, sizeof(CProduct), ostream);

            nTotalRecordsWritten++;

        }

 

        fclose(ostream);

 

        return nTotalRecordsWritten;

}

 

 

CProducts theProducts("c:\\products.bin");

 

 

int DisplayMainMenu()

{

    ::system("cls");

   

    gotoxy(10,4);

    cout << "Welcome to Product Database Application";

 

    gotoxy(10,5);

    cout << "___________________________________________";

 

    gotoxy(10,6);

    cout << "1. Add Product";

 

    gotoxy(10,7);

    cout << "2. Edit Product";

 

    gotoxy(10,8);

    cout << "3. View Product";

 

    gotoxy(10,9);

    cout << "4. Delete Product";

 

    gotoxy(10,10);

    cout << "5. Exit";

 

    gotoxy(10,11);

    cout << "___________________________________________";

 

    gotoxy(10,13);

    cout << "Enter your Selection: ";

    int m = -1;

    cin >> m;

 

    return m;

}

 

void ViewRecords()

{

    theProducts.ReadRecords();

   

    ::system("cls");

       

    gotoxy(10,4);

    cout << "Welcome to Product Database Application";

 

    gotoxy(10,5);

    cout << "___________________________________________________________________";

 

    gotoxy(10,6);

    cout << "SNo Product Name       Category          Manufacturer        Price ";

 

    gotoxy(10,7);

    cout << "___________________________________________________________________";

 

    int pos = 8;

    // Enable Paging

    for(int i = 0; i < theProducts.m_nMaxProducts; i++)

    {

        gotoxy(10,pos);

      cout << i + 1;

        gotoxy(14,pos);

      cout << theProducts.m_prodList[i].name;

        gotoxy(33,pos);

      cout << theProducts.m_prodList[i].category;

        gotoxy(51,pos);

      cout << theProducts.m_prodList[i].manufacturer;

        gotoxy(72,pos);

    cout << theProducts.m_prodList[i].price;

        pos++;

    }

    gotoxy(10,pos++);

    cout << "___________________________________________________________________";

    pos++;

    gotoxy(10,pos++);

}

 

 

void InputRecords()

{

    while(1)

    {

        ::system("cls");

       

        gotoxy(10,4);

      cout << "Welcome to Product Database Application";

 

        gotoxy(10,5);

      cout << "___________________________________________";

 

        gotoxy(10,6);

      cout << "Product Name: ";

 

        gotoxy(10,7);

      cout << "Category Name: ";

 

        gotoxy(10,8);

      cout << "Manufacturer Name: ";

 

    gotoxy(10,9);

      cout << "Product Price: ";

 

        gotoxy(10,10);

      cout << "___________________________________________";

 

        gotoxy(24,6);

        char name[128];

      cin >> name;

 

        gotoxy(26,7);

        char category[128];

      cin >> category;

 

        gotoxy(29,8);

        char manufacturer[128];

      cin >> manufacturer;

 

    gotoxy(25,9);

        float price;

      cin >> price;

 

        theProducts.AddRecord(name, category, manufacturer, price);

 

        gotoxy(10,12);

      cout << "Do you want to add another record (Y/N)? ";

        char ch = getch();

 

        if(ch == 'Y' || ch == 'y')

            continue;

        else

            break;

    }

}

 

 

void EditRecords()

{

    ViewRecords();

    cout << "Enter the serial number you want to edit: ";

    int m;

    cin >> m;

 

    if(m >= 1 && m <= theProducts.m_nMaxProducts)

    {

        ::system("cls");

        gotoxy(10,4);

      cout << "Welcome to Product Database Application";

 

        gotoxy(10,5);

      cout << "___________________________________________";

 

        gotoxy(10,6);

      cout << "Product Name: ";

 

    gotoxy(10,7);

      cout << "Category Name: ";

 

        gotoxy(10,8);

      cout << "Manufacturer Name: ";

 

    gotoxy(10,9);

      cout << "Product Price: ";

 

        gotoxy(10,10);

      cout << "___________________________________________";

 

    gotoxy(24,6);

        char name[128];

      cin >> name;

 

        gotoxy(26,7);

        char category[128];

      cin >> category;

 

        gotoxy(29,8);

        char manufacturer[128];

      cin >> manufacturer;

 

    gotoxy(25,9);

        float price;

      cin >> price;

 

   

 

        theProducts.EditRecord(m - 1, name, category, manufacturer, price);

        gotoxy(10,13);

      cout << "Record updated. Press any key to return to Main Menu";

      getch();

    }

    else

    {

      gotoxy(10,12);

      cout << "Invalid Entry. Press any key to return to Main Menu";

      getch();

    }

 

}

 

void DeleteRecords()

{

    ViewRecords();

    cout << "Enter the serial number you want to delete: ";

    int m;

    cin >> m;

    if(m >= 1 && m <= theProducts.m_nMaxProducts)

    {

        theProducts.DeleteRecord(m - 1);

      cout << "Record deleted. Press any key to return to Main Menu";

      getch();

    }

    else

    {

      cout << "Invalid Entry. Press any key to return to Main Menu";

      getch();

    }

}

 

int main()

{

    theProducts.ReadRecords();

    while(1)

    {

        int selection = DisplayMainMenu();

 

        switch(selection)

        {

        case 1:

            InputRecords();

            break;

      case 2:

            EditRecords();

            break;

        case 3:

            {

            ViewRecords();

          cout << "Press any key to return to Main Manu: ";

          getch();

          }

            break;

        case 4:

          DeleteRecords();

            break;

 

        case 5:

        default:

            return 0;

        };

    }

}

Output