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 :)