Problem with fstream

So i have program, which makes a queue with a linked list. the problem is that, when i am inserting the information from the file, the program takes all information , and then makes one more insertion with incorrect data.
here's the 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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

class elem
{
public:
	long laiks;
	char darb;
	char kods[12];
	char nosauk[51];
	elem * next;
	
	elem(long,char,char [12],char [51]);

};
elem::elem(long llaiks,char ddarb, char kkods[12],char nnosauk[51])
{
	laiks=llaiks;
	darb=ddarb;
	strcpy(kods,kkods);
	strcpy(nosauk,nnosauk);
	next=NULL;
}
class queue{
public:
		queue();
		void print();
		bool isempty();
		void insert( long llaiks,char ddarb, char kkods[12],char nnosauk[51]);
		elem *first;
		elem *last;
};

void queue::insert( long laiks,char darb, char kods[12],char nosauk[51])
{
	elem *newptr=new elem(laiks,darb,kods,nosauk);
	
	if(isempty())
	{
		first=last=newptr;
	}
	else
	{
		last->next=newptr;
		last=newptr;
	}

}

bool queue::isempty()
{
	return (first==NULL);
}

queue::queue()
{
	first=last=NULL;
}


bool checkCode(elem *iev, elem *iev2)
{
	for(int i=0;i<51;i++)
	{
		if(iev->nosauk[i]!=iev2->nosauk[i]) return false;
	}

return true;
}


int main()
{
fstream fin("cd_maina.in",ios::in);
fstream fout("cd_maina.out",ios::out);

	long laiks;
	char darb;
	char kods[12];
	char nosauk[51];
	
	queue rinda;

	bool delete1=0;

	while(fin)
	{
		fin>>laiks;
		fin>>darb;
		fin>>kods;
		fin>>nosauk;

		rinda.insert(laiks,darb,kods,nosauk);
	}

	elem *glob=rinda.first;
	elem *cur;
	elem *cur2;

	while(glob!=NULL)
	{
		cur=glob;
		cur2=cur->next;
		delete1=0;

		while(cur2->next!=NULL)
		{

			if( ((cur->darb=='A' && cur2->darb=='B') || (cur->darb=='B' && cur2->darb=='A')) && checkCode(cur,cur2) )
			{
				if(cur->darb=='A')
				{
					fout<<cur2->laiks<<" ";
					fout<<cur->kods<<" ";
					fout<<cur2->kods<<endl;
				}
				else
				{
					fout<<cur2->laiks<<" ";
					fout<<cur2->kods<<" ";
					fout<<cur->kods<<endl;
				}

				if(cur==rinda.first)
				{
					rinda.first=cur->next;
					glob=rinda.first;
					delete cur;
					delete1=1;
				}
				else
				{
					elem *cur3=rinda.first;
					while(cur3->next!=cur)
					{
						cur3=cur3->next;
					}
					if(cur==glob) glob=glob->next;
					cur3->next=cur->next;
					delete cur;
					delete1=1;
				}
				
				if(cur2==rinda.last)
				{
					elem *cur4=rinda.first;
					while(cur4->next!=cur2)
					{
						cur4=cur4->next;
					}
					cur4->next=0;
					rinda.last=cur4;
					break;
				}
				else
				{
					elem *cur4=rinda.first;
					while(cur4->next!=cur2)
					{
						cur4=cur4->next;
					}
					cur4->next=cur2->next;
					delete cur2;
					break;
				}
			}

			cur2=cur2->next;
		}

		if(delete1==0) glob=glob->next;
	}

}


So the problems occurs in lines 88-96, everything else works normal.

so for example i have file with the following data:

10 B 01010112345 ABBA
15 A 02020213456 U2
16 A 03030314567 4you
17 B 02020213456 ABBA
20 B 04040415678 4you
127 B 04040415678 HipHop2011
1234 A 05050516789 ABBA

the program makes all elem units with this information, and adds another elem with laiks set to 1234, darb set to A, kods and nosauk variables are set to ""

is it the problem with fstream?
Last edited on
Topic archived. No new replies allowed.