-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.cpp
2054 lines (1859 loc) · 62.6 KB
/
new.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Inheritence working fine
//TODO: Get all quantities a check for which acc. value>0
//TODO: ADD date of journey to Ticket
//TODO: Add Admin option to edit fare // use slabs to do so
//TODO: Line 1617 PNR()
#include"sock_macro.cpp"
#include<time.h>
#include<math.h>
#define ignore cin.ignore(numeric_limits<streamsize>::max(),'\n')
//#include"cplug.cpp"
typedef unsigned int ui;
#define corr "Correct!"
class cplug
{
int ch; //*! int !! Test var.
char passkey[50];
// int sock;
int port;
int sendInt; //!Replace the ID,PASS,CHANGE to sendInt later
int connectRes;
string ipAddress;
int bytesRecieved;
char buf[4608];
string strRec;
sockaddr_in hint;
public:
int sock;
cplug() //Sorted
{
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1)
cerr<<"\033[1;31mCan't create socket\033[0m";
}
void init_hint_struct() //Sorted
{
port=54000;
ipAddress="127.0.0.1";
hint.sin_family=AF_INET;
hint.sin_port=htons(port);
inet_pton(AF_INET,ipAddress.c_str(),&hint.sin_addr);
}
int connect_to_server()
{
connectRes=connect(sock,(sockaddr*)&hint,sizeof(hint));
if(connectRes==-1)
{
cerr<<"\033[1;31mConnection failed\033[0m";
return -1;
}
}
int id_to_server(int ch)
{
int ID=1;
if(send(sock,(int*)&ID,sizeof(ID),0)==-1)
{
cerr<<"\033[1;31mCoudnot send data to server \033[0m";
return -1;
}
//*SEND
if(send(sock,(int*)&ch,sizeof(ch),0)==-1)
{
cerr<<"\033[1;31mCoudnot send data to server \033[0m";
return -1;
}
//*RECV
strcpy(buf,"0");
bytesRecieved=recv(sock,buf,sizeof(buf),0);
strRec=string(buf,bytesRecieved);
if(strcmp(strRec.c_str(),corr)==0)
return 1;
else
{
cout<<"\033[1;31mEnter correct ID\033[0m"<<endl;
sleep(1);
return -1;
}
return 1;
}
int pass_to_server(string str)
{
int PASS=2;
if(send(sock,(int*)&PASS,sizeof(PASS),0)==-1)
{
cerr<<"\033[1;31mCoudnot send data to server[0m";
return -1;
}
if(send(sock,str.c_str(),sizeof(str)+1,0)==-1)
{
cerr<<"\033[1;31mCoudnot send data to server[0m";
return -1;
}
strcpy(buf,"0"); //memset
bytesRecieved=recv(sock,passkey,sizeof(passkey),0);
// strRec=string(passkey,bytesRecieved);
if(strcmp(passkey,corr)==0)
{
cout<<"\033[1;32m Access Granted\033[0m"<<endl;
sleep(1);
}
else
{
cout<<"\033[1;31m Access Denied\033[0m"<<endl;
sleep(1);
return -1;
}
return 1;
}
int change_pass_req(string pass)
{
int CHANGE=3;
if(send(sock,(int *)&CHANGE,sizeof(CHANGE),0)==-1)
{
cout<<"\n \033Coudnot send data to server!! \033[0m";
sleep(1);
return -1;
}
if(send(sock,pass.c_str(),sizeof(pass),0)==-1)
{
cout<<"\n \033Could not send data to server!! \033[0m";
sleep(1);
return -1;
}
//? Checked till here, works fine
strcpy(buf,"0");
bytesRecieved=recv(sock,buf,sizeof(buf),0);
// strRec=string(buf,bytesRecieved);
if(strcmp(buf,corr)==0)
{
cout<<"\n \033[1;32m Password changed successfully!! \033[0m"<<endl;
sleep(1);
return 1;
}
else
{
cerr<<"\n \033[1;31m Error in changing password!! \033[0m"<<endl;
sleep(1);
return -1;
}
}
~cplug()
{
int exitsignal=100;
send(sock,(int *)&exitsignal,sizeof(exitsignal),0);
close(sock);
}
};
class rail : public cplug
{
ui tno;
char tname[16];
char tempname[16];
int noTrain;
char from[10];
char to[10];
ui SL,A3,A2,A1;
int mech; //Menu choice
int noStation;
int adminID;
int dist;
ui cost;
string adminPass;
char rStat[10];
fstream train_det;
int pnr;
int slf[10],a3f[10],a2f[10],a1f[10];
// int get_tno;
// unsigned int count=0;
void def_no_seats() //! Admin
{
do
{
system("clear");
cout<<"\n \033[36mNo. of seats must be less than 100\033[0m"<<endl;
cout<<"\n Enter no. of seats in Sleeper: "; cin>>SL;
cout<<"\n Enter no. of seats in 3 Tier AC: "; cin>>A3;
cout<<"\n Enter no. of seats in 2 Tier AC: "; cin>>A2;
cout<<"\n Enter no. of seats in 1 Tier AC: "; cin>>A1;
if(SL<0 || A3<0 || A2<0 || A1<0)
{
cerr<<"\n \033[1;31m How can no. of seats be negative!! Re-enter! \033[0m";
sleep(1);
}
if(SL>99 || A3>99 || A2>99 || A1>99)
{
cerr<<"\n \033[1;31mSeats must be less than 100!!! Re-enter! \033[0m";
sleep(1);
}
}while(SL<0 || A3<0 || A2<0 || A1<0 || SL>99 || A3>99 || A2>99 || A1>99);
print_to_stream();
}
void print_to_stream()
{
train_det.open("trainDetails.txt",ios::out|ios::binary|ios::app);
fcheck();
train_det<<tno<<" ";
train_det<<tname<<" ";
if(SL<10)
train_det<<"0"<<SL<<" ";
else
train_det<<SL<<" ";
if(A3<10)
train_det<<"0"<<A3<<" ";
else
train_det<<A3<<" ";
if(A2<10)
train_det<<"0"<<A2<<" ";
else
train_det<<A2<<" ";
if(A1<10)
train_det<<"0"<<A1<<" ";
else
train_det<<A1<<" ";
train_det.close();
}
int update_train_details() //*This & associated functions working good
{
int tellpoint;
int mech;
int search;
int flag=-1;
int tno;
int newSeat;
train_det.open("trainDetails.txt",ios::in|ios::binary);
fcheck();
// cin.ignore(numeric_limits<streamsize>::max(),'\n'); //uncomment it later
system("clear");
cout<<"\n Enter train no.: "; cin>>tno;
train_det.seekg(0);
for(int i=0;i<noTrain;i++)
{
train_det.seekg(i*35);
train_det>>search;
if(tno==search)
{
flag=i;
train_det.close();
break;
}
}
train_det.close();
if(flag==-1)
{
cerr<<"\n \033[1;31m Error! Train not found!\033[0m";
sleep(1);
return -1;
}
system("clear");
do
{
cout<<"\n What do you want to update? "<<endl;
cout<<" 1. Train No. "<<endl;
cout<<" 2. Train Name "<<endl;
cout<<" 3. No. of Seats SL "<<endl;
cout<<" 4. No. of Seats in 3A "<<endl;
cout<<" 5. No. of Seats in 2A "<<endl;
cout<<" 6. No. of seats in 1A "<<endl;
cout<<" 7. Train Route "<<endl;
cout<<"\033[1;32m 8. Return to ADMIN Menu \033[0m"<<endl;
cout<<" Enter your choice: "; cin>>mech;
switch(mech)
{
case 1: update_train_no(flag);
break;
case 2: update_train_name(flag);
break;
case 3: // 1
cout<<"\n Enter new no. of seats(<100): ";
cin>>newSeat;
update_no_seats(flag,1,newSeat);
break;
case 4: //2
cout<<"\n Enter new no. of seats(<100): ";
cin>>newSeat;
update_no_seats(flag,2,newSeat);
break;
case 5: //3
cout<<"\n Enter new no. of seats(<100): ";
cin>>newSeat;
update_no_seats(flag,3,newSeat);
break;
case 6: //4
cout<<"\n Enter new no. of seats(<100): ";
cin>>newSeat;
update_no_seats(flag,4,newSeat);
break;
case 7: update_train_route(tno);
break;
case 8: //Return back to ADMIN menu
return -1;
default: cerr<<"\n\033[1;31m Please select a correct choice! \033[0m";
}
}while(mech<1 || mech>8);
return 0;
}
int update_train_no(int flag)
{
string B,C;
train_det.open("trainDetails.txt",ios::in|ios::binary);
fcheck();
train_det.seekg(35*flag);
char uptno[6];
cout<<" Enter updated train no.: ";
cin>>uptno;
char A[35*flag];
train_det.seekg(0);
train_det.getline(A,(35*flag));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(35*flag + 6);
getline(train_det,C);
train_det.close();
// cout<<A<<" "<<uptno<<" "<<C;
train_det.open("trainDetails.txt",ios::out|ios::binary);
train_det<<A<<" "<<uptno<<" "<<C;
return 0;
}
void update_train_name(int trainIndex)
{
int currSeek=trainIndex*35;
cout<<" Enter (new) train name: ";
ignore;
cin.getline(tname,16);
castToSize(tname,16);
char A[currSeek+6];
string C;
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(0);
train_det.getline(A,(currSeek+6));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+23);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
train_det.seekg(0);
train_det<<A<<" "<<tname<<" "<<C;
}
void update_no_seats(int trainIndex, int coach, int upSeat) //For direct update
{
int currSeek=trainIndex*35;
int noSeat;
char X1[currSeek+23];
char X2[currSeek+26];
char X3[currSeek+29];
char X4[currSeek+32];
string C;
switch(coach)
{
case 1: //SL
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+23);
train_det>>noSeat;
noSeat=upSeat;
train_det.seekg(0);
train_det.getline(X1,(currSeek+23));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+23+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X1<<" 0"<<noSeat<<" "<<C;
else
train_det<<X1<<" "<<noSeat<<" "<<C;
train_det.close();
break;
case 2: //3A
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+26);
train_det>>noSeat;
noSeat=upSeat;
train_det.seekg(0);
train_det.getline(X2,(currSeek+26));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+26+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X2<<" 0"<<noSeat<<" "<<C;
else
train_det<<X2<<" "<<noSeat<<" "<<C;
train_det.close();
break;
case 3: //2A
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+29);
train_det>>noSeat;
noSeat=upSeat;
train_det.seekg(0);
train_det.getline(X3,(currSeek+29));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+29+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X3<<" 0"<<noSeat<<" "<<C;
else
train_det<<X3<<" "<<noSeat<<" "<<C;
train_det.close();
break;
case 4: //1A
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+32);
train_det>>noSeat;
noSeat=upSeat;
train_det.seekg(0);
train_det.getline(X4,(currSeek+32));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+32+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X4<<" 0"<<noSeat<<" "<<C;
else
train_det<<X4<<" "<<noSeat<<" "<<C;
train_det.close();
break;
default: cerr<<"\n \033[1;31mCan't Happen! Contact DEV! \033[0m";
exit(0);
}
}
void update_no_seats(int trainIndex,int coach) //For ticket booking update
{
int currSeek=trainIndex*35;
int noSeat;
char X1[currSeek+23];
char X2[currSeek+26];
char X3[currSeek+29];
char X4[currSeek+32];
string C;
switch(coach)
{
case 1: //SL
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+23);
train_det>>noSeat;
noSeat--;
train_det.seekg(0);
train_det.getline(X1,(currSeek+23));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+23+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X1<<" 0"<<noSeat<<" "<<C;
else
train_det<<X1<<" "<<noSeat<<" "<<C;
train_det.close();
break;
case 2: //3A
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+26);
train_det>>noSeat;
noSeat--;
train_det.seekg(0);
train_det.getline(X2,(currSeek+26));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+26+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X2<<" 0"<<noSeat<<" "<<C;
else
train_det<<X2<<" "<<noSeat<<" "<<C;
train_det.close();
break;
case 3: //2A
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+29);
train_det>>noSeat;
noSeat--;
train_det.seekg(0);
train_det.getline(X3,(currSeek+29));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+29+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X3<<" 0"<<noSeat<<" "<<C;
else
train_det<<X3<<" "<<noSeat<<" "<<C;
train_det.close();
break;
case 4: //1A
train_det.open("trainDetails.txt",ios::in|ios::binary);
if(!train_det.is_open()); //!ADD Statement
train_det.seekg(currSeek+32);
train_det>>noSeat;
noSeat--;
train_det.seekg(0);
train_det.getline(X4,(currSeek+32));
train_det.close();
train_det.open("trainDetails.txt",ios::in|ios::binary);
train_det.seekg(currSeek+32+3);
getline(train_det,C);
train_det.close();
train_det.open("trainDetails.txt",ios::out|ios::binary);
if(noSeat<10)
train_det<<X4<<" 0"<<noSeat<<" "<<C;
else
train_det<<X4<<" "<<noSeat<<" "<<C;
train_det.close();
break;
default: cerr<<"\n \033[1;31mCan't Happen! Contact DEV! \033[0m";
exit(0);
}
}
void update_train_route(int tno)
{
ifstream troute;
ofstream tout;
int location,nost;
string search;
troute.open("distance.txt",ios::binary);
while(!troute.eof())
{
troute>>search;
if(search==to_string(tno))
break;
}
location=troute.tellg();
troute>>nost;
location++;
char A[location];
troute.seekg(0);
troute.getline(A,location);
troute.close();
troute.open("distance.txt",ios::binary);
troute.seekg(location+3+(15*nost));
string C;
getline(troute,C);
troute.close();
tout.open("distance.txt",ios::binary);
tout<<A<<" ";
tout.close();
cout<<"\n Enter no. of stations(including SRC and DEST): "; cin>>noStation;
train_det.open("distance.txt",ios::out|ios::binary|ios::app);
if(noStation<10)
train_det<<"0"<<noStation<<" ";
else if(noStation<100)
train_det<<noStation<<" ";
else
{
cerr<<"\033[1;31m Itni lambi train kon chalata hai bhai! \033[0m"<<endl;
exit(-1);
}
train_det.close();
for(int i=0;i<noStation;i++)
{
ignore;
cout<<"\n Enter station name "<<i+1<<": ";
cin.getline(rStat,10);
for(int j=0;rStat[j]!='\0';j++)
rStat[j]=toupper(rStat[j]);
castToSize(rStat,10);
train_det.open("distance.txt",ios::out|ios::app|ios::binary);
train_det<<rStat<<" ";
train_det.close();
cout<<" Enter distance from source: ";
cin>>dist;
dist=abs(dist);
train_det.open("distance.txt",ios::out|ios::app|ios::binary);
if(dist<10)
train_det<<"00"<<dist<<" ";
else if(dist<100)
train_det<<"0"<<dist<<" ";
else if(dist<1000)
train_det<<dist<<" ";
else
cerr<<"\033[1;31m Large distance error xD \033[0m"<<endl;
train_det.close();
}
train_det.open("distance.txt",ios::out|ios::app|ios::binary);
train_det<<C;
train_det.close(); //*/
}
void gen_PNR()
{
srand(time(0));
pnr=rand()%900000+100000;
}
void castToSize(char str[],int size)
{
for(int i=0;i<strlen(str);i++)
{
if(str[i]==' ')
str[i]='0';
}
for(int i=strlen(str);i<size;i++)
str[i]='0';
str[size]='\0';
}
int seatCheck(int tno,int coach)
{
ifstream fin;
fin.open("trainDetails.txt",ios::binary);
string stno;
while(!fin.eof())
{
fin>>stno;
if(stno==to_string(tno))
break;
}
int location;
location=fin.tellg();
location+=18;
int noSeat;
switch(coach)
{
case 1: fin.seekg(location);
fin>>noSeat;
break;
case 2: fin.seekg(location+3);
fin>>noSeat;
break;
case 3: fin.seekg(location+3+3);
fin>>noSeat;
break;
case 4: fin.seekg(location+3+3+3);
fin>>noSeat;
break;
default: cout<<"\033[1;31m Lite \033[0m";
}
fin.close();
return noSeat;
}
int delete_train()
{
system("clear");
char choice;
cout<<"Enter train no. whose details you want to wipe out: ";
cin>>tno;
do
{
cout<<"Are you sure you want to wipe details of "<<tno<<"? (y/n) ";
cin>>choice;
choice=toupper(choice);
if(choice=='Y' || choice=='N')
break;
else
{
cerr<<" \033[;131mEnter correct choice\033[0m";
system("clear");
}
}while(choice!='Y' || choice!='N');
if(choice=='Y')
{
ifstream fin;
fin.open("trainDetails.txt",ios::binary);
string stno;
while(!fin.eof())
{
fin>>stno;
if(stno==to_string(tno))
break;
}
int location;
location=fin.tellg();
location-=5;
char A[location];
fin.seekg(0);
fin.getline(A,location);
fin.close();
fin.open("trainDetails.txt",ios::binary);
fin.seekg(location+35);
string C;
getline(fin,C);
fin.close();
ofstream fout;
fout.open("trainDetails.txt",ios::binary);
fout<<A<<" "<<C;
fout.close();
}
return 0;
}
public:
rail()
{
string whole;
int endpoint;
train_det.open("trainDetails.txt",ios::in|ios::binary);
getline(train_det,whole);
train_det.close();
endpoint=strlen(whole.c_str());
noTrain=endpoint/35;
SL=A3=A2=A1=0;
// * Init fare
ifstream fare;
fare.open("fare.txt",ios::binary);
int i=0;
//* Get SL fare
while(!fare.eof())
{
fare>>slf[i];
i++;
if(i==10)
break;
}
i=0;
//* Get 3A fare
while(!fare.eof())
{
fare>>a3f[i];
i++;
if(i==10)
break;
}
i=0;
//* Get 2A fare
while(!fare.eof())
{
fare>>a2f[i];
i++;
if(i==10)
break;
}
i=0;
//* Get 1A fare
while(!fare.eof())
{
fare>>a1f[i];
i++;
if(i==10)
break;
}
}
int update_fare()
{
int uFare,ch,slab;
do
{
system("clear");
cout<<" Which class' fare do you wish to update? "<<endl;
cout<<" 1. SL\n 2. 3A\n 3. 2A\n 4. 1A "<<endl;
cout<<" Enter your choice: "; cin>>ch;
if(ch<1 || ch>4)
{
cerr<<"\033 [1;31m No such coach option \033[0m";
sleep(1);
}
}while(ch<1 || ch>4);
system("clear");
cout<<" Every 100 km accounts for a slab. "<<endl;;
cout<<" \033[1;32mSlab 1:\033[0m 0-99km\n \033[1;32mSlab 2:\033[0m 100-199km \n \033[1;32mSlab 3:\033[0m 200-299km \n .\n . \n . \n\033[1;32m Slab 10:\033[0m 1000 km"<<endl;
cout<<"\n Enter the slab no. whose fare you wish to change: ";
cin>>slab;
if(slab<1 || slab>10)
{
cout<<"\033[1;31m No such slab \033[0m";
return -1;
}
slab--;
fstream fare,fout;
string C;
int location;
//TODO: stack dump.exe created don't know why! Check data type conversions
//TODO: Check step by step8
switch(ch)
{
case 1://SL
cout<<" Current Fare per seat: "<<slf[slab]<<endl;
cout<<" Enter updated fare per seat: ";
cin>>uFare;
uFare=abs(uFare);
slf[slab]=uFare;
break;
case 2://3A
cout<<" Current Fare per seat: "<<a3f[slab]<<endl;
cout<<" Enter updated fare per seat: ";
cin>>uFare;
uFare=abs(uFare);
a3f[slab]=uFare;
break;
case 3://2A
cout<<" Current Fare per seat: "<<a2f[slab]<<endl;
cout<<" Enter updated fare per seat: ";
cin>>uFare;
uFare=abs(uFare);
a2f[slab]=uFare;
break;
case 4://1A
cout<<" Current Fare per seat: "<<a1f[slab]<<endl;
cout<<" Enter updated fare per seat: ";
cin>>uFare;
uFare=abs(uFare);
a1f[slab]=uFare;
break;
}
fare.open("fare.txt",ios::in|ios::binary);
location=((ch-1)*50)+(slab*5);
char A[location];
fare.getline(A,location);
fare.close();
fare.open("fare.txt",ios::in|ios::binary);
fare.seekg(location+5);
getline(fare,C);
fare.close();
fout.open("fare.txt",ios::out|ios::binary);
if(uFare<1000)
fout<<A<<" 0"<<uFare<<" "<<C;
else if(uFare<10000)
fout<<A<<" "<<uFare<<" "<<C;
fout.close();
cout<<"\n\033[1;32m Updated! \033[0m"<<endl;
// */
return 0;
}
void print()
{
cout<<"SL\t3A\t2A\t1A"<<endl;
for(int i=0;i<10;i++)
{
cout<<slf[i]<<"\t"<<a3f[i]<<"\t"<<a2f[i]<<"\t"<<a1f[i]<<endl;
}
}
void def_train_route(int tno) //! Admin
{
train_det.open("distance.txt",ios::out|ios::binary|ios::app);
train_det<<tno<<" ";
train_det.close();
cout<<"\n Enter no. of stations(including SRC and DEST): "; cin>>noStation;
train_det.open("distance.txt",ios::out|ios::binary|ios::app);
if(noStation<10)
train_det<<"0"<<noStation<<" ";
else if(noStation<100)
train_det<<noStation<<" ";
else
cerr<<"\033[1;31m Itni lambi train kon chalata hai bhai! \033[0m"<<endl;
train_det.close();
for(int i=0;i<noStation;i++)
{
ignore;
cout<<"\n Enter station name "<<i+1<<": ";
cin.getline(rStat,10);
for(int j=0;rStat[j]!='\0';j++)
rStat[j]=toupper(rStat[j]);
castToSize(rStat,10);
train_det.open("distance.txt",ios::out|ios::app|ios::binary);
train_det<<rStat<<" ";
train_det.close();
cout<<" Enter distance from source: ";
cin>>dist;
dist=abs(dist);
train_det.open("distance.txt",ios::out|ios::app|ios::binary);
if(dist<10)
train_det<<"00"<<dist<<" ";
else if(dist<100)
train_det<<"0"<<dist<<" ";
else if(dist<1000)
train_det<<dist<<" ";
else
cerr<<"\033[1;31m Large distance error xD \033[0m"<<endl;
train_det.close();
}
}
int change_pass()
{
string currpass;
int passvar;
int flag=1;
ignore;
do
{
cout<<"\n Enter current password: "; getline(cin,currpass);
passvar=pass_to_server(currpass);
if(passvar==1)
{
string newpass;
char confpass[8];
do
{
do
{
system("clear");
cout<<"\n Enter new password(max. 7 char.): "; getline(cin,newpass);
if(strlen(newpass.c_str())>7)
{