TRAIN TICKET CREATOR IN C++

Today in this tutorial we will learn how to generate an Indian Railways Reservation Ticket using the C++ programming language. We will create the ticket using C++ graphics.h library and generate an exact replica of Indian Railways Ticket.

So if you are looking for solutions to generate a ticket in your project, you are in the right place. Because here you can learn and get the code for the same.

ABOUT GRAPHICS LIBRARY

The graphics library of C++ contains many functions. But few of these are as follows –

  1. creation of a good graphical user interface
  2. creation of user-friendly output screen

Because graphics library functions are much vast, we list some of the functions used in the code snippet –

  1. initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver) – It initializes the graphical mode. So to start the graphics system, you must first call initgraph.
  2. setcolor(int color) – It sets the drawing color, which ranges from 0 to getmaxcolor.
  3. rectangle(int left, int top, int right, int bottom) – It draws a rectangle with current attributes.
  4. circle(int x, int y, int r) – It draws a circle with (x,y) as center and radius r. But the radius must be an integer.
  5. setfillstyle(int pattern, int color) – It sets the fill color and pattern.
  6. floodfill(int x, int y, int border) – It fills an area having a particular border color. But the area must be enclosed and (x,y) position must be inside that area.
  7. settextstyle(int font, int direction, int charsize) – It sets the text font, the direction for display and the size of characters.
  8. outtextxy(int x, int y, char far *string) – It displays a string at position (x,y) on the screen.
  9. line(int x1, int y1, int x2, int y2) – It draws a line from (x1,y1) to (x2,y2) position on the screen.
  10. arc(int x, int y, int stangle, int endangle, int radius) – It draws an arc having a center (x,y) from starting angle to ending angle.
  11. closegraph() – It shuts down the graphics system. So it frees the memory location of the graphics system.
  12. restorecrtmode() – It restores screen mode to pre-initgraph setting.

 

C++ PROGRAM TO CREATE A TRAIN TICKET

Now we will start our code to generate a train ticket using C++ graphics. The program is divided into some small code snippets. Because it will be easy for you to understand. So for implementation, you must copy all the code snippets into one single program file and execute it.

