Program does not give desired output

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;
int rec=0,pos=0;
char found = 'n';
class item
{
    int item_no;
    char name[20];
    long price;
public:
    void new_item()                                      //To Get the data from the user.
    {
        ofstream fout("ITEM.TXT",ios::out);
        cout<<"Enter The item Number : "<<endl;
        cin>>item_no;
        fout<<item_no<<endl;
        cout<<"Enter The item Name : "<<endl;
        cin>>name;
        fout<<name<<endl;
        cout<<"Enter The Price of the item : "<<endl;
        cin>>price;
        fout<<price<<endl;
        fout.close();
        rec++;
    }
    void show_item()                                      //To Display the entered data.
    {
        cout<<"Item Number : "<<item_no<<endl;
        cout<<"Item Name : "<<name<<endl;
        cout<<"Item Price : "<<price<<endl;
    }
    int getino()                                          //To Return the item number
    {
    return item_no;
    }
    void modify();
};
void item::modify()                                       //To Modify a data.
{
    int io; char nme[20]; float value;
    cout<<"Enter New Details : "<<endl;
    cout<<"Enter New Item Number : "<<endl;
    cin>>io;
    cout<<"Enter New Item Name : "<<endl;
    cin>>nme;
    cout<<"Enter New Item Price : "<<endl;
    cin>>value;
}
int main()
{
    item i,itm;
    char x,in;
    char ch='y';
    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':
        i.new_item();
        break;
    case'2':
        {
        ofstream fout("ITEM.TXT",ios::app);
        i.new_item();
        fout.write((char*) &i ,sizeof(i));
        fout.close();
        rec++;
        }
            break;
    case'3':
    {
        fstream fio("ITEM.TXT",ios::in|ios::out);
        cout<<"Enter The Item Number To Modify : "<<endl;
        cin>>in;
        while(!fio.eof())
        {
           fio.read((char*) &i ,sizeof(i));
           if(i.getino()==in)
           {
               fio.seekg(-sizeof(i),ios::cur);
               i.modify();
               fio.write((char*) &i ,sizeof(i));
               found ='f';
               break;
           }
           fio.close();
           break;
         }
    }
    case'4':
        {
        fstream fiot("ITEM.TXT",ios::in|ios::out);
        fiot.seekg(0);
        cout<<"Details Of Item : "<<endl;
        while(!fiot.eof())
        {
            fiot.read((char*) &itm ,sizeof(itm));
            itm.show_item();
        }
        fiot.close();
        }

    }
    cout<<"Press Y to enter more or Press N to exit : "<<endl;
    cin>>ch;
}
    return 0;
}

This is a program to write object of class item to binary file then perform read,append,search and modify functions on the records. The program does not give the desired output. Can someone tell me What is wrong with this code
The program does not give the desired output.

It's helpful, descriptive statements like that which really make it easy for us to help you...

:rolleyes:
I would start with a cut-down version to begin with. Here's a version with options 2 and 4. I don't think option 1 is required. Option 3 you can add when you are satisfied with the basic version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#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';
    int in;

    while (x != '5')
    {
        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'2':
                {
                    ofstream fout(filename, ios::binary | ios::app);
                    i.new_item();
                    fout.write((char*) &i ,sizeof(i));
                    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();
                }

        }

    }

    return 0;
}
Last edited on
Thanks Chervil. The above program works without any error. Just when i copy/paste the modify() function and case 4 in the original program, it doesn't work. It asks to enter the item number and after entering, the file handling menu appears once again. And also, when i want to view the records, the program displays it 3 times.
Last edited on
The original program had a number of errors. For example, in the "modify" code,
1
2
        cout<<"Enter The Item Number To Modify : "<<endl;
        cin>>in;

in was of type char it should be int.

Also, the while(!fio.eof()) loop is wrong. Look at how it is done in my code for option 4. Of course you will need to open the file in the correct input-output mode,
fstream fio(filename,ios::in|ios::out | ios::binary);

I think the original code also had this inside the while loop which stopped it from working correctly.
1
2
    fio.close();
    break;


Thank You Chervil once again. One last thing. I have entered the data. The data is written to the file successfully. I also viewed the data from the program(output). It showed perfectly. Now, I modified the data. I once again viewed the data from the program(output), But it displays the old data.
The Modified Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  #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';
    int in;

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

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

        switch(x)
        {

            case'1':
                {
                    ofstream fout(filename, ios::binary | ios::app);
                    i.new_item();
                    fout.write((char*) &i ,sizeof(i));
                    fout.close();
                }
                break;


            case'3':
                {
                    ifstream fiot(filename, ios::binary);
                    fiot.seekg(0);
                    cout << "Details Of Item : " << endl;

                    while (fiot.read((char*) &itm ,sizeof(itm)))
                    {
                        itm.show_item();
                    }
                    fiot.close();
                    break;
                }
            case '2':
                {
                  fstream fio(filename,ios::in|ios::out|ios::binary);
                  cout<<"Enter The Item Number To Modify : "<<endl;
                  cin>>in;
                  while(fio.read((char*) &i ,sizeof(i)))
                  {
                   fio.read((char*) &i ,sizeof(i));
                   if(i.getino()==in)
                   {
                    fio.seekg(-sizeof(i),ios::cur);
                    i.modify();
                    fio.write((char*) &i ,sizeof(i));
                    break;
                   }
                  }
                  fio.close();
                  break;
                }

        }

    }

    return 0;
}

I think the error comes from the object. Did i write the data to one object and then viewed from the other?
Last edited on
In order to understand what is happening, you could try temporarily adding some extra cout statements around the call to seekg()
1
2
3
4
    cout << boolalpha;
    cout << "good: " << fio.good() << endl;
    fio.seekg(-sizeof(i),ios::cur);
    cout << "good: " << fio.good() << endl;  

