where's my mistake? [SOLVED]

Hi! I have to make a program in C++ using Visual C++ 6.0 .There must be a list of options for operating with the program:
1..Creating a file which holds information about all of the teachers in the university.
2..Showing the information ,written in the file, on the screen.
3..Adding information about a teacher.
4..Deleting information about a teacher

All of the functions work exept for the last one.When I delete the info about a teacher and try to show the new information written in the file it says "Can't read the file".Can you please tell me where my mistake is because I can't find it by myself and I need to have this program done by tuesday.

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

const N=10;
long begin,end;
struct prep //structure that hold the info about the teachers
{
char ime[10]; //name
char prezime[10]; //surname
char familiq[10]; //family name
int hours;
};


int static m;
fstream fp;

void vyvejdane(struct prep pre[]); //creating a file
void chetene(struct prep pre[]); //reading from the file
void dopylvane(struct prep pre[]); //adding info
void iztrivane(struct prep pre[]); //deleting info

int main()
{
int izbor;

struct prep pre[N];

do
{
do
{

cout<<endl<<endl;
cout<<setw(50)<<"* * * * * meniu * * * * *"<<endl;
cout<<setw(50)<<"* 1.syzdavane na fail *"<<endl;
cout<<setw(50)<<"* 2.chetene *"<<endl;
cout<<setw(50)<<"* 3.dopylvane *"<<endl;
cout<<setw(50)<<"* 4.iztrivane *"<<endl;
cout<<setw(50)<<"* 5.krai *"<<endl;
cout<<setw(50)<<"* * * * * * * * * * * * *"<<endl;

cin>>izbor;

if(izbor<1 || izbor>5)
cout<<endl<<"Greshka pri vyvejdane!!! Molia vyvedete otnovo..."<<endl<<endl<<endl;

}

while(izbor<1 || izbor>4);


switch(izbor)
{
case 1: vyvejdane(pre); break;
case 2: chetene(pre); break;
case 3: dopylvane(pre); break;
case 4: iztrivane(pre); break;
case 5: break;
default: cout<<"Greshka pri vyvejdane!!! Molia vyvedete otnovo..."<<endl;
}
}

while(izbor!=5);

return izbor;

}


void vyvejdane(struct prep pre[])

{
cout<<"Molia vyvedete broia na prepodavatelite"<<endl;
cin>>m; //m-number of teachers

fp.open("my_file.dat",ios::binary|ios::out);

if(!fp)
{ cout<<"Greshka pri syzdavane na faila"<<endl;
exit(1); }

for(int i=0; i<m; i++)
{
fflush(stdin);
cout<<endl<<"VYVEDETE DANNITE ZA PREPODAVATEL "<<i+1<<endl<<endl;
cout<<"vyvedete ime i natisnete ENTER... ";
cin>>pre[i].ime;
cout<<"vyvedete prezime i natisnete ENTER... ";
cin>>pre[i].prezime;
cout<<"vyvedete familiia i natisnete ETER... ";
cin>>pre[i].familiq;
cout<<"vyvedete broiat otrabotenite chasove za sedmica i natisnete ENTER... ";
cin>>pre[i].hours;

};

fp.write((char*)pre,m*sizeof(prep));

fp.close();


}


void chetene(struct prep pre[])

{

int i=0;
struct prep c;

cout<<endl<<"STATUS NA PREPODAWATELITE : "<<endl<<endl<<endl;


fp.open("my_file.dat",ios::binary|ios::in);
if(!fp)
{
cout<<endl<<"Greshka pri prochitane na faila!"<<endl; //error
exit(1);
}

cout<<setw(10)<<"IME";
cout<<setw(15)<<"PREZIME";
cout<<setw(15)<<"FAMILIIA";
cout<<setw(15)<<"CHASOVE";
cout<<setw(19)<<"NATOVARENOST"<<endl<<endl;

while(fp.good())
{
fp.read((char*)&c,sizeof(prep));
pre[i]=c;

cout<<setw(10)<<pre[i].ime;
cout<<setw(15)<<pre[i].prezime;
cout<<setw(13)<<pre[i].familiq;
cout<<setw(13)<<pre[i].hours<<setw(3)<<" ";

if(pre[i].hours >55) cout<<setw(22)<<"pretovaren"<<endl;
else if(pre[i].hours >35 && pre[i].hours <55) cout<<setw(21)<<"normalno natovaren"<<endl;
else if(pre[i].hours <35) cout<<setw(22)<<"slabo natovaren"<<endl<<endl<<endl;

i++; }

fp.close();

}


