Create and Append Data into a New File Based&From Two Data Files.

Pages: 12
Hello! I have a project that is due tonight and I am at the last step.. However, everything I tried has not worked so far.



What am I trying to Achieve?
Well, I have two .txt files.. namely candidates.txt and merit.txt which are manipulated through other functions. And what I am trying to do is read merit line by line, storing every word in each structure string, compare it's candidate's respective attributes and store it in a new file depending on the type of merit list generated which is also compared with both files.

Candidates.txt is in the following format;
1
2
3
4
5
FormNo<TAB>First Name<TAB>Last Name<TAB>Category<TAB>Group<TAB>Marks<NEW_LINE>
10028<TAB>Muhammad Zubair<TAB>Muhammad Abdullah<TAB>engg<TAB>army<TAB>1010<NEW_LINE>
10044<TAB>Muhammad Abdullah<TAB>Abdul Rehman<TAB>engg<TAB>army<TAB>898<NEW_LINE>
10268<TAB>Usama Khan<TAB>Saad ur Rehman<TAB>cs<TAB>civ<TAB>812<NEW_LINE>
10302<TAB>Muhammad Kamran<TAB>Waris Khan<TAB>cs<TAB>civ<TAB>937<NEW_LINE>


While merit.txt is in the following;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Session<TAB>Listno<TAB>Grade<TAB>OM<TAB>Army<TAB>DEF/Gov<NEW_LINE>
2019<TAB>1<TAB>med<TAB>1000<TAB>950<TAB>960<NEW_LINE>
2019<TAB>1<TAB>engg<TAB>990<TAB>940<TAB>950<NEW_LINE>
2019<TAB>1<TAB>cs<TAB>980<TAB>930<TAB>940<NEW_LINE>
2019<TAB>1<TAB>econ<TAB>970<TAB>920<TAB>930<NEW_LINE>
2019<TAB>1<TAB>stats<TAB>960<TAB>910<TAB>920<NEW_LINE>
2019<TAB>2<TAB>med<TAB>940<TAB>890<TAB>900<NEW_LINE>
2019<TAB>2<TAB>engg<TAB>930<TAB>880<TAB>890<NEW_LINE>
2019<TAB>2<TAB>cs<TAB>920<TAB>870<TAB>880<NEW_LINE>
2019<TAB>2<TAB>econ<TAB>910<TAB>860<TAB>870<NEW_LINE>
2019<TAB>2<TAB>stats<TAB>900<TAB>850<TAB>860<NEW_LINE>
2019<TAB>3<TAB>med<TAB>880<TAB>830<TAB>840<NEW_LINE>
2019<TAB>3<TAB>engg<TAB>870<TAB>820<TAB>830<NEW_LINE>
2019<TAB>3<TAB>cs<TAB>860<TAB>810<TAB>820<NEW_LINE>
2019<TAB>3<TAB>econ<TAB>850<TAB>800<TAB>810<NEW_LINE>
2019<TAB>3<TAB>stats<TAB>840<TAB>790<TAB>800<NEW_LINE>