When I tried this is showed that the call to seekg failed.

That's because the sizeof operator returns an unsigned integer, so putting a minus sign in front of the result, instead of giving a small negative number, gives a huge positive one. Try putting this, somewhere in the program just to see the value it gives:
cout << "diagnostic 1: " << -sizeof(i) << endl;

The answer is to convert the value to a signed type before negating it.
Perhaps the most correct way would be:
fio.seekg(-static_cast<streamoff>(sizeof(i)),ios::cur);
though this would probably work:
fio.seekg(-(int) sizeof(i),ios::cur);
Guys.. I have rewritten the program and still the output does not come as desired.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include<fstream>
#include<string.h>
using namespace std;
class item
{
    int item_no;
    char name[20];
    long price;
public:
    void newdata();
    void showdata();
    int getino()
    {
        return item_no;
    }
}i,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 Add();
void DisplayP();
void Modify();
int main()
{
    int x;
    ofstream fout("ITEM.TXT",ios::out|ios::binary);
    while(1)
    {
            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:
            {
                Add();
                break;
            }
         case 3:
            {
                Modify();
                break;
            }
         case 4:
            {
                DisplayP();
            }
         }
    }
    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));
    while(!fil.eof())
    {
    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::binary);
    fil.read((char*)&itm, sizeof(itm));
    while(!fil.eof())
    {
        if(strcmp(in,itm.getino())==0)
        {
            fil.seekg(0,ios::cur);
            cout<<"Enter New Details.."<<endl;
            itm.newdata();
            fil.seekp(fil.tellg() - sizeof(itm));
            fil.write((char*)&itm, sizeof(itm));
        }
    }
    fil.close();
}

The program prints some garbage value for the item number instead of the entered item number.
Last edited on
Well, all you really needed to do was to take the existing (working) code and cut+paste it into your new functions. Very minor changes would be required that way.

As it is, the latest code seems to have taken several steps backwards. For example here:
84
85
86
87
88
89
90
91
92
93
94
void DisplayP()
{
    fstream fil;
    fil.open("ITEM.TXT",ios::in|ios::binary);
    fil.read((char*)&itm, sizeof(itm));
    while(!fil.eof())
    {
    itm.showdata();
    }
    fil.close();
}

Lines 89 to 92 look pretty much like an infinite loop to me.

And just picking another line from the code here, line 105:
if(strcmp(in,itm.getino())==0)
both in and the value returned by getino() are integers, so why they would be compared as c-strings is beyond me.


Just taking a step back for a moment, to an earlier version:
http://www.cplusplus.com/forum/beginner/115239/#msg629090
There was an error I overlooked. At line 105:
100
101
102
103
104
105
106
    fstream fio(filename,ios::in|ios::out|ios::binary);
    cout<<"Enter The Item Number To Modify : "<<endl;
    cin>>in;
    while(fio.read((char*) &i ,sizeof(i)))
    {
        fio.read((char*) &i ,sizeof(i)); // this line is not needed
        if (i.getino()==in)

The file is being read twice in the loop, once as part of the while condition, and again in the body of the loop. The second read (at line 105) is not needed.
Last edited on
As you said, i took the previous working code and just added the modify() function. 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. This is the code that you(Chervil) posted , and i just entered the modify() function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#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';
    int in;

    while (x != '5')
    {
        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();
                }
            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.new_item();
                          fil.seekp(fil.tellg() - sizeof(itm));
                          fil.write((char*)&itm, sizeof(itm));
                      }
                   }
                 fil.close();
                }

        }

    }

    return 0;
}

By program stops i mean , no crash or whatever. It just stops.
Last edited on
Ok, let's look at this, just the code for case 3.
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
                {
                    fstream fil;
                    int in;
                    cout<<"Enter The Item Number To Modify : "<<endl;
                    cin>>in;
                    fil.open("ITEM.TXT",ios::in|ios::binary); // Open file for input only -but we need output too
                    fil.read((char*)&itm, sizeof(itm));  // Read the first item from the file
                    while(!fil.eof())                    // now loop forever
                   {
                      if(in==itm.getino())
                      {
                          fil.seekg(0,ios::cur);
                          cout<<"Enter New Details.."<<endl;
                          itm.new_item();
                          fil.seekp(fil.tellg() - sizeof(itm));
                          fil.write((char*)&itm, sizeof(itm));
                      }
                   }
                 fil.close();
                }

Do you see why there is an infinite loop?

Take a look at the code for option 4. That is the correct way to read each item from the file. Of course the code inside the body of the loop needs to be different. But the overall structure should be similar.

In this case it is a logic error. But as a general recommendation, it is usually a bad idea to put eof() in a while condition.

Try changing your code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
            {
                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.new_item();
                        fil.seekp(fil.tellg() - streampos(sizeof(itm)));
                        fil.write((char*)&itm, sizeof(itm));
                    }
                }
                fil.close();
                break;
            }  



I do like the idea of using separate functions, so go ahead and put the working code inside your functions.
Thanks Bro, everything worked.
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
Last edited on
I ran the same code with no problems. You must have changed something else, I think.
I use Code Blocks. Will this be causing any problems.
Last edited on
Line 41:
 
ofstream fout("ITEM.TXT",ios::out|ios::binary);

This creates a new output file,which replaces any previous file with the same name. You don't need this line at all, because each of your functions does its own file handling..
I dont know you to thank you bro, Everything works fine. I think this would do.
1
2
3
4
for(i=0;i<1000;i++)
{
cout<<"Thank You "<<endl;
}

And Thank you once again. =D
No problem, you're welcome.
Topic archived. No new replies allowed.