void dopylvane(struct prep pre[]) //adding

{

prep cs;

fp.open("my_file.dat",ios::binary|ios::app);

if(!fp)
{ cout<<"Greshka pri obnoviavane na faila!!!";
exit(1); }

cout<<"Vyvedete ime i natisnete ENTER... ";
cin>>cs.ime;
cout<<"Vyvedete prezime i natisnete ENTER... ";
cin>>cs.prezime;
cout<<"Vyvedete familiia i natisnete ENTER... ";
cin>>cs.familiq;
cout<<"Vyvedete broiat otraboteni chasove za sedmica i natisnete ENTER... ";
cin>>cs.hours;

pre[m]=cs;

fp.write((char*)&cs,sizeof(prep));
cout<<cs.ime;
cout<<cs.prezime;
cout<<cs.familiq;
cout<<cs.hours;

m++;

fp.close();

}


void iztrivane(struct prep pre[]) //deleting
{
int nomer;

struct prep f[10];
int beg,en;

fp.open("my_file.dat", ios::binary|ios::in);

fp.seekg(0, ios::beg);
begin = fp.tellg();
beg=(begin/sizeof(prep));
fp.seekg (0l, ios::end);
end = fp.tellg();
en=(end/sizeof(prep));

cout<<"Molia vyvedete nomer na prepodavatel, chiito danni jelatete da iztriete"<<endl<<endl;

cin>>nomer; //the number of the teacher whose data you want to delete

fp.seekg((nomer)*sizeof(prep));



for(int i=nomer, l=0; i<=en ; i++)
{
pre[i]=f[l];

l++ ; };

fp.seekg((nomer-1),ios::beg);

for(int k=(nomer-1), s=0; k<10; k++)
{
f[s]=pre[k];

k++ ; };


fp.write((char*)pre,(m-1)*sizeof(prep));


cout<<"Dannite sa iztriti!"<<endl<<endl; //"The data is deleted"

fp.close();

}



That's what I managed to do.If somebody can help me pls do so if you have some time. Thanks in advance




Last edited on
Hello evanescence.
I did NOT edit your code. I only put it in code tags and added indentations to it.

I did not find anything wrong with it. But I am a beginner and you should wait from someone else to see it :D


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
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

const N=10;
long begin,end;
struct prep //structure that hold the info about the teachers
{
    char ime[10]; //name
    char prezime[10]; //surname
    char familiq[10]; //family name
    int hours;
};

int static m;
fstream fp;

void vyvejdane(struct prep pre[]); //creating a file
void chetene(struct prep pre[]); //reading from the file
void dopylvane(struct prep pre[]); //adding info
void iztrivane(struct prep pre[]); //deleting info

int main()
{
    int izbor;

    struct prep pre[N];

    do
    {
        do
        {
        cout<<endl<<endl;
        cout<<setw(50)<<"* * * * * meniu * * * * *"<<endl;
        cout<<setw(50)<<"* 1.syzdavane na fail *"<<endl;
        cout<<setw(50)<<"* 2.chetene *"<<endl;
        cout<<setw(50)<<"* 3.dopylvane *"<<endl;
        cout<<setw(50)<<"* 4.iztrivane *"<<endl;
        cout<<setw(50)<<"* 5.krai *"<<endl;
        cout<<setw(50)<<"* * * * * * * * * * * * *"<<endl;

        cin>>izbor;

        if(izbor<1 || izbor>5)
        cout<<endl<<"Greshka pri vyvejdane!!! Molia vyvedete otnovo..."<<endl<<endl<<endl;

        }

        while(izbor<1 || izbor>4);


        switch(izbor)
        {
        case 1: vyvejdane(pre); break;
        case 2: chetene(pre); break;
        case 3: dopylvane(pre); break;
        case 4: iztrivane(pre); break;
        case 5: break;
        default: cout<<"Greshka pri vyvejdane!!! Molia vyvedete otnovo..."<<endl;
        }
    }

    while(izbor!=5);

    return izbor;
}


