FILE HANDLING: Bug in displaying the name and date of birth. How to fix this?

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
#include <iostream>
#include <fstream>
#include <conio.h>
#include <cstring>


using namespace std;

class crim_rec
{
    char name[50];
	char sex;
	char fathr_name[50];
	char addrs[50];
	char offense[20];
	char blood[15];
	char dob[25];
    int crim_code, reward;
    
public:
	void disp();
    void add();
    void wtf();
    void rff();
    void search();
    void del();
    void mod();

}c;
void crim_rec::add()
{
	system("cls");
	cout<<"\n\n\n\t          Enter name of criminal: ";
	cin>>name;
	cin.get(name, 50);
        cout<<"\n\t          Sex (m/f): ";
        cin>>sex;
        cout<<"\n\t          Enter date of birth: ";
        cin>>dob;
	gets(dob);
        cout<<"\n\t          Enter blood type (Ap/An/Bp/Bn/ABp/ABn/Op/On): ";
        gets(blood);
        cout<<"\n\t          Enter father's name: ";
        gets(fathr_name);
        cout<<"\n\t          Enter address: ";
        gets(addrs);
        cout<<"\n\t          Enter crime commited: ";
        gets(offense);
        cout<<"\n\t          Enter reward on criminal: ";
        cin>>reward;
        system("cls");
}
void crim_rec::disp()
{
	system("cls");
	cout<<"\n\n\n\t\t          The record of criminal:\n";
    cout<<"\n\n\t\t          Name: "<<name;
    cout<<"\n\n\t\t          Sex: "<<sex;
    cout<<"\n\n\t\t          Date Of Birth: "<<dob;
    cout<<"\n\n\t\t          Blood Type: "<<blood;
    cout<<"\n\n\t\t          Father's name: "<<fathr_name;
    cout<<"\n\n\t\t          Address: "<<addrs;
    cout<<"\n\n\t\t          Crime: "<<offense;
    cout<<"\n\n\t\t          Reward: "<<reward<<endl;
    getch();
}
void crim_rec::wtf()
{
    ofstream ofile;
    ofile.open("CESIS.txt", ios::app);
    add();
    ofile.write((char*)&c, sizeof(c));
    ofile.close();
}
void crim_rec::rff()
{
    ifstream ifile;
    ifile.open("CESIS.txt");
    ifile.seekg(0, ios::beg);
    ifile.read((char*)&c, sizeof(c));
    while(ifile)
    {
        disp();
        ifile.read ((char*)&c, sizeof(c));
    }
    ifile.close();
}
void crim_rec::search()
{
	system("cls");
	char m[50];
    ifstream ifile("CESIS.txt");
    puts("\n\n\n\t          Enter name of criminal which has to be searched:");
    cout<<"\n\t\t  ";
    cin>>m;
    gets(m);
    ifile.seekg (0, ios::beg);
    ifile.read((char*)&c, sizeof(c));
    while(ifile)
    {
        if (strcmpi(m, name)==0)
            disp();
            ifile.read((char*)&c, sizeof(c));
    }
    ifile.close();
}
void crim_rec::del()
{
	system("cls");
	char b[50];
    ifstream ifile;
    ifile.open("CESIS.txt", ios::app);
    ofstream ofile;
    ofile.open("new.txt", ios::app);
    puts("\n\n\n          Enter the name of the criminal whose records you want to delete: ");
    cout<<"\n\t  ";
    cin>>b;
    gets(b);
    ifile.seekg (0, ios::beg);
    ifile.read((char*)&c, sizeof(c));
    while(ifile)
    {
        if (strcmpi(b, name))
            ofile.write((char*)&c, sizeof(c));
        ifile.read((char*)&c, sizeof(c));
    }
    ifile.close();
    ofile.close();
    remove ("CESIS.txt");
    rename("new.txt", "CESIS.txt");
    rename("new.txt", "CESIS.txt");
}
void crim_rec::mod()
{
	
	system("cls");
	char d[50];
    int p;
    puts("\n\n\n          Enter name of criminal whose record you want to modify:");
    cout<<"\n\t  ";
    cin>>d;
    gets(d);
    fstream f;
    f.open("CESIS.txt", ios::in|ios::out);
    f.seekg(0, ios::beg);
    f.read((char*)&c, sizeof(c));
    int a=f.tellg();
    while(! f.eof())
    {
    	
        if (!strcmpi(d, name))
        {
        	system("cls");
            puts("\n\n\t          Please 1 enter new details of the criminal ");
            cout<<"\t\t  ";
            cin>>p;
            switch(p)
        	{
        	case 1:
        		system("cls");
        		cout<<"\n\n\t\tPlease enter the new details of the criminal";
            	cout<<"\n\n\n\t          Enter name of criminal: ";
				cin>>name;
				gets(name);
    			cout<<"\n\t          Sex (m/f): ";
    			cin>>sex;
    			cout<<"\n\t          Enter date of birth: ";
    			cin>>dob;
				gets(dob);
    			cout<<"\n\t          Enter blood type (Ap/An/Bp/Bn/ABp/ABn/Op/On): ";
    			gets(blood);
    			cout<<"\n\t          Enter father's name: ";
    			gets(fathr_name);
    			cout<<"\n\t          Enter address: ";
    			gets(addrs);
    			cout<<"\n\t          Enter crime commited: ";
    			gets(offense);
    			cout<<"\n\t          Enter reward on criminal: ";
    			cin>>reward;    
    			system("cls");
    			break;
    		default:
    			break;
            }
            f.seekg(a-sizeof(c), ios::beg);
            f.write((char*)&c, sizeof(c));
        }
        f.read((char*)&c, sizeof(c));
        a=f.tellg();
   }
   f.close();
} 


