programming printing values twice

i am trying to make a hotel management system in c++ and it is almost in its complete phase but having some logical issues.These some things needs to be sorted out.
Issues are

1- In the customer record displaying function, same value is printed 2-3 times .Often the last string entered is printed twice.
2- If any entity is deleted by delete function it is removed but some other value is printed in its replacement (often the upcoming value is printed twice).

please take a look at the code and do let me know what can be the logical issue

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include<iostream>
#include <conio.h>
#include<fstream>
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
#include <ClearScreen.h>
using namespace std;

class hotel
{
    int room_no;
    char name[20];
    char city[50];
    char phone[20];

    public:
    void main_menu();    //to display the main menu
    void add();        //to book a room
    void display();    //to display the customer record
    void rooms();    //to display alloted rooms
    void edit();    //to edit the customer record
    int check(int);    //to check room status
    void modify(int);    //to modify the record
    void delete_rec(int);    //to delete the record
};

void hotel::main_menu()
{
    int choice;

    while(choice!=5)
    {

        cout<<"\n\t\t\t\t*************";
        cout<<"\n\t\t\t\t* MAIN MENU *";
        cout<<"\n\t\t\t\t*************";
        cout<<"\n\n\n\t\t\t1.Book A Room";
        cout<<"\n\t\t\t2.Customer Record";
        cout<<"\n\t\t\t3.Rooms Allotted";
        cout<<"\n\t\t\t4.Edit Record";
        cout<<"\n\t\t\t5.Exit";
        cout<<"\n\n\t\t\tEnter Your Choice: ";
        cin>>choice;

        switch(choice)
        {
            case 1: add();
                    break;
            case 2: display();
                    break;
            case 3: rooms();
                    break;
            case 4: edit();
                    break;
            case 5: break;
            default:
                    {
                        cout<<"\n\n\t\t\t Wrong choice!!!";
                        cout<<"\n\t\t\t Press any key to continue!!";
                        _getch();
                    }
        }
    }
}

void hotel::add()
{
clrscr();
    int r,flag;
    ofstream fout("Record.txt",ios::app);

    cout<<"\n Enter Customer Details";
    cout<<"\n **********************";
    cout<<"\n\n Room no: ";
    cin>>r;
    flag=check(r);

    if(flag)
        cout<<"\n Sorry..!!!Room is already booked";
    else
    {
        room_no=r;
        cout<<" Name: ";
        cin>>name;
        cout<<" City: ";
        cin>>city;
        cout<<" Phone No: ";
        cin>>phone;
        fout.write((char*)this,sizeof(hotel));
        cout<<"\n Room is booked!!!";
    }

    cout<<"\n Press any key to continue!!";
    fout.close();
}

void hotel::display()
{
clrscr();
    ifstream fin("Record.txt",ios::in);
    int r,flag;
    cout<<"\n Enter room no: ";
    cin>>r;

    while(!fin.eof())
    {
        fin.read((char*)this, sizeof(hotel));
        if(room_no==r)
        {
            cout<<"\n Customer Details";
            cout<<"\n ****************";
            cout<<"\n\n Room no: "<<room_no;
            cout<<"\n Name: " <<name;
            cout<<"\n City: "<<city;
            cout<<"\n Phone no: "<<phone;
            flag=1;
            break;
        }
    }

    if(flag==0)
        cout<<"\n Sorry Room no. not found or vacant!!";

    cout<<"\n\n Press any key to continue!!";

    fin.close();
}

void hotel::rooms()
{

clrscr();
    ifstream fin("Record.txt",ios::in);
    cout<<"\n\t\t\tList Of Rooms Allotted";
    cout<<"\n\t\t\t*********************";
    cout<<"\n\nRoom No.\tName\t\tAddress\t\tPhone No.\n";

    while(!fin.eof())
    {
        fin.read((char*)this,sizeof(hotel));
        cout<<"\n\n "<<room_no<<"\t\t"<<name;
        cout<<"\t\t"<<city<<"\t\t"<<phone;
    }

    cout<<"\n\n\n\t\t\tPress any key to continue!!";
    _getch();
    fin.close();
}

