C++ Program stops suddenly

Sainatarajan

Wise Old Owl
Question : I have to write a program to write object of class item to binary file then perform read,append,search and modify functions on the records. I have the written the following code.
Code:
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

const char filename[] = "ITEM.TXT";

class item
{
    int item_no;
    char name[20];
    long price;

public:
    void new_item()                                      // Get data from the user.
    {
        cout << "Enter The item Number : " << endl;
        cin  >> item_no;
        cout << "Enter The item Name : " << endl;
        cin  >> name;
        cout << "Enter The Price of the item : " << endl;
        cin  >> price;
    }

    void show_item()                                      // Display the entered data.
    {
        cout << "Item Number : " << item_no << endl;
        cout << "Item Name :   " << name    << endl;
        cout << "Item Price :  " << price   << endl;
    }

    int getino()                                          // Return the item number
    {
        return item_no;
    }

    void modify();
};

void item::modify()                                       // To Modify a data.
{
    cout << "Enter New Details : " << endl;
    cout << "Enter New Item Number : " << endl;
    cin  >> item_no;
    cout << "Enter New Item Name : " << endl;
    cin  >> name;
    cout << "Enter New Item Price : " << endl;
    cin  >> price;
}

int main()
{
    item i;
    item itm;
    char x = '0',ch='y';
    int in;

    while (ch=='y')
    {
        cout << "File Handling Menu :: \n"
                "1) To Enter New Record : \n"
                "2) To Append New Record : \n"
                "3) To Modify Existing Records : \n"
                "4) To View Records : \n"
                "5) Exit\n" << endl;

        cout << "Enter your choice : ";
        cin  >> x;

        switch(x)
        {

            case'1':
                {
                    ofstream fout(filename, ios::binary | ios::app);
                    itm.new_item();
                    fout.write((char*) &itm ,sizeof(itm));
                    fout.close();
                }
                break;
            case'4':
                {
                    ifstream fiot(filename, ios::binary);
                    cout << "Details Of Item : " << endl;

                    while (fiot.read((char*) &itm ,sizeof(itm)))
                    {
                        itm.show_item();
                    }
                    fiot.close();
                }
                break;
            case'3':
                {
                    fstream fil;
                    int in;
                    cout<<"Enter The Item Number To Modify : "<<endl;
                    cin>>in;
                    fil.open("ITEM.TXT",ios::in|ios::binary);
                    fil.read((char*)&itm, sizeof(itm));
                    while(!fil.eof())
                   {
                      if(in==itm.getino())
                      {
                          fil.seekg(0,ios::cur);
                          cout<<"Enter New Details.."<<endl;
                          itm.modify();
                          fil.seekp(fil.tellg() - sizeof(itm));
                          fil.write((char*)&itm, sizeof(itm));
                      }
                   }
                 fil.close();
                }
                break;
        }
        cout<<"Do you want to enter more press y : "<<endl;
        cin>>ch;
    }
return 0;
}
Everything went well, i entered the new info , i viewed the new info, the i press 3 to modify, the program stops after i enter the new data using modify() on the previously entered data. Can someone Please help me. i have to submit this program tomorrow.
 
In 'case 3':
Code:
while(!fil.eof())
                   {
                      if(in==itm.getino())
                      {
                          fil.seekg(0,ios::cur);
                          cout<<"Enter New Details.."<<endl;
                          itm.modify();
                          [B]fil.seekp(fil.tellg() - sizeof(itm));[/B]
                          fil.write((char*)&itm, sizeof(itm));
                      }
                   }

> Visual Studio gets an ambiguity with 3 overloads available of the '-' operator matching the provided argument types.

> The while loot is probably going in an infinite loop.
 
OP
Sainatarajan

Sainatarajan

Wise Old Owl
Will including a break; statement avoid that process. Another thing is that, after closing the program and reopening it, the data stored are lost. How can this e avoided?
 
Casting 'fil.tellg()' and 'sizeof(itm)' to integer and adding break did resolve the infinite loop but does not modify any entries. And data IS being saved. I enter the item, close the program and the data is still intact.

P.S: why are use using txt extension in a binary file?
 
OP
Sainatarajan

Sainatarajan

Wise Old Owl
Beginner in c++, that's why. So the program doesnt modify any data and is there a solution for that??
Data is still intact , but when i reopen the program and run it and press to view, it shows Item no = 0 , item price =0.

This is a new code which i have edited some lines.
Code:
#include <iostream>
#include <fstream>
#include <string.h>
#include<stdlib.h>
using namespace std;
class item
{
    int item_no;
    char name[20];
    long price;
public:
    void newdata();
    void showdata();
    int getino()
    {
        return item_no;
    }
}itm;
void item::newdata()
{
    cout<<"Enter The item Number : "<<endl;
    cin>>item_no;
    cout<<"Enter The item Name : "<<endl;
    cin>>name;
    cout<<"Enter The Price of the item : "<<endl;
    cin>>price;
}
void item::showdata()
{
    cout<<"Item Number : "<<item_no<<endl;
    cout<<"Item Name : "<<name<<endl;
    cout<<"Item Price : "<<price<<endl;
}
void Create();
void DisplayP();
void Modify();
int main()
{
    char ch='y';
    int x;
    ofstream fout("ITEM.TXT",ios::out|ios::binary);
    while(ch=='y'||ch=='Y')
    {
            cout<<"File Handling Menu :: "<<endl;
            cout<<"1) To Enter New Record : "<<endl;
            cout<<"2) To Append New Record : "<<endl;
            cout<<"3) To Modify Existing Records : "<<endl;
            cout<<"4) To View Records : "<<endl;
            cout<<"5) Exit\n";
            cout<<"Enter your choice : ";
            cin>>x;
         switch(x)
         {
         case 1:
            {
                Create();
                break;
            }
         case 2:
            {
                Create();
                break;
            }
         case 3:
            {
                Modify();
                break;
            }
         case 4:
            {
                DisplayP();
            }
         case 5:
            {
                exit(0);
            }
         }
    }
    cout<<"Do You Want To Enter More : "<<endl;
    cin>>ch;
    return 0;
}
void Create()
{
    fstream fil;
    fil.open("ITEM.TXT",ios::out| ios::binary);
    itm.newdata();
    fil.write((char*)&itm, sizeof(itm));
    fil.close();
}
void DisplayP()
{
    fstream fil;
    fil.open("ITEM.TXT",ios::in|ios::binary);
    fil.read((char*)&itm, sizeof(itm));
    itm.showdata();
    fil.close();
}
void Modify()
{
                fstream fil;
                int in;
                cout<<"Enter The Item Number To Modify : "<<endl;
                cin>>in;
                fil.open("ITEM.TXT", ios::in|ios::out|ios::binary); // open for input-output

                while (fil.read((char*)&itm, sizeof(itm)))
                {
                    if (in==itm.getino())
                    {
                        cout<<"Enter New Details.."<<endl;
                        itm.newdata();
                        fil.seekp(fil.tellg() - streampos(sizeof(itm)));
                        fil.write((char*)&itm, sizeof(itm));
                    }
                    break;
                }
                fil.close();
}
But when i close the program and open it again and run it and press 4 to view the records, previous data were deleted.
The Output shows the following.
Item Number : 0
Item Name :
Item Price : 0
 
Top Bottom