Searching a text file and showing data spread over several lines

I was writing a simple program of railway reservation ( you may call it so )
when I chanced upon this problem. My program creates a file with all the details of the registration and arranges it in various lines. So, I want to add a function where the user enter the name of the ticket owner and complete details about the ticket are given out (from file 1). How should I do it ? I have given the code of the program as it looks till NOW.
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
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<fstream.h>
struct r{
char train[30];
char list[30];
char name[80][30];
int age[80];
char gen[80];
}pass;
int newres()
{
int n,male=0,female=0;
char bor[30];
char des[30];
char con='y';
char co;
float pri , tot;
ticket :
clrscr();
while(con=='y'||con=='Y')
{
male = 0;
female = 0;
cout<<"\n\t\t\t\tNew Reservation";
cout<<"\n\nEnter the name of the Train : ";
gets(pass.train);
cout<<"\n\nEnter the name of the list : ";
gets(pass.list);
cout<<"\n\nEnter the number of Passengers : ";
cin>>n;
cout<<"\n\nEnter Boarding Station : ";
gets(bor);
cout<<"Enter Destination : ";
gets(des);
for(int y = 0;y<n;y++)
{
cout<<"\n\nPassenger "<<y+1<<" : ";
cout<<"\n\n\n\nName : ";
gets(pass.name[y]);
cout<<"\n\nAge : ";
cin>>pass.age[y];
gn:
cout<<"\n\nGender : ";
cin>>pass.gen[y];
}
//check m / f
for(int g = 0;g<n;g++)
{
if(pass.gen[g]=='M'||pass.gen[g]=='m')
{
male++;
}
else
if(pass.gen[g]=='F'||pass.gen[g]=='f')
{
female++;
}
}
cout<<"\n\n\nEnter the price : ";
cin>>pri;
tot = (pri*n)+(pri+n)*0.12;
cout<<"\n\nPress enter to view the final ticket ...";
getch();
clrscr();
cout<<"\t\t\t\tRailways";
cout<<"\n\n\t"<<pass.train;
cout<<"\n"<<bor<<"\t----->\t"<<des;
cout<<"\n\nNAME\t\tGEN\tAGE"<<endl;
for(int t =0;t<n;t++)
{
cout<<pass.name[t]<<"\t"<<pass.gen[t]<<"\t"<<pass.age[t]<<endl;
}
cout<<"\n\nTotal : "<<pri<<" x "<<n<<" = "<<tot;
cout<<"\n\nConfirm ? [y/n] : ";
cin>>co;
switch(co)
{
case 'y' : break;
case 'n' : goto ticket;
default : cout<<"Enter a valid option ... ";
}
ofstream file_out(pass.train,ios::app);
if(file_out)
{
file_out<<"Ticket Owner : "<<pass.name[0];
file_out<<"\nBoarding Station : "<<bor;
file_out<<"\nDestination : "<<des;
file_out<<"\nPassengers : "<<n;
file_out<<"\nMale : "<<male<<"\tFemale : "<<female;
file_out<<"\nTotal Cost : "<<tot<<"\n\n\n\n";
file_out.close();
}
ofstream file2_out(pass.list,ios::app);
if(file2_out)
{
for(int p =0;p<n;p++)
{
file2_out<<pass.name[p]<<"\t"<<pass.age[p]<<"\t"<<pass.gen[p]<<"\t"<<bor<<"\t"<<des<<endl;
}
file2_out.close();
}
clrscr();
cout<<"Continue ? [y/n] : ";
cin>>con;
}
return 0;
}
void main()
{
int opt;
mn:
clrscr();
cout<<"\t\t\t\t RAILWAYS";
cout<<"\n\n\nPress \n1 for new ticket\n2 for ticket enquiry\n3 for help";
ot :
cout<<"\n\n\nYour Option : ";
cin>>opt;
switch(opt)
{
case 1: newres();
break;
case 2: cout<<"enquiry feature to be added";
break;
case 3: cout<<"help to be added";
break;
default: cout<<"Please select a valid option .";
goto op;
break;
}
//yet to design the program ending
cout<<"\n\n\n\Done !!!";
getch();
}
Are you an assembler programmer? Because you code reminds me of assembly, and that's not good!
First: use TAB, space and enter as often as possible!
I only added TABs and a few enters, and look how much better it looks already:
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
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<fstream>

using namespace std;

struct r{
	char train[30];
	char list[30];
	char name[80][30];
	int age[80];
	char gen[80];
}pass;