void vyvejdane(struct prep pre[])

{
    cout<<"Molia vyvedete broia na prepodavatelite"<<endl;
    cin>>m; //m-number of teachers

    fp.open("my_file.dat",ios::binary|ios::out);

    if(!fp)
    {
        cout<<"Greshka pri syzdavane na faila"<<endl;
        exit(1);
    }

    for(int i=0; i<m; i++)
    {
        fflush(stdin);
        cout<<endl<<"VYVEDETE DANNITE ZA PREPODAVATEL "<<i+1<<endl<<endl;
        cout<<"vyvedete ime i natisnete ENTER... ";
        cin>>pre[i].ime;
        cout<<"vyvedete prezime i natisnete ENTER... ";
        cin>>pre[i].prezime;
        cout<<"vyvedete familiia i natisnete ETER... ";
        cin>>pre[i].familiq;
        cout<<"vyvedete broiat otrabotenite chasove za sedmica i natisnete ENTER... ";
        cin>>pre[i].hours;

    };

    fp.write((char*)pre,m*sizeof(prep));

    fp.close();
}

void chetene(struct prep pre[])
{
    int i=0;
    struct prep c;

    cout<<endl<<"STATUS NA PREPODAWATELITE : "<<endl<<endl<<endl;


    fp.open("my_file.dat",ios::binary|ios::in);
    if(!fp)
    {
        cout<<endl<<"Greshka pri prochitane na faila!"<<endl; //error
        exit(1);
    }

    cout<<setw(10)<<"IME";
    cout<<setw(15)<<"PREZIME";
    cout<<setw(15)<<"FAMILIIA";
    cout<<setw(15)<<"CHASOVE";
    cout<<setw(19)<<"NATOVARENOST"<<endl<<endl;

    while(fp.good())
    {
        fp.read((char*)&c,sizeof(prep));
        pre[i]=c;

        cout<<setw(10)<<pre[i].ime;
        cout<<setw(15)<<pre[i].prezime;
        cout<<setw(13)<<pre[i].familiq;
        cout<<setw(13)<<pre[i].hours<<setw(3)<<" ";

        if(pre[i].hours >55) cout<<setw(22)<<"pretovaren"<<endl;
        else if(pre[i].hours >35 && pre[i].hours <55) cout<<setw(21)<<"normalno natovaren"<<endl;
        else if(pre[i].hours <35) cout<<setw(22)<<"slabo natovaren"<<endl<<endl<<endl;

        i++;
    }

    fp.close();

}

void dopylvane(struct prep pre[]) //adding
{

    prep cs;

    fp.open("my_file.dat",ios::binary|ios::app);

    if(!fp)
    {
        cout<<"Greshka pri obnoviavane na faila!!!";
        exit(1);
    }

    cout<<"Vyvedete ime i natisnete ENTER... ";
    cin>>cs.ime;
    cout<<"Vyvedete prezime i natisnete ENTER... ";
    cin>>cs.prezime;
    cout<<"Vyvedete familiia i natisnete ENTER... ";
    cin>>cs.familiq;
    cout<<"Vyvedete broiat otraboteni chasove za sedmica i natisnete ENTER... ";
    cin>>cs.hours;

    pre[m]=cs;

    fp.write((char*)&cs,sizeof(prep));
    cout<<cs.ime;
    cout<<cs.prezime;
    cout<<cs.familiq;
    cout<<cs.hours;

    m++;

    fp.close();

}


