using iostreams but dont work properly

i write data in binary mode " ios::binary "
but data comes in this form:

ìý" 2“yvÜŸ þÿÿÿÛsv³Ãßw €@èw · èw   èw ¬þ"

i dont understand. here is my full 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include<fstream>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<conio.h>

using namespace std;

struct node
{
node *pre;
int roll_no;
char name[40],grade,ch;
int p_m ,c_m,m_m,cs_m,e_m;
int num;
float per;

node *nxt;
};
node *start,*cur,*temp;

class student
{
public:	

student()
{
start=NULL;
}

void calculate();
void getdata();	
void showdata();	
void show_tabular();
int retrollnumber();

}; 


void student::calculate()
{
node *percent;
percent->per=(percent->p_m + percent->c_m + percent->m_m + percent->e_m + percent->cs_m)/5;
if(percent->per>=60)
percent->grade='A';
else if(percent->per>=50)
percent->grade='B';
else if(percent->per>=33)
percent->grade='C';
else
percent->grade='F';
}



void student::showdata()
{

cur=start;
while( cur!=NULL)
{

cout<<"\nRoll number of student : "<<cur->roll_no;
cout<<"\nStudentname of student : "<<cur->name;
cout<<"\nMarks in Physics : "<<cur->p_m;
cout<<"\nMarks in Chemistry : "<<cur->c_m;
cout<<"\nMarks in Maths : "<<cur->m_m;
cout<<"\nMarks in English : "<<cur->e_m;
cout<<"\nMarks in Computer Science :"<<cur->cs_m;
cout<<"\nPercentage of student is :"<<cur->per;
cout<<"\nGrade of student is :"<<cur->grade;
cur=cur->nxt;
}
}

void student::show_tabular()
{

for(cur=start; cur!=NULL; cur=cur->nxt)

cout<<cur->roll_no<<setw(6)<<" "<<cur->name<<setw(10)<<cur->p_m<<setw(4)<<cur->c_m<<setw(4)<<cur->m_m<<setw(4)
<<cur->e_m<<setw(4)<<cur->cs_m<<setw(6)<<cur->per<<setw(6)<<" "<<cur->grade<<endl;
}

int student::retrollnumber()
{

while(cur->nxt!=NULL)
{ 
cur=cur->nxt;
}


return cur->roll_no;
}

void write_student();	
void display_all();	
void display_sp(int);	
void modify_student(int);	
void delete_student(int);	
void class_result();	
void result();	
void intro();	
void entry_menu(); 



int main()
{
char ch;
cout.setf(ios::fixed|ios::showpoint);
cout<<setprecision(2); 

intro();
do
{
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. RESULT MENU";
cout<<"\n\n\t02. ENTRY/EDIT MENU";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tPlease Select Your Option (1-3) ";
cin>>ch;

switch(ch)
{
case '1': result();
break;
case '2': entry_menu();
break;
case '3':
break;
default :cout<<"\a";
}
}while(ch!='3');
return 0;
}

void write_student()
{
node rec;
ofstream outFile;

outFile.open("student.txt",ios::binary);

if(start==NULL)
{
start=new node;
start->pre=NULL;


cout<<"\nroll number of student ";
cin>>start->roll_no;
cout<<"\n\nStudentname of student ";
cin>>start->name;
cout<<"\nmarks in physics out of 100 : ";
cin>>start->p_m;
cout<<"\nmarks in chemistry out of 100 : ";
cin>>start->c_m;
cout<<"\nmarks in maths out of 100 : ";
cin>>start->m_m;
cout<<"\nmarks in english out of 100 : ";
cin>>start->e_m;
cout<<"\nmarks in computer science out of 100 : ";
cin>>start->cs_m;

start->nxt=NULL;
}else
{
node *p;
cur=start;
while(cur->nxt!=NULL)

{ cur=cur->nxt; }
p=temp;
temp=new node;
temp->pre=p;

cout<<"\nroll number of student ";
cin>>start->roll_no;
cout<<"\n\nStudentname of student ";
cin>>start->name;
cout<<"\nmarks in physics out of 100 : ";
cin>>start->p_m;
cout<<"\nmarks in chemistry out of 100 : ";
cin>>start->c_m;
cout<<"\nmarks in maths out of 100 : ";
cin>>start->m_m;
cout<<"\nmarks in english out of 100 : ";
cin>>start->e_m;
cout<<"\nmarks in computer science out of 100 : ";
cin>>start->cs_m;
temp->nxt=NULL;
cur->nxt=temp;
}
outFile.write((char *) &rec, sizeof(rec));
outFile.close();
cout<<"\n\nStudent record Has Been Created ";
getch();
}