int newres ()
{
	int n,male=0,female=0;
	char bor[30];
	char des[30];
	char con='y';
	char co;
	float pri , tot;

	ticket :
	clrscr();

	while(con=='y'||con=='Y')
	{
		male = 0;
		female = 0;
		cout<<"\n\t\t\t\tNew Reservation";
		cout<<"\n\nEnter the name of the Train : ";
		gets(pass.train);
		cout<<"\n\nEnter the name of the list : ";
		gets(pass.list);
		cout<<"\n\nEnter the number of Passengers : ";
		cin>>n;
		cout<<"\n\nEnter Boarding Station : ";
		gets(bor);
		cout<<"Enter Destination : ";
		gets(des);

		for(int y = 0;y<n;y++)
		{
			cout<<"\n\nPassenger "<<y+1<<" : ";
			cout<<"\n\n\n\nName : ";
			gets(pass.name[y]);
			cout<<"\n\nAge : ";
			cin>>pass.age[y];
			gn:
			cout<<"\n\nGender : ";
			cin>>pass.gen[y];
		}

		//check m / f
		for(int g = 0;g<n;g++)
		{
			if(pass.gen[g]=='M'||pass.gen[g]=='m')
			{
				male++;

			}else if(pass.gen[g]=='F'||pass.gen[g]=='f')
			{
				female++;
			}
		}

		cout<<"\n\n\nEnter the price : ";
		cin>>pri;
		tot = (pri*n)+(pri+n)*0.12;
		cout<<"\n\nPress enter to view the final ticket ...";
		getch();
		clrscr();

		cout<<"\t\t\t\tRailways";
		cout<<"\n\n\t"<<pass.train;
		cout<<"\n"<<bor<<"\t----->\t"<<des;
		cout<<"\n\nNAME\t\tGEN\tAGE"<<endl;

		for(int t =0;t<n;t++)
		{
			cout<<pass.name[t]<<"\t"<<pass.gen[t]<<"\t"<<pass.age[t]<<endl;
		}

		cout<<"\n\nTotal : "<<pri<<" x "<<n<<" = "<<tot;
		cout<<"\n\nConfirm ? [y/n] : ";

		cin>>co;

		switch(co)
		{
			case 'y' : break;
			case 'n' : goto ticket;
			default : cout<<"Enter a valid option ... ";
		}

		ofstream file_out(pass.train,ios::app);

		if(file_out)
		{
			file_out<<"Ticket Owner : "<<pass.name[0];
			file_out<<"\nBoarding Station : "<<bor;
			file_out<<"\nDestination : "<<des;
			file_out<<"\nPassengers : "<<n;
			file_out<<"\nMale : "<<male<<"\tFemale : "<<female;
			file_out<<"\nTotal Cost : "<<tot<<"\n\n\n\n";
			file_out.close();
		}

		ofstream file2_out(pass.list,ios::app);

		if(file2_out)
		{
			for(int p =0;p<n;p++)
			{
				file2_out<<pass.name[p]<<"\t"<<pass.age[p]<<"\t"<<pass.gen[p]<<"\t"<<bor<<"\t"<<des<<endl;
			}
			file2_out.close();
		}

		clrscr();
		cout<<"Continue ? [y/n] : ";
		cin>>con;
	}

	return 0;
}
void main()
{
	int opt;

	mn:
	clrscr();
	cout<<"\t\t\t\t RAILWAYS";
	cout<<"\n\n\nPress \n1 for new ticket\n2 for ticket enquiry\n3 for help";
	ot :
	cout<<"\n\n\nYour Option : ";
	cin>>opt;
	switch(opt)
	{
		case 1: newres();
		break;
		case 2: cout<<"enquiry feature to be added";
		break;
		case 3: cout<<"help to be added";
		break;
		default: cout<<"Please select a valid option .";
		goto op;
		break;
	}
	//yet to design the program ending
	cout<<"\n\n\n\Done !!!";
	getch();
}


Second: use more comments!

Third: give your variables intuitive names! "pass" "newres" "n" "pre" "tot", these names aren't descriptive at all.

Fourth: Don't use 'Jumps', like "ot:" and "mn:", you don't need them in cpp, that's what functions and control statments are for!

It shouldn't be so difficult to understand such a simple program, but its almost incomprehensible to me. Try and make it prettier, and more "c++"ee, it reminds me of assembly too much...

As far as your question goes: You need to use an "ifstream", which is the opposite of "ofstream" - you can load files from memory with it. simply create an ifstream:
ifstream trainTicketFile(NAME OF FILE);
and display it using the "get()" function, and cout in a loop:
1
2
3
4
5
6
7
8
9
10
char c = ' '
bool exit = false;
while (!exit)
{
	c = trainTicketFile.get();
	cout << c;

	if (trainTicketFile.eof())
		exit = true;
}


Hope it helps :)
Last edited on
Thanks for the help. Sometimes I get lazy so I just skip those tabs and comments ( will put them from now on though). Actually I wanted to get the name from the user then look for it in the File 1 and give out the rest of the details. Thnx for the help though :)
You can either store the information as an array in a file set to use binary, or store it as text.

If you choose the second option, you need to come up with some basic parsing algorithm to retrive the data. It should be pretty easy to do, but I recommend the first option: just store it as binary, saves your time, effort, and processing power...

Once you have the array stored in memory, finding the user you want is just a matter of looping through the array.

Messing around with files and storage can actually be quite fun! :)
Last edited on
Topic archived. No new replies allowed.