void iztrivane(struct prep pre[]) //deleting
{
    int nomer;

    struct prep f[10];
    int beg,en;

    fp.open("my_file.dat", ios::binary|ios::in);

    fp.seekg(0, ios::beg);
    begin = fp.tellg();
    beg=(begin/sizeof(prep));
    fp.seekg (0l, ios::end);
    end = fp.tellg();
    en=(end/sizeof(prep));

    cout<<"Molia vyvedete nomer na prepodavatel, chiito danni jelatete da iztriete"<<endl<<endl;

    cin>>nomer; //the number of the teacher whose data you want to delete

    fp.seekg((nomer)*sizeof(prep));



    for(int i=nomer, l=0; i<=en ; i++)
    {
        pre[i]=f[l];

        l++ ;
    };

    fp.seekg((nomer-1),ios::beg);

    for(int k=(nomer-1), s=0; k<10; k++)
    {
        f[s]=pre[k];

        k++ ;
    };


    fp.write((char*)pre,(m-1)*sizeof(prep));


    cout<<"Dannite sa iztriti!"<<endl<<endl; //"The data is deleted"

    fp.close();

}
Last edited on
thanks anyway :)
If you want to delete the teacher on nomer, just move to the left the entire list and that way u delete the teacher you want, now you will have the last two the same so, m-- to delete the last one, and ready.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 fp.seekg((nomer)*sizeof(prep));
 
   for(int k=(nomer-1); k<10; k++)
    {
        pre[k]=pre[k+1];
    };
   m--;  

  fp.write((char*)pre, m*sizeof(prep));


  cout<<"Dannite sa iztriti!"<<endl<<endl; //"The data is deleted"

  fp.close();

Last edited on
Unfortunately this didn't work out and honestly I still can't figure out why cause it seems to be OK.I tried sth a bit different and here it is:

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
void iztrivane(struct prep pre[]) // deleting
	 {
         int nomer;
         struct prep f[50];
		 struct prep t[50];
		 int en;
		 int l;
	

		 fp.open(LIST, ios::binary|ios::in);

	   
    	fp.seekg (0l, ios::end);  //finds the number of elements in the massiv
	    end = fp.tellg();        
		en=(end/sizeof(prep));


		cout<<"Molia vyvedete nomer na prepodavatel, chiito danni jelatete da iztriete"<<endl<<endl;
		cin>>nomer; // number of a  teacher


        for(int i=0,s=0; i<(nomer-1); i++)  //saving the elements before the one we wanna erase in a new massiv t
      
		{  
            t[s]=pre[i];

			s++;  };

         
		for( i=nomer, l=0; i<=en ; i++) //saving the elements after the one we wanna delete in a new massive f
		{
			f[l]=pre[i];

			l++ ; };

			fp.seekg((nomer-1),ios::beg);

		
           fp.close();

		   fp.open(LIST,ios::binary|ios::out);


              fp.write((char*)t,(nomer-1)*sizeof(prep)); //saving the elements before the one we wanna erase in the file
			  fp.write((char*)f,(l+1)*sizeof(prep)); //saving the elements after the one we wanna erase in the file


             fp.close();

			 m--;

		cout<<"Dannite sa iztriti!"<<endl<<endl;

				

	 }  


Now it works OK but ...after I do these steps:
1) erase a teacher
2) add a new one
3)try to read the file again

at the 3d step there's a problem- it openes the file but it seems to be ruined. Any ideas how to fix that?
1
2
3
   
   fp.write((char*)t,(nomer-1)*sizeof(prep)); //saving the elements before the one we wanna erase in the file
   fp.write((char*)f,(l+1)*sizeof(prep)); //saving the elements after the one we wanna erase in the file 


When you do the last loop the l value is incremented once more, i think that you need l - 1, not add one, try it and let my know,

fp.write((char*)f,(l-1)*sizeof(prep));

by the way, good solution, is a liltle large but still good, :)
Last edited on
Topic archived. No new replies allowed.