-2

I'm working on my project which is a cooking assistant which instructs the user do the tasks that are stored in a binary file. I cant save the inputs into the file and read it back again. I've included my code below. Suggest me a method to do this or a correction in my code. I want to get instructions which is stored in a two dimensional array... I have to get input until the instructions and ingredients for the specific recipe is over.I'm working on c++

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<process.h>

class recipe {
    int laststate, icount, qcount;
    char recno;
    char name[50];
    float quantity[][50];
    char ingredient[][50];
    char instructions[][50];
    char unit[5];

public:

    recipe() {
        //if(laststate>recno)
        //recno=laststate;
        //else
        recno = 0;
    }


    void add() {
        char ch;
        cout << "\nEnter Recipe Name :";
        cin >> name;
        do {
            qcount = 0;
            cout << "\nQuantity, Unit and Ingredient Name :";
            cin >> quantity[qcount] >> unit[qcount] >> ingredient[qcount];
            qcount++;
            cout << "\nDo You Want to Enter More Ingredients :";
            cin >> ch;
        } while (ch == 'y' || ch == 'Y');
        do {
            icount = 0;
            cout << "\nEnter Instructions:\n";
            cin >> instructions[icount];
            icount++;
            cout << "\nDo You Want to Enter More Instructions :";
            cin >> ch;
        } while (ch == 'y' || ch == 'Y');
        recno++;
        laststate = recno;
    }

    void display() {
        cout << "Recipe Name :" << name << endl;
        cout << "Ingredients :" << endl;
        for (int i = 0; i <= qcount; i++)
            cout << i + 1 << "." << quantity[i] << " " << unit[i] << " ";//<<ingredient[i]<<endl;
        for (i = 0; i <= icount; i++)
            cout << i + 1 << "." << instructions[i] << endl;
        cout << "last state :" << laststate;
    }
};

void main() {
    clrscr();
    recipe R;
    char ch;
    fstream file("DATA.DAT", ios::binary | ios::app);
    do {
        R.add();
        file.write((char*)&R, sizeof(R));
        cout << "DYWTC :";
        cin >> ch;
    } while (ch == 'y' || ch == 'Y');

    while (!file.eof()) {
        file.read((char*)&R, sizeof(R));
        R.display();
    }
    file.close();
    getch();
}
HMD
  • 2,202
  • 6
  • 24
  • 37
Balu Sk
  • 13
  • 4
  • You probably want to read this: https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong You should also consider splitting your qty/unit/ingredient/instruction into its own struct and use a vector of those in your recipe class to keep track of things. You will need to serialize each variable rather than writing a block, but really you need to do that anyway if you want this to be robust. – Retired Ninja Nov 06 '17 at 14:37
  • You also seem to be using a terribly old compiler if `iostream.h` is available. There are several modern free compilers to choose from, you might consider that as well. – Retired Ninja Nov 06 '17 at 14:40
  • I recommend spelling out acronyms, as most applications have enough memory for "Do you want to continue? ". – Thomas Matthews Nov 06 '17 at 15:04
  • Must read: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Thomas Matthews Nov 06 '17 at 15:06
  • You should try using proper serialisation instead of just casting a pointer to your class to a char*. I'm not sure whether it's undefined behaviour but it certainly isn't portable. – Sean Burton Nov 06 '17 at 16:54

1 Answers1

0

After a series of writes you have a file position at the very end of the file. When you start reading, you are still at the end of the file and there is nothing to read there.

To get back to the beginning you can do a seek file.seekg(0, ios::beg);.

Also, to enable both reading and writing you might need to also add ios::in | ios::out in the call to open.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203