Here is my code so far
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
menu_generate()
{	
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
	
    start_generate:
	system("cls");
	struct name
    	{
    		string namef;
    		string namel;
	};
	struct infof
	{
		int marks;
		string category;
		string group;
	};
    	struct form
    	{
    		int formno;
    		int session;
    		name personal;
    		infof data;
    	};
    	
    	struct grade
    	{
    		int om;
    		int army;
    		int def;
	};
    	struct infom
    	{
    		int listno;
    		grade quota;
    		string group;
	};
    	struct list
    	{
    		int session;
    		infom data;
	};
	
	struct infoi
	{
		int listno;
		string quota;
		string group;
	};
	struct input
	{
		int session;
		infoi category;
	};
	form student;
	list merit;
	input generate;
    	string col[2], in, record;
	char cmd;
	system("cls"); 
	
	cout<<"\n\t\t Generate"<<endl;
	cout<<"_________________________________________"<<endl;
	cout<<"Back\t\t\t\t\tX\n"<<endl;
	cout<<"\n  Hint: The Capital Letters Represents\n\tthe Input Commands."<<endl;
	cout<<"\n\tSession: ";
	getline(cin,in);
	if(in=="b")
	{
		system("cls");
		return(0);
	}
	if(in=="x")
	{
		system("cls");
		exit(0);
	}
	generate.session=stoi(in);
	if(!((generate.session>=0)&&(generate.session<=9999)))
	{
		cout<<"\t    ERROR! Session out of bound.";
		getch();
		system("cls");
		goto start_generate;
	}
	cout<<"\tMerit List: ";
	getline(cin,in);
	if(in=="b")
	{
		system("cls");
		return(0);
	}
	if(in=="x")
	{
		system("cls");
		exit(0);
	}
	generate.category.listno=stoi(in);
	if(!((generate.category.listno>=1)&&(generate.category.listno<=3)))
	{
		cout<<"\t    ERROR! MeritList out of bound.";
		getch();
		system("cls");
		goto start_generate;
	}
	cout<<"\n\t    Quota: ";
	getline(cin,in);
	if(in=="b")
	{
		system("cls");
		return(0);
	}
	if(in=="x")
	{
		system("cls");
		exit(0);
	}
	generate.category.quota=in;
	if(!((generate.category.quota=="civ")||(generate.category.quota=="om")||(generate.category.quota=="army")||(generate.category.quota=="def")||(generate.category.quota=="gov")))
	{
		cout<<"\t    ERROR! Quota out of bound.";
		getch();
		system("cls");
		goto start_generate;
	}
	cout<<"\t    Group: ";
	getline(cin,in);
	if(in=="b")
	{
		system("cls");
		return(0);
	}
	if(in=="x")
	{
		system("cls");
		exit(0);
	}
	generate.category.group=in;
	if(!((generate.category.group=="cs")||(generate.category.group=="econ")||(generate.category.group=="stats")||(generate.category.group=="engg")||(generate.category.group=="med")))
	{
		cout<<"\t    ERROR! Group out of bound.";
		getch();
		system("cls");
		goto start_generate;
	}
	
	cout<<"\n\t Are you Sure? (y/n): ";
	in=getche();
	if(!(in=="y"))
		goto start_generate;
	else
	{
		ifstream fetch_candidate("candidates.txt",ios::in);
		if(!fetch_candidate)
		{
			cout<<"\tERROR! \'candidates.txt\' Not Found."<<endl;
			getch();
			system("cls");
			return(0);
		}
		else
		{
			while(fetch_candidate>>student.formno)
			{
        			getline(fetch_candidate>>ws,student.personal.namef,'\t');
        			getline(fetch_candidate>>ws,student.personal.namel,'\t');
        		
        			fetch_candidate>>student.data.category
            				>>student.data.group
            				>>student.data.marks
            				>>student.session;
            			ifstream fetch_merit("merit.txt",ios::in);
            			if(!fetch_merit)
            			{
            				cout<<"\n\t    ERROR! \'merit.txt\' Not Found.";
					getch();
					system("cls");
					return(0);
				}
				else
				{
					while(fetch_merit>>merit.session
            					>>merit.data.listno
            					>>merit.data.group
            					>>merit.data.quota.om
            					>>merit.data.quota.army
            					>>merit.data.quota.def)
					{
            					if((generate.session==merit.session)&&(generate.session==student.session)&&(student.session==merit.session))
							if(generate.category.listno==merit.data.listno)
            							if((generate.category.group==merit.data.group)&&(generate.category.group==student.data.group)&&(student.data.group==merit.data.group))
            							{
            								if((((generate.category.quota=="civ")||(generate.category.quota=="om"))&&(student.data.category=="civ"))&&(student.data.marks>=merit.data.quota.om))
									{
										ofstream list_om("LISTOM.txt",ios::app);
										list_om<<student.formno<<'\t'
										<<student.personal.namef<<'\t'
										<<student.personal.namel<<'\t'
										<<student.data.marks<<'\t'
										<<student.data.group<<'\t'
										<<student.data.category<<'\t'
										<<student.session<<'\n';
									}
									if(((generate.category.quota=="def")||(generate.category.quota=="gov")&&(student.data.category=="def")||(student.data.category=="gov"))&&(student.data.marks>=merit.data.quota.def))
									{
										ofstream list_def("LISTDefGov.txt",ios::app);
										list_def<<student.formno<<'\t'
										<<student.personal.namef<<'\t'
										<<student.personal.namel<<'\t'
										<<student.data.marks<<'\t'
										<<student.data.group<<'\t'
										<<student.data.category<<'\t'
										<<student.session<<'\n';
									}
									if(((generate.category.quota=="army")&&(student.data.category=="army"))&&(student.data.marks>=merit.data.quota.army))
									{
										ofstream list_army("LISTArmy.txt",ios::app);
										list_army<<student.formno<<'\t'
										<<student.personal.namef<<'\t'
										<<student.personal.namel<<'\t'
										<<student.data.marks<<'\t'
										<<student.data.group<<'\t'
										<<student.data.category<<'\t'
										<<student.session<<'\n';
									}
								}
					}
				}
			}
		}
	}
	cout<<"\n\tMerit List Successfully Generated!";
        getch();
       	goto start_generate;
}




