0

Below is my code in C++ about updating and deleting particular Product from a text file. It really works but after functioning, it create the things in the command box picture. Parallel to that, text file appears to create space according to product view in command box picture. The two functions are mostly the same.

void delete_Product(struct Product p)
{
    fstream exa;
    fstream temp;
    char code[25];
    exa.open("example.txt", ios::in);
    temp.open("temp.txt", ios::out);
    cin.ignore();
    cout<<"\nEnter Product Code : ";
    cin.getline(code,25);
    while (!exa.eof())
    {
        exa.getline(p.P_code,25,'\n');
        exa.getline(p.P_name,25,'\n');
        exa.getline(p.meg,25,'\n');
        exa.getline(p.expiry,25,'\n');
        exa.getline(p.producer,25);
        search_Product(p,code);
        if (strcmp(p.P_code,code)==0)
        {
            continue;
        } else
        {
            temp<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
        }
    }
    temp.close();
    exa.close();
    exa.open("example.txt", ios::out);
    temp.open("temp.txt", ios::in);
    while (!temp.eof())
    {
        temp.getline(p.P_code,25,'\n');
        temp.getline(p.P_name,25,'\n');
        temp.getline(p.meg,25,'\n');
        temp.getline(p.expiry,25,'\n');
        temp.getline(p.producer,25);
        exa<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
    }
    temp.close();
    exa.close();
    remove("temp.txt");
    cout<<"Done!";
}
void update_Product(struct Product p)
{
    fstream exa;
    fstream temp;
    char code[25];
    exa.open("example.txt", ios::in);
    temp.open("temp.txt", ios::out);
    cin.ignore();
    cout<<"\nEnter Product Code : ";
    cin.getline(code,25);
    while (!exa.eof())
    {
        exa.getline(p.P_code,25,'\n');
        exa.getline(p.P_name,25,'\n');
        exa.getline(p.meg,25,'\n');
        exa.getline(p.expiry,25,'\n');
        exa.getline(p.producer,25);
        search_Product(p,code);
        if (strcmp(p.P_code,code)==0)
        {
            cout<<"\n\nPlease update the selected Product";
            cout<<"\nEnter Product's ID: ";
            cin>>p.P_code;
            cin.ignore();
            cout<<"Enter Product's Name: ";
            cin.getline(p.P_name, 25);
            cout<<"Enter Manufacturing date: ";
            cin>>p.meg;
            cout<<"Enter Expiry date: ";
            cin>>p.expiry;
            cin.ignore();
            cout<<"Enter Producer's Name: ";
            cin.getline(p.producer, 25);
            temp<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
        }
        else
        {
            temp<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
        }
    }
    temp.close();
    exa.close();
    exa.open("example.txt", ios::out);
    temp.open("temp.txt", ios::in);
    while (!temp.eof())
    {
        temp.getline(p.P_code,25,'\n');
        temp.getline(p.P_name,25,'\n');
        temp.getline(p.meg,25,'\n');
        temp.getline(p.expiry,25,'\n');
        temp.getline(p.producer,25);
        exa<<p.P_code<<'\n'<<p.P_name<<'\n'<<p.meg<<'\n'<<p.expiry<<'\n'<<p.producer<<'\n';
    }
    temp.close();
    exa.close();
    remove("temp.txt");
    cout<<"Done!";
}

My full code is on Google Drive. Below are screenshots of the output:

Image 1: Command box Image 2: Command box Image 3: Command box Image 4: Text file

Community
  • 1
  • 1
Trí Lê
  • 1
  • 1
  • 1
    Read about [why `while (!stream.eof())` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Jan 08 '20 at 13:09
  • Sorry, I cannot parse your question. Your use of `while (!exa.eof())` is wrong, though; you have to actually check the input operations. – Lightness Races in Orbit Jan 08 '20 at 13:09
  • So what can I use instead of while(!exa.eof()) – Trí Lê Jan 08 '20 at 13:13
  • 1) Write functions that read and write entire `Product`s; 2) Separate user interaction from data manipulation. – molbdnilo Jan 08 '20 at 13:32
  • IMHO, you should overload `operator>>` in your `Product` class to read from a stream. This strengthens the concepts of *encapsulation*, *data hiding* and *loose coupling* (research these on the web). – Thomas Matthews Jan 08 '20 at 15:33

0 Answers0