void display_all()
{
node rec;
ifstream inFile;
inFile.open("student.txt",ios::binary);
if(!inFile)
{
cout<<"File could not be open ! Press any Key...";
getch();
return;
}
cout<<"\n\n\n\t\tDISPLAY ALL RECORD !\n\n";
while(inFile.read((char *) &rec, sizeof(rec)))
{
cur=start;
while(cur->nxt!=NULL)
{


cout<<"\nRoll number of student : "<<cur->roll_no;
cout<<"\nStudentname of student : "<<cur->name;
cout<<"\nMarks in Physics : "<<cur->p_m;
cout<<"\nMarks in Chemistry : "<<cur->c_m;
cout<<"\nMarks in Maths : "<<cur->m_m;
cout<<"\nMarks in English : "<<cur->e_m;
cout<<"\nMarks in Computer Science :"<<cur->cs_m;
cout<<"\nPercentage of student is :"<<cur->per;
cout<<"\nGrade of student is :"<<cur->grade;


cur=cur->nxt;
}
cout<<"\n\n======\n";
}
inFile.close();
getch();
}

void display_sp(int n)
{
node rec;
student st;
ifstream inFile;
inFile.open("student.txt",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
getch();
return;
}

int flag=0;
cur=start;
while(inFile.read((char *) &rec, sizeof(rec)))
{

if( cur->roll_no==n) 

{



cout<<"\nRoll number of student : "<<cur->roll_no;
cout<<"\nStudentname of student : "<<cur->name;
cout<<"\nMarks in Physics : "<<cur->p_m;
cout<<"\nMarks in Chemistry : "<<cur->c_m;
cout<<"\nMarks in Maths : "<<cur->m_m;
cout<<"\nMarks in English : "<<cur->e_m;
cout<<"\nMarks in Computer Science :"<<cur->cs_m;
cout<<"\nPercentage of student is :"<<cur->per;
cout<<"\nGrade of student is :"<<cur->grade;

flag=1;
}
cur=cur->nxt;	}
inFile.close();
if(flag==0)
cout<<"\n\nrecord not exist";
getch();
}


void class_result()
{
node rec;
student st;
ifstream inFile;
inFile.open("student.txt",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
getch();
return;
}
cout<<"\n\n\t\tALL STUDENTS RESULT \n\n";
cout<<"==========================================================\n";
cout<<"R.No Studentname P C M E CS %age Grade"<<endl;
cout<<"==========================================================\n";


while(inFile.read((char *) &rec, sizeof(rec)))
{

for(cur=start; cur!=NULL; cur=cur->nxt)
{

cout<<cur->roll_no<<setw(6)<<" "<<cur->name<<setw(10)<<cur->p_m<<setw(4)<<cur->c_m<<setw(4)<<cur->m_m<<setw(4)
<<cur->e_m<<setw(4)<<cur->cs_m<<setw(6)<<cur->per<<setw(6)<<" "<<cur->grade<<endl;
}

}
getch();
inFile.close();
}