But, I have no idea why... The function takes input and as soon as it moves on list generation, it exits. by exit, i mean the program ends even though i put a confirmation and a getch(); command.
P.s; Classes&Vectors are NOT Allowed.
Last edited on
Yeah, i'm basically tired now.. That's why i didn't try more than just that in the end where List Generation (File Creation & Data Append) works.
The project is due by midnight and my group members didn't even write a single word of code. So, they're useless and i've been staying up until 7 Am and waking at 1 Pm daily working on this project alone and my mind is fed up..

Maybe i should read candidate.txt first and then read merit? i'm not sure anymore... Please help. :(

EDIT Another theory of the error cause is that the ofstreams to create and append a new list is failing to do so... But still, why would it skip getch(); and confirmation cout afterwards? it should execute those before exiting.. and even before that, it should return to the previous function Home and not just straight-up exit. :/
Last edited on
I think this is the error..
When the program straight-up exists, it prints the following message;

 
Process exited after 11.73 seconds with return value 3221225477



Whereas normal return value is;

1
2
Process exited after 16.66 seconds with return value 0
Press any key to continue . . .
Last edited on
You need to post the full text of the assignment. Your description doesn't give enough information about what you're trying to do. Remember, we aren't in your class. We don't even know what school you're going to. We have no idea what the assignment is.

Consider it from my perspective:
Well, I have two .txt files.. namely candidates.txt and merit.txt 
So far, so good!

which are manipulated through other functions. 
Wait. Manipulated how? Does that mean you start with some manipulated data? Now the starting conditions are unclear.

And what I am trying to do is read merit line by line
Okay, so I guess the whole "manipulated by other functions" thing doesn't matter?

storing every word in each structure string
Merit.txt contains mostly numbers, not words. So are you only storing the words? What happens to the number? And what are these "structure strings" your talking about?

compare it's candidate's respective attributes
Hmm. So a merit has a candidate? Which merit's correspond to which candidates? And "respective attributes?" Does that mean merits are related to candidate attributes? How? None of the column headers in the candidates file talks about attributes. In fact, none of the column headers in candidates matches any of the column headings in merits. So if there's a relationship between them, it's not apparent.

and store it in a new file depending on the type of merit list generated
So... you generate a merit list somehow? And that list has a type? Is that "econ", "stats", "med", etc? Then why is the heading "OM"?

 which is also compared with both files.
So the output is compared with the input files? or something else? What's compared? How is it compared? What's the purpose of the comparison?
One of the functions simply adds a new line of merit in the same format as the file.. and doesn't delete or show anything from the file.
The other, add a single line at a time that includes the data of a candidate into the candidates.txt file

Take it as one function is trying to transfer data to another function through a file and then compares them.. whichever lines have similar words in the file, it simply appends it into a new file.

By structure string i meant the structure of strings above.. which, is surprising long. xD
It simply makes it easier to identify what value the variable holds by it's name.