OUTER LAYOUT OF TICKET

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
#include<string.h>
void main()
{
 clrscr();
 int gd=DETECT,gm,x,y;
 initgraph(&gd,&gm,"c:\\TC\\bgi");  //Initializing Graphics Mode
 //OUTER RECTANGLE AND HOLES
 setcolor(15);
 rectangle(630,100,10,400);
 circle(25,115,6);     //LEFT SMALL HOLES
 circle(25,145,6);
 circle(25,175,6);
 circle(25,205,6);
 circle(25,235,6);
 circle(25,265,6);
 circle(25,295,6);
 circle(25,325,6);
 circle(25,355,6);
 circle(25,385,6);
 circle(615,115,6);    //RIGHT SMALL HOLES
 circle(615,145,6);
 circle(615,175,6);
 circle(615,205,6);
 circle(615,235,6);
 circle(615,265,6);
 circle(615,295,6);
 circle(615,325,6);
 circle(615,355,6);
 circle(615,385,6);

 setfillstyle(1,15);
 floodfill(200,200,15);
 //INNER BLUE RECTANGLE
 setcolor(1);
 rectangle(504,113,40,349);

This is the starting code snippet of the program. So at first start the graphics system and draw the outer rectangle for the ticket. The different x,y coordinates are given as input in function parameters to create the ticket properly. But this code uses setcolor(15) – this sets the drawing color as white and setfillstyle(1,15) which sets solid pattern with the white fill color. Because 15 is the integer code for white color. The output screen after executing this code snippet in Turbo C++ generates this output –

TRAIN TICKET LAYOUT

BASIC INNER LAYOUT OF TICKET

//HAPPY JOURNEY
 setcolor(1);
 rectangle(504,113,40,140);
 setfillstyle(1,1);
 floodfill(500,120,1);
 setcolor(15);
 settextstyle(1,0,1);
 outtextxy(70,115,"HAPPY JOURNEY");
 outtextxy(330,115,"HAPPY JOURNEY");

//PNR NO.
 setcolor(1);
 rectangle(135,140,40,170);
 setfillstyle(1,7);
 floodfill(60,150,1);

 setcolor(4);
 settextstyle(8,0,1);
 outtextxy(48,140,"PNR NO.");

 setcolor(1);
 rectangle(135,170,40,190);
 setfillstyle(1,14);
 floodfill(60,180,1);

 setcolor(0);
 settextstyle(2,0,5);
 outtextxy(45,172,"253-7179055");
 outtextxy(46,172,"253-7179055");

//TRAIN NO. DATE KM ADULT CHILD TICKET NO.
 setcolor(1);
 rectangle(380,140,135,170);
 settextstyle(2,0,4);
 outtextxy(140,150,"TRAIN NO.    DATE        K.M. ADULT CHILD");
 outtextxy(385,140,"TICKET NO.");
 rectangle(415,170,135,190);

//TICKET NO.
 setcolor(1);
 settextstyle(0,0,1);
 rectangle(505,170,415,190);
 outtextxy(420,175,"70301530");
 settextstyle(1,0,1);
 outtextxy(385,150,"7030");
 settextstyle(1,0,3);
 outtextxy(433,145,"1530");

//BOOKING CLASS
 setcolor(1);
 rectangle(95,190,40,210);
 setcolor(1);
 settextstyle(2,0,4);
 outtextxy(49,193,"CLASS");

//RESERVATION DETAILS
 setcolor(7);
 rectangle(503,191,96,245);
 setfillstyle(1,7);
 floodfill(149,210,7);
 setcolor(1);
 settextstyle(2,0,4);
 outtextxy(149,210,"RESV. FROM                      RESV. UPTO");

//PASSENGER DETAILS
 setcolor(1);
 rectangle(504,245,40,265);
 setcolor(1);
 settextstyle(2,0,4);
 outtextxy(47,250,"COACH SEAT/BERTH SEX AGE T.AUTHORITY CONC R.FEE S.CH SF.CH VOUCH.Rs. T.CASH.Rs.");

//GRAY SHADE ON AMOUNT
 setcolor(7);
 rectangle(450,267,330,285);
 setfillstyle(1,7);
 floodfill(350,270,7);

 setcolor(7);
 settextstyle(8,0,5);
 outtextxy(62,280,"INDIAN RAILWAYS");
 outtextxy(63,280,"INDIAN RAILWAYS");
 outtextxy(64,280,"INDIAN RAILWAYS");
 outtextxy(65,280,"INDIAN RAILWAYS");
 outtextxy(66,280,"INDIAN RAILWAYS");
 outtextxy(67,280,"INDIAN RAILWAYS");

This code snippet generates the layout of the ticket. It creates the important fields in the ticket like PNR number, class, berth details, ticket number, train details. Because the PNR and ticket numbers are randomly given by the system you can use C++ functions to generate random numbers. The statement setcolor(int color) sets the drawing color. The color codes are as follows –

  • Black – 0
  • Blue – 1
  • Green – 2
  • Cyan – 3
  • Red – 4
  • Magenta – 5
  • Brown – 6
  • Light gray – 7
  • Dark gray – 8
  • Copper blue – 9
  • Bottle green – 10
  • Light cyan – 11
  • Bright red – 12
  • Light magenta – 13
  • Yellow – 14
  • White – 15

So these are the ranges from which you can pick any color as per your choice and requirements. After executing this code snippet we get the output given below –

TRAIN TICKET

DISPLAY INPUT

//INPUTS
 setcolor(1);
 settextstyle(0,1,1);
 outtextxy(40,140,"CENTRAL RAILWAY");

 settextstyle(0,0,1);
 outtextxy(140,175,"12162 20-12-2015  656  1   0");  //TRAIN DETAILS
 outtextxy(303,270,"40     60           770 "); //FARE

 outtextxy(47,267," S4  33  LB  F  19");        //PASSENGER DETAILS
 outtextxy(47,278," S4  34  MB  F  29 ");
 outtextxy(47,289," S4  35  UB  M  45 ");
 outtextxy(47,300," S4  36  LB  F  49 ");
 outtextxy(47,311," S4  37  MB  M  15 ");
 outtextxy(47,322," S4  38  UB  M  37 ");

 outtextxy(250,283,"Rs.SEVEN SEVEN ZERO ONLY");  //TOTAL AMOUNT
 outtextxy(250,296,"VALID WITH ORIGINAL ID");

 settextstyle(2,0,4);
 outtextxy(45,328,"LASHKAR EXP BRD BHOPAL JN SCH DEP 13-02 18:20  ARR 13-02 19:25"); //BOARDING DETAILS AT BOTTOM
 outtextxy(45,338,"256  22-08-2015  18:04 NK  3  VIA  -ET -BSL -MMR");

 setcolor(1);
 settextstyle(10,0,2);
 outtextxy(49,200,"SL");   //BOOKING CLASS

 setcolor(1);
 settextstyle(8,0,1);
 outtextxy(115,220,"BHOPAL JN");   //BOARDING STATION
 outtextxy(290,220,"NASHIK ROAD"); //DESTINATION STATION

 setcolor(1);
 settextstyle(0,0,1);
 outtextxy(155,200,"JOURNEY CUM RESERVATION TICKET");
 outtextxy(440,192,"PRS-CSTM");
 outtextxy(470,230,"256");

Now, after creating the layout of the ticket we need to display the ticket details on the graphics screen. Because we want to display a simple Railway Ticket we will not take the input from the user. But if you want to display the inputs given by users, you need to take input in string format and just replace the strings in code with the variable. Because the outtextxy() function takes only strings for display. The output of this code is –

TRAIN TICKET in C++

You can take inputs using gets() function from the user.

ADVERTISEMENT DESIGN IN TICKET

Advertisement is a must in every Railway ticket. Because it generates revenue to Indian Railways. So we will also include a sample advertisement for our ticket.

//ADVERTISEMENT
setcolor(0);
settextstyle(2,1,4);
outtextxy(10,250,"ATLANTA FORMS-05/2014");
outtextxy(618,150,"FOR ADVERTISEMENT : [email protected]");

setcolor(3);
rectangle(600,113,505,350); //SIDE
setfillstyle(1,3);
floodfill(550,200,3);

setcolor(6);
rectangle(600,350,40,389);  //BTM
setfillstyle(1,6);
floodfill(550,360,6);

setcolor(0);
settextstyle(1,0,2);
outtextxy(45,350,"WHATEVER");
outtextxy(228,350,"BUDGET,");
outtextxy(229,350,"BUDGET,");
outtextxy(230,350,"BUDGET,");

outtextxy(410,350,"PO    Y");
outtextxy(411,350,"PO    Y");
outtextxy(412,350,"PO    Y");
setcolor(4);
outtextxy(437,350,"L"); outtextxy(438,350,"L"); outtextxy(439,350,"L");
outtextxy(452,350,"I"); outtextxy(453,350,"I"); outtextxy(454,350,"I");
outtextxy(462,350,"C"); outtextxy(463,350,"C"); outtextxy(464,350,"C");

setcolor(0);
settextstyle(2,0,6);
outtextxy(155,355,"BE YOUR");
outtextxy(315,355,"WE HAVE A");
outtextxy(500,355,"FOR YOU.");

setcolor(15);
rectangle(590,200,515,340);
setfillstyle(1,15);
floodfill(550,210,15);

setcolor(6);
rectangle(575,202,517,285);
setfillstyle(1,6);
floodfill(550,210,6);

setcolor(1);
settextstyle(10,1,4);
outtextxy(505,200,"LIC");

setfillstyle(1,1);
floodfill(555,210,1);
floodfill(555,247,1);
floodfill(555,274,1);

setcolor(1);
rectangle(575,288,517,338);
setfillstyle(1,1);
floodfill(550,290,1);

setcolor(15);
line(560,300,532,300);
line(560,325,532,325);

line(555,310,559,310);
line(555,315,559,315);

line(555,310,550,304);
line(555,315,550,321);

line(550,304,538,304);
line(550,321,538,321);

line(548,306,538,306);
line(548,319,538,319);

arc(538,312,100,243,13);
arc(554,312,270,70,13);

arc(545,312,270,90,4);
line(545,308,545,315);

arc(541,315,15,140,5);
arc(541,308,210,310,5);

setfillstyle(1,15);
floodfill(546,310,15);
floodfill(541,312,15);

setcolor(1);
settextstyle(2,1,2);
outtextxy(580,200,"LIFE INSURANCE CORPORATION OF INDIA");

setcolor(1);
line(528,185,588,175);
line(528,185,547,122);
line(588,175,547,122);

setcolor(1);
setfillstyle(1,1);
floodfill(540,180,1);

setcolor(11);
line(520,180,580,180);
line(520,180,555,120);
line(580,180,555,120);

setfillstyle(1,11);
floodfill(528,170,11);

setcolor(14);
rectangle(538,134,527,177);
setfillstyle(1,14);
floodfill(528,170,14);

setcolor(0);
settextstyle(0,1,1);
outtextxy(538,135,"LIC'S");

settextstyle(2,1,4);
outtextxy(538,160,"NEW");
outtextxy(548,125,"ENDOWMENT");
outtextxy(558,155,"PLAN");

settextstyle(2,0,3);
outtextxy(550,180,"PLAN NO.814");
settextstyle(2,1,3);
outtextxy(588,108,"UIN : 512N277V01");

settextstyle(2,0,4);
outtextxy(70,375,"CATG COMP NO-1800-111-321,PASSENGER HELPLINE NO:138,PNR/TRAIN TIMING ENQUIRY NO:139");

settextstyle(0,0,1);
outtextxy(70,348,"VALID WITH ORIGINAL ID");
outtextxy(280,348,"TOTAL AMT INCL ALL CHRG");

So let us start understanding the code snippet carefully. It designs an advertisement for the LIC insurance company. But you can design any advertisement as per your wish. Here you see the same functions as described at the start of the tutorial. The parameters given in the functions contain integer numbers. You must plot the points on the graphic screen accurately. Because it may cause some unwanted output. The ticket with advertisement looks like –

TRAIN TICKET with Advertisement

TRAIN LOGO CREATOR AT TOP

Now, the last task is to create the Indian Railways logo at the top center of the ticket. To design the logo we need to create circles and train using straight lines at the center of the circle. The code snippet is given below –

//SYMBOL
 setcolor(4);
 circle(270,128,18);
 setfillstyle(1,4);
 floodfill(270,128,4);

 setcolor(14);
 circle(270,128,11);
 setfillstyle(1,14);
 floodfill(270,128,14);

 setcolor(4);
 circle(270,128,10);

//FOR COLOUR
 setcolor(15);
 line(265,125,262,128);
 line(262,130,257,136);
 line(262,128,262,130);
 line(265,137,257,136);
 line(270,139,265,136);  //\
 line(270,139,275,136);  ///
 line(275,136,275,130);  //|
 circle(270,127,5);      //ENGINE

 setfillstyle(1,15);
 floodfill(270,133,15);
 floodfill(270,127,15);
 setcolor(0);
 line(270,139,265,136);  //\
 line(265,136,265,130);  //|
 line(270,139,275,136);  ///
 line(275,136,275,130);  //|
 line(265,133,275,133); //-
 line(265,135,275,135); //-

 line(267,136,267,135);  //|
 line(269,137,269,135);  //|
 line(271,138,271,135);  //|
 line(273,138,273,135);  //|

 setcolor(4);
 rectangle(267,133,265,131);
 rectangle(275,133,273,131);

 setcolor(0);
 circle(270,127,5); //ENGINE
 setfillstyle(1,0);
 floodfill(270,127,0); //FILLING BLACK COLOR
 setcolor(15);
 circle(270,127,3); //ENGINE

 setfillstyle(1,15);
 floodfill(270,127,15);

 setcolor(0);
 circle(270,127,3); //ENG

//SMOKE CREATION
 line(260,125,270,119);
 line(260,126,270,120);

 setcolor(15);
 circle(270,122,2);
 setfillstyle(1,0);
 floodfill(270,122,15);
 setcolor(0);
 circle(270,122,2);

 setcolor(14);
 circle(270,122,1); //HEAD LIGHT OF TRAIN
 setfillstyle(1,14);
 floodfill(270,122,14);

 setcolor(0);
//CREATING COACH
 line(265,125,262,128);
 line(262,130,257,136);
 line(262,128,262,130);
 line(265,135,257,136);
 line(265,137,257,136);
 rectangle(263,133,262,132);

//CREATING STARS AROUND CIRCLE
 setcolor(14);
 circle(270,112,1);       //NUMBER 1
 setfillstyle(1,14);
 floodfill(270,112,14);
 circle(279,116,1);       //NUMBER 2
 setfillstyle(1,14);
 floodfill(279,116,14);
 circle(284,122,1);       //NUMBER 3
 setfillstyle(1,14);
 floodfill(284,122,14);
 circle(285,130,1);       //NUMBER 4
 setfillstyle(1,14);
 floodfill(285,130,14);
 circle(281,138,1);       //NUMBER 5
 setfillstyle(1,14);
 floodfill(281,138,14);
 circle(274,143,1);       //NUMBER 6
 setfillstyle(1,14);
 floodfill(274,143,14);
 circle(265,142,1);       //NUMBER 7
 setfillstyle(1,14);
 floodfill(265,142,14);
 circle(259,138,1);       //NUMBER 8
 setfillstyle(1,14);
 floodfill(259,138,14);
 circle(255,130,1);       //NUMBER 9
 setfillstyle(1,14);
 floodfill(255,130,14);
 circle(256,122,1);       //NUMBER 10
 setfillstyle(1,14);
 floodfill(256,122,14);
 circle(260,116,1);       //NUMBER 11
 setfillstyle(1,14);
 floodfill(260,116,14);
 getch();
 closegraph();
 restorecrtmode();
}

Here, we have created the logo of Indian Railways on the top of the train ticket designed by us. The logo contains a train at the center of the red circle. The engine and smoke are created using circles of different radius. There are 11 small yellow circles on the outer red filled circles. The getch() function holds the screen till user presses any key. The closegraph() function deallocates the memory and restorecrtmode() restores to pre-initgraph setting.

So the code for creating Indian Railways Train Ticket is thus completed. You can enhance your project’s interface by using this ticket in your program. The final ticket looks like –

TRAIN TICKET in C++

In the end, we are done with the creation of the Indian Railways ticket. The program displays the ticket on the graphics screen. Thanks for reading this article. I hope this tutorial is helpful to you.

 

Leave a Reply

Your email address will not be published. Required fields are marked *