void hotel::edit()
{
clrscr();
    int choice,r;

    cout<<"\n EDIT MENU";
    cout<<"\n *********";
    cout<<"\n\n 1.Modify Customer Record";
    cout<<"\n 2.Delete Customer Record";

    cout<<"\n Enter your choice: ";
    cin>>choice;
clrscr();
    cout<<"\n Enter room no: ";
    cin>>r;

    switch(choice)
    {
        case 1: modify(r);
                break;
        case 2: delete_rec(r);
                break;
        default: cout<<"\n Wrong Choice!!";
    }

    cout<<"\n Press any key to continue!!!";
    _getch();
}

int hotel::check(int r)
{

    int flag=0;
    ifstream fin("Record.txt",ios::in);
    while(!fin.eof())
    {
        fin.read((char*)this,sizeof(hotel));
        if(room_no==r)
        {
            flag=1;
                break;
        }
    }

    fin.close();
    return(flag);
}

void hotel::modify(int r)
{

    long pos,flag=0;
    fstream file("Record.txt",ios::in|ios::out|ios::binary);

    while(!file.eof())
    {
        pos=file.tellg();
        file.read((char*)this,sizeof(hotel));
        if(room_no==r)
        {
            cout<<"\n Enter New Details";
            cout<<"\n *****************";
            cout<<"\n Name: ";
            cin>>name;
            cout<<"\n City: ";
            cin>>city;
            cout<<"\n Phone No: ";
            cin>>phone;

            file.seekg(pos);
            file.write((char*)this,sizeof(hotel));
            cout<<"\n Record is modified!!";
            flag=1;
            break;
        }
    }

    if(flag==0)
        cout<<"\n Sorry Room no. not found or vacant!!";

    file.close();
}

void hotel::delete_rec(int r)
{

    int flag=0;
    char ch;
    ifstream fin("Record.txt",ios::in);
    ofstream fout("temp.txt",ios::out);

    while(!fin.eof())
    {
        fin.read((char*)this,sizeof(hotel));
        if(room_no==r)
        {
            cout<<"\n Name: "<<name;
            cout<<"\n City: "<<city;
            cout<<"\n Phone No: "<<phone;
            cout<<"\n\n Do you want to delete this record(y/n): ";
            cin>>ch;

            if(ch=='n')
            fout.write((char*)this,sizeof(hotel));

            flag=1;
        }
        else
            fout.write((char*)this,sizeof(hotel));
    }

    fin.close();
    fout.close();

    if(flag==0)
        cout<<"\n Sorry room no. not found or vacant!!";
    else
    {
        remove("Record.txt");
        rename("temp.txt","Record.txt");
    }
}

int main()
{
   hotel h;
    cout<<"\n\t\t\t****************************";
    cout<<"\n\t\t\t* HOTEL MANAGEMENT SYSTEM *";
    cout<<"\n\t\t\t****************************";

    cout<<"\n\n\n\n\t\tMade By:";
    cout<<"\n\t\t Ahmad Imtiaz"<<endl;
    cout<<"\t\t\tPress any key to continue!!";
    _getch();
    h.main_menu();
}
Last edited on
When you finish reading the last record, the stream doesn't know it's at the end of the file. It only knows when you try to read past it. So, for example, line 244 will try to read one more record than there exists in the file. So in your various loops, instead of doing:
1
2
3
4
    while(!fin.eof())
    {
        fin.read((char*)this, sizeof(hotel));
        ...
You should:
1
2
   while(fin.read((char*)this, sizeof(hotel))) {
        ...
I changed

1
2
3
4
    while(!fin.eof())
    {
        fin.read((char*)this, sizeof(hotel));
        ...


to this

1
2
while(fin.read((char*)this, sizeof(hotel))) {
        ...

and it resolved the issue . Thank you Problem solved.
Hello ehmad,

Your biggest problem is while(!fin.eof()) is not working the way you are thinking. By the time the while condition figures out that you are at "eof" you have processed the last good read twice. The first time when it was good and the second time after "fin" fails, but still has to process something which is what is still in the variables from the last read.

For an example in the display function: while (fin.read((char*)this, sizeof(hotel))) would work better. Another option is read before the while loop and again the end of the while loop.

Nothing else jumped out for now, so I will have to check out the cade in a little while.

Hope that helps,

Andy
Topic archived. No new replies allowed.