And by Attributes, i mean.. in the last step of list generation, the program needs to see if the merit requested to generate exists in both the files.. it checks both files line by line,
for example.. if i input value of generate.session = 2019, generate.listno = 1 and group = "cs" (all the previous values will check which line of merit.txt matches these values and stores them in merit.session, merit.listno, etc..)
while quota = om means it does not care if the candidate.category is civ, gov, army, or def. it simply appends it into a new file if the candidate.session, candidate.group match..
But the next ofstreams also check if candidate.category is the same as required list to be generated such as army and only appends the data of the candidates if their candidate.category=="army"

That is how it compares the values of both files.

Also, i added the error, do you have any idea what it's about?
Last edited on
Now looking over your code try to make sense of this:

Line 172 reads a "session" column from the candidates file, but the sample file you posted doesn't have that column.

What's line 184 supposed to be doing? It reads a single word from the merit file.

And then lines 186-191 look like they're reading the merit data, but they're reading from the candidate file.
Sorry, i updated the file and forgot to update the file sample here..
But yes, there is session column in candidate.txt

line 184 means 'as long as it can read a line' ???? I'm not sure, but that's what our teacher told us. however, it worked when i printed the entire candidate.txt file in another funtion?

yes, 186-191 is my mistake, it should be fetch_merit and not fetch_candidate.
lemme try rephrasing my objective...

What i'm trying to do is;
1. take user input of what session, listno, group and quota does the user want to generate the merit list of? all these values are stored in generate structure.

2.1 then the program needs to read the header/column titles of the file into col[1] //this is just to eliminate the header in the calculation.
2.2 it is supposed to read the first line and store all of the words into separate variables within merit structure.

3.1 it again needs to read the first header/column title into col[2] just to eliminate it.
3.2 after that, now comes the tricky part.. It needs to read each line of the candidate.txt file and store it into candidate structure.
4.1 and everytime it reads one line, it should compare if merit.session==candidate.session if yes, then it needs to check if merit.group==candidate.group.
once these two are out of the way, it checks who the candidate belongs to, armed forces, civilian, gov employee, etc... regardless of these, the ofstream list_om (stands for open merit) should ignore the category and append the data of the candidate into a new file.

4.2 whereas the upcoming ofstream list_army checks if candidate.category=="army" and only appends those who have it..
4.3 same with ofstream list_def, the only difference is that it checks candidate.category=="gov"||"def"


Note that all ofstreams need to create different files and append data int their respective ones..
Last edited on
._.
Maybe one of the files isn't opening and you don't see the error message because you're clearing the screen. You've forgotten to put getch() calls before those system("cls") calls.
Last edited on
WEll, true.. but they should be found anyway because the filenames are correct. :/
Well, okay, but the error you describe in your first post sounds like it's not finding one of the files.
i rewrote that most as diligently as i could.. xD
that seems to be fixed, now the issue is at reading the merit file which as numbers.
can you send me a Private Message Request?
Forum comments take a while to read..

If we work it out sooner, I'll post the solution here and besides, I don't have much time left. ._.
Last edited on
I don't like PMs. And I'm going to be busy anyway.
But I notice that you aren't reading from the merit.txt file properly.
Get rid of the while (fetch_merit >> record) part (which makes no sense)
and replace it with

1
2
3
4
5
6
        while (fetch_merit >> merit.session
                >> merit.data.listno
                >> merit.data.group
                >> merit.data.quota.om
                >> merit.data.quota.army
                >> merit.data.quota.def)

(Obviously you would get rid of the similar code below that, too.)
You may also have your goto in the wrong place at the end, so it may only read the first record.
yeah, but checking the forums take a moment..
okay, lemme try..
Do you have discord? 🥺
Last edited on
Okay, i have replaced the ofstreams with a cout<<"hello world!"; so it seems to go there now.
Hey! IT worked!

Well, most of it.. It is still ignoring the fact that the student.category also needs to be the same as generate.category

I updated the code in the question.
if you're still there, WE'RE HOMESTRAIGHT! :D
Updated Once more!

I added more conditions in the final If statements before ofstreams...
There is only one tiny problem now, it somehow, however.. always gives 1 List of Def/Gov Category regardless if i don't even ask it to do that. :/

I could put all final conditions into their else statements..?
EDIT Nah it's still doing that even after putting them into their else statements.
Last edited on
Pages: 12