void result()
{

char ch;
int rno;
cout<<"\n\n\n\tRESULT MENU";
cout<<"\n\n\n\t1. Class Result";
cout<<"\n\n\t2. Student Report Card";
cout<<"\n\n\t3. Back to Main Menu";
cout<<"\n\n\n\tEnter Choice (1/2/3)? ";
cin>>ch;

switch(ch)
{
case '1' :class_result(); break;
case '2' :cout<<"\n\n\tEnter Roll Number Of Student : ";
cin>>rno;
display_sp(rno); break;
case '3' :break;
default :cout<<"\a";
}
}
void intro()
{

cout<<"\n\n\t\tSTUDENT REPORT CARD SYSTEM ";
getch();
}
void entry_menu()
{
int num;
char ch;
cout<<"\n\n\n\tENTRY MENU";
cout<<"\n\n\t1.ADD STUDENT RECORD";
cout<<"\n\n\t2.DISPLAY ALL RECORDS";
cout<<"\n\n\t3.SEARCH STUDENT RECORD ";
cout<<"\n\n\t4.EDIT STUDENT RECORD";
cout<<"\n\n\t5.DELETE STUDENT RECORD";
cout<<"\n\n\t6.BACK TO MAIN MENU";
cout<<"\n\n\tPlease Enter Your Choice (1-6) ";
cin>>ch;

switch(ch)
{
case '1':	write_student(); break;
case '2':	display_all(); break;
case '3':	cout<<"\n\n\tPlease roll number "; cin>>num;
display_sp(num); break;
/*	case '4':	cout<<"\n\n\tPlease roll number "; cin>>chose->num;
modify_student(chose->num);break;
case '5':	cout<<"\n\n\tPlease roll number "; cin>>num;
delete_student(num);break;*/
case '6':	break;
default:	cout<<"\a"; entry_menu();
}
}
Last edited on
You have a number of problems:

1) Line 36 42: percent is an uninitialized pointer. It contains garbage. You proceed to move data using this uninitialized pointer.

2) Line 49, 65, 71,216 59,79,90,216: cur is a global variable. This is a bad practice. It should be a local or member variable.

3) Line 117 141: You declare rec, then at line 168 196 you write it to outfile without ever moving anything to it. What exactly do you expect it to contain if you never move anything to it? Hint: It contains garbage.

4) You have memory leaks in your program. You allocate dynamic memory, but never release it.

5) Line 208,254 214,255: You read into rec, but never reference it.

6) It's a poor idea to be reading and writing structures (node) that contain pointers. They won't point to anything meaningful when you read them back in.


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.

Edit: Corrected line numbers to match OP's edited posted to add code tags.
Last edited on
tnx dear abstractionAnon,,, can you please sort out,,, i cant do anymore, i m beginner, so it is very diffcult to me...
i studied but didnt work
can you please sort out


I've told you where your problems are. I'm not going to do your homework for you.
You would not learn the material if I did.

Thank you for adding the code tags. I've edited my previous post so that my line numbers match your edited post.

Try and correct the issues I've pointed out and post your corrected code. Someone here will try and help as long as you keep trying.

Last edited on
OK dear.
you mean that my algorithms are correct.
i need to know, am i going on right way or not ?

if yes then i will do it
Are you on the right track, generally yes.
Does your program need a lot of work, again yes.

Some additional comments:

1) What is the purpose of your Student class? It has no variable members. Your members of node are attributes of a student and should be members of Student.

2) Line 28: When you instantiate a Student, you're nulling your global start pointer, meaning if you create a second student, you lose any elements already on your linked list.

3) You have several global functions would really seem to belong as function members of Student.

4) I'm wondering why you have struct node at all? You can implement a linked list using Student.
1
2
3
class Student
{  Student *  m_next;  // pointer to next student on list
...


5) There is a std::list container that manages a linked list for you, but I'm guessing you haven't covered that yet and are being asked to write your own linked list.
that is the problem , i have no idea how to implement a linkedlist using student ,,i told you im learning at home with the help of internet , that's why i am so confused, i search many programs linked list using class but i dont get their codes. i need simple programs to understand,
if you teach me little bit , it will be very helpful to me,
You can find a good description of linked-lists here:
http://en.wikipedia.org/wiki/Linked_list

1
2
3
4
5
6
7
8
  Student * listhead = NULL;  // An empty list 
  
  // Add a student
  Student * temp;
  temp = new Student;
  //  Fill in member variables
  temp->next = listhead;  // Point current entry to existing list
  listhead = temp;            // New student becomes front of list 




Topic archived. No new replies allowed.