int main ()
{
	system("cls");
    int ch;
    char choice;
    do
    {
    cout<<"\n\n\t\tComputer Engineering Students' Intelligence Services\n";
    cout<<"\n\n\t\t    ********************************************";
    cout<<"\n\n\t\t    *       1. View criminal details           *";
    cout<<"\n\n\t\t    *       2. Add new criminal details        *";
    cout<<"\n\n\t\t    *       3. Search a criminal record        *";
    cout<<"\n\n\t\t    *       4. Delete a criminal record        *";
    cout<<"\n\n\t\t    *       5. Modify a criminal record        *";
    cout<<"\n\n\t\t    *       6. Exit program                    *";
    cout<<"\n\n\t\t    ********************************************";
    cout<<"\n\n\t\t\t    Enter your choice (1-5): ";
    cin>>ch;
	cout<<endl;

    switch(ch)
    {
    case 1:
            c.rff();
       	 	break;
    case 2:
            c.wtf();
            break;
    case 3:
            c.search();
            break;
    case 4:
      	 	c.del();
        	break;
    case 5:
        	c.mod();
        	break;
	case 6:
			system("cls");
			cout<<"\n\n\t\t\tP R O G R A M   I S   C L O S I N G . . .";
			return 0;
			break;		    	
    default:
        {
        system("cls");	
        cout<<"\n\n\t\t\t          E R R O R !";
        }
        break;
    }
    cout<<"\n\n\n\n\n\t\t\t    C O N T I N U E? (y/n): ";
    cin>>choice;
    system("cls");
    }while(choice=='y');
    cout<<"\n\n\t\t\tP R O G R A M   I S   C L O S I N G . . .";
return 0;
}
Last edited on
Are you putting spaces or formatting in the DOB ?

putting a space in the name or dob will not work the way the cin statements are written.
I want Dob something like this "26 January 2017". Also the name w spaces. Did you see the bug sir?
"I have trouble formatting my output..."

// Here's 240 lines of irrelevant code


Learn to present minimal examples. Not only does it make it likely more people may attempt to answer, it is more likely you'll discover the problem/solution by distilling the code down to the necessities yourself.
Topic archived. No new replies allowed.