// program implement the working of files simple stock
// management

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <stdio.h>

class stock{
    int code;
    char name[30];
    float cost;
    char flag;
public:
    void getStock();
    void putStock();
    char getFlag();
    void putFlag(char);
    int getCode();
};

int stock :: getCode(){
    return code;
}

char stock :: getFlag(){
    return flag;
}
void stock::putFlag(char a){
    flag=a;
}
void stock::getStock(){
    cout << "\nEnter Stock Code: "; cin >> code;
    cout << "\nEnter Stock Name: "; cin >> name;
    cout << "\nEnter Stock Cost: "; cin >> cost;
}

void stock :: putStock(){
    cout << endl << setw(10) << code << setw(10)
         << name << setw(10) << cost << setw(10) 
         << getFlag();
}


void main(){
    stock stk,stk1;
    int choice,loc,count,temp,cnt;
    int SIZE = sizeof(stk);
    int tcount =0,find;
    char filename[] ="d:\\stock2.dat";  
    int sel;

    fstream iofile;
    ofstream tempfile;

    iofile.open(filename, ios::in | ios::out);
    iofile.seekg(0, ios::beg);

    cout << "\nThe Current Stock's are :\n";
    cout << setw(10) << "Code" << setw(10) << "Name" 
         << setw(10) << "Cost" << setw(10) << "Status";


    while(iofile.read((char *) &stk, sizeof(stk))){
        stk.putStock();
        tcount ++;
        }
    iofile.close();

    while(1){
        cout << "\nEnter \n1.Add\n2.Display\n3.Display(*)\n4.Modify With Sl No\n5.Modify with Item Number\n\
6.Mark For Delete\n7.Delete Permenantly\n8.Unmark\n9.Exit\n";

        cin >> choice;

    switch(choice){
        case 1:
            int rec;
            cnt=0;

            // code for adding new record
            iofile.open(filename, ios::in | ios::app);
            iofile.seekg(0);
            iofile.clear();
            stk1.getStock();
            stk1.getCode();
            stk1.putFlag('&');
            loc=0;
            count=0;
            while(iofile.read((char *) &stk, sizeof(stk))){
                if( stk.getCode()==stk1.getCode()){
                cout<<"\nRecord already exist....";
                cnt++;
                }
                }
                iofile.close();
                if(cnt>=1){
                    iofile.close();
                    break;
                }
                else{
                    iofile.open(filename,ios::app|ios::in);
                    iofile.write((char *) &stk1,sizeof(stk1));
                    tcount++;
                    iofile.close();
                    break;
                    }       
        case 3:
                //displaying with (*)
                cout << "\nThe Mark Stock's are :\n";
                cout << setw(10) << "Code" << setw(10) << "Name" 
                     << setw(10) << "Cost" << setw(10)<<"status";
                cout<<endl;

                iofile.open(filename, ios::in|ios::out);
                while(iofile.read((char *) &stk, sizeof(stk)))
                    if(stk.getFlag()=='*')
                    stk.putStock();
                    iofile.close();
                break;
        case 4:
            //modify with serial number...
                iofile.open(filename, ios::in | ios::out);
                int recno;
                loc = 0;
                cout << "\nEnter the record Number to be modified: ";
                cin >> recno;
                if(recno > tcount){
                cout << "\nYou have only " << tcount 
                    << " Number of records\nPlease Enter with in range";
                break;
                }
                loc = (recno -1) * SIZE;
                iofile.seekp(loc);
                cout << "\nEnter new Values for Stock";
                stk.getStock();

                iofile.write((char *) &stk, sizeof(stk));
                iofile.close();
                break;
        case 2://display all item...

                    cout <<"\n The current stocks are:\n";
                    cout<<endl<<setw(10)<<"Code"<<setw(10)<<"Name"<<setw(10)<<"Cost"<<setw(10)<<"Status"<<endl;
                    iofile.open(filename,ios::in|ios::out);
                    while(iofile.read((char *)&stk,sizeof(stk)))
                    stk.putStock();
                    iofile.close();
                    break;
        case 5:
                // Modify item details with Item Number 
                cout << "\nEnter the code ";
                cin >> temp;
                iofile.open(filename, ios::in | ios::out);

                iofile.seekg(0);
                loc = 0;
                count=0;

                while(iofile.read((char *) & stk, SIZE)){
                    if(temp != stk.getCode())
                        count++;
                    else{
                        loc = (count) * SIZE;
                        iofile.seekp(loc);
                        cout << "\nEnter new Values for Stock";
                        stk.getStock();

                        iofile.write((char *) &stk, sizeof(stk));

                        iofile.close();
                        break;
                        }
                    }
                iofile.close();
                cout<< "\n Item Not Found";
                break;
        case 6:
                // code for marking a record for delete
                int recno1;
                count =0,cnt=0,loc=0;
                cout<<"\n1.To mark particular file: press 1\n2.To mark all records: press 2\n";
                cin>>sel;
                if(sel==1){
                cout << "\nEnter the record number of Deletion(*): ";
                cin >> recno1;

                iofile.open(filename, ios::in | ios::out);
                iofile.seekg(0);
                while(iofile.read((char *) & stk, SIZE)){
                    if(recno1 == stk.getCode()){
                        loc=(count)*SIZE;
                        iofile.seekp(loc);
                        stk.putFlag('*');
                        iofile.write((char *)&stk,SIZE);
                        cout << "\nRecord found with id " << recno1;
                        iofile.close();
                        cnt++;
                        break;
                        } 
                        count++;
                        }

                    if(count>=1)
                        break;
                    else{
                        cout << "\nRecord Not found for Deletion";
                        iofile.close();
                        break;
                        }
                    }
                else if(sel==2){
                        iofile.open(filename, ios::in | ios::out);
                        iofile.seekg(0);
                        while(iofile.read((char *) & stk, SIZE)){
                            loc=(count)*SIZE;
                            iofile.seekp(loc);
                            stk.putFlag('*');
                            iofile.write((char *)&stk,SIZE);
                            count++;
                        }
                        iofile.close();
                        break;
                        }           
        case 7:     
                    iofile.open(filename, ios::in | ios::out);
                    iofile.seekg(0);

                    tempfile.open("D:\\delet.dat",ios::out);

                    while(iofile.read((char *)&stk,SIZE)){
                        if(stk.getFlag()!='*'){
                            tempfile.write((char *)&stk,SIZE);
                        }
                    }
                    iofile.close();
                    tempfile.close();
                    remove(filename);
                    rename("D:\\delet.dat",filename);
                    break;
        case 8: 
                    int recno2;
                    count=0;
                    cnt=0;
                    loc=0;  
                    cout<<"\na). To unmark particular file: press 1\nb). To unmark all marked files: press 2";
                    cin>>sel;
                    if(sel==1){
                        iofile.open(filename, ios::in | ios::out);
                        iofile.seekg(0);
                        cout<<"\nEnter the record number to unmark: ";
                        cin>>recno2;
                        while(iofile.read((char *) & stk, SIZE)){
                        if(recno2==stk.getCode()){
                        loc=(count)*SIZE;
                        iofile.seekp(loc);
                        stk.putFlag('&');
                        iofile.write((char *)&stk,SIZE);
                        cout << "\nRecord found with id " << recno1;
                        iofile.close();
                        cnt++;
                        break;
                        } 
                        count++;
                        }

                    if(count>=1)
                        break;
                    else{
                        cout << "\nRecord Not found for Deletion";
                        iofile.close();
                        break;
                        }   
                    }
                    else if(sel==2){
                        iofile.open(filename, ios::in | ios::out);
                        iofile.seekg(0);
                        while(iofile.read((char *) & stk, SIZE)){
                        loc=(count)*SIZE;
                        iofile.seekp(loc);
                        stk.putFlag('&');
                        iofile.write((char *)&stk,SIZE);
                        count++;
                        }
                        iofile.close();
                        break;
                        } 
                    else{
                        cout << "\nInvlaid selection...";
                        break;
                        }   
            case 9:
                    exit(0);
            default:
                cout <<"\nPlease Enter valid option ";
                }

        }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

If you don't use code tags then nobody will look at your program. Also you need to tell us what problem(s) you are having with it, what compiler and operating system you are using. Afterall, we are not mind readers.

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.