Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Games Code Examples

Caught in the Middle - Game

/* Caught in the Middle - Game */ #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> #include<stdio.h> enum dir direction; /*indicates the direction of the shooter*/ int fire(int xco,int yco,int &lr,int &tb,int &rl,int &bt); /*shoots place check for hit*/ /*The most complex function flashes the green boxes and*/ /*records their coordinates*/ int environment(int xposition,int yposition,int position_array_location); int match(void); /*checks the fired location and the locations of the boxes*/ int put_banner(void); /*banner at the end of game*/ /*structure to record the position of boxes*/ //void refresh(); struct position { int x_pos; int y_pos; }; struct position p[10]; /*array to record the locations of boxes*/ struct position sh; /*variable to record the location of the shooter*/ enum dir{EAST,WEST,SOUTH,NORTH}; /*enum to indicate direction*/ unsigned int hitcount=0; void main() { int gdriver = DETECT; int gmode; int ch,is_hit; int x=350,y=200;/*x,y coordinates*/ /*variables used to move around boxes*/ int i=0,j=320,k=400,l=0,m=800,n=200,p=600,o=300; int iflag,lflag,mflag,pflag; /*flag to indicate which box has been hit*/ initgraph(&gdriver,&gmode,"c:\tc\bgi"); /*initiating graphics*/ setcolor(YELLOW); settextstyle(TRIPLEX_FONT,HORIZ_DIR,5); /*Drawing the initial banners*/ outtextxy((getmaxx()/2)-260,(getmaxy()/2)-20,""CAUGHT IN THE MIDDLE""); setcolor(WHITE); outtextxy(100,50,"Use "arrow" key to move"); outtextxy(100,100,"Use "enter" to fire"); /*drawing banner completed*/ getch(); settextstyle(DEFAULT_FONT,HORIZ_DIR,0); setbkcolor(BLUE); delay(1000); cleardevice(); x=getmaxx()/2;y=getmaxy()/2; moveto(x,y); do { /*draws the shooter*/ setfillstyle(SOLID_FILL,WHITE); circle(x,y,10); floodfill(x,y,WHITE); circle(x,y,20); line(x,y-20,x,y+20); line(x-20,y,x+20,y); /*draws the shooter*/ //refresh(); iflag=0,lflag=0,mflag=0,pflag=0; while(!kbhit()) /*until the keyboard is hit boxes are flashed*/ { i=i%800; l=l%600; if(m<=0)m+=800; if(p<=0)p+=600; environment(i,j,0); /*moves from left to right*/ environment(k,l,1); /*moves from top to bottom*/ environment(m,n,2); /*moves from right to left*/ environment(o,p,3); /*moves from bottom to top*/ i+=20; l+=20; m-=20; p-=20; if(i>=800) j=rand()%600; if(l>=600) k=rand()%800; if(m<=0) n=rand()%600; if(p<=0) o=rand()%800; } ch =getch(); /*gets key from user*/ /*key board is hit*/ setfillstyle(SOLID_FILL,BLUE); /*draws a block to erase shooter at previous position*/ bar(x+20,y+20,x-20,y-20); x=getx(); y=gety(); sh.x_pos=x; /*gets the shooter position*/ sh.y_pos=y; setfillstyle(SOLID_FILL,WHITE); /*draws the shooter*/ circle(x,y,10); floodfill(x,y,WHITE); circle(x,y,20); line(x,y-20,x,y+20); line(x-20,y,x+20,y); setfillstyle(SOLID_FILL,BLUE); bar(x+20,y+20,x-20,y-20); outtextxy(240,10,"HIT CTRL+Z TO QUIT"); switch(ch) { case 72:/*up arrow*/ direction=NORTH; x=getx(); y=gety(); moveto(x,y-20); x=getx(); y=gety(); sh.x_pos=x; sh.y_pos=y; setfillstyle(SOLID_FILL,WHITE); circle(x,y,10); floodfill(x,y,WHITE); circle(x,y,20); line(x,y-20,x,y+20); line(x-20,y,x+20,y); break; case 80:/*down arrow*/ direction=SOUTH; x=getx(); y=gety(); moveto(x,y+20); x=getx(); y=gety(); sh.x_pos=x; sh.y_pos=y; setfillstyle(SOLID_FILL,WHITE); circle(x,y,10); floodfill(x,y,WHITE); circle(x,y,20); line(x,y-20,x,y+20); line(x-20,y,x+20,y); break; case 75:/*left arrow*/ direction=WEST; x=getx(); y=gety(); moveto(x-20,y); x=getx(); y=gety(); sh.x_pos=x; sh.y_pos=y; setfillstyle(SOLID_FILL,WHITE); circle(x,y,10); floodfill(x,y,WHITE); circle(x,y,20); line(x,y-20,x,y+20); line(x-20,y,x+20,y); break; case 77:/*right arrow*/ direction=EAST; x=getx(); y=gety(); moveto(x+20,y); x=getx(); y=gety(); sh.x_pos=x; sh.y_pos=y; setfillstyle(SOLID_FILL,WHITE); circle(x,y,10); floodfill(x,y,WHITE); circle(x,y,20); line(x,y-20,x,y+20); line(x-20,y,x+20,y); break; case 13:/*enter key*/ x=getx(); y=gety(); sh.x_pos=x; sh.y_pos=y; is_hit=fire(x,y,iflag,lflag,mflag,pflag); /*flags r passed by reference*/ if(is_hit==1) { hitcount++; if(iflag)i+=800; else if(lflag)k+=600; else if(mflag)m=0; else if(pflag)p=0; iflag=0;lflag=0;mflag=0;pflag=0; /*flag reset*/ } x=getx(); y=gety(); setfillstyle(SOLID_FILL,WHITE); circle(x,y,10); floodfill(x,y,WHITE); circle(x,y,20); line(x,y-20,x,y+20); line(x-20,y,x+20,y); break; } }while(ch!=26); closegraph(); restorecrtmode(); gotoxy(30,12); textcolor(YELLOW); cprintf("YOU HAVE SCORED :%d",hitcount);/*score is indicated*/ getch(); } /*function fire draws ans erases a line along the current direction*/ int fire(int x,int y,int <r,int &ttb,int &rtl,int &btt) { int i=0,hit=0; switch(direction) { case EAST:/*right*/ if(sh.y_pos>=p[i].y_pos-20&&sh.y_pos<=p[i].y_pos+20&&sh.x_pos<p[i].x_pos) { hit=1; switch(i) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.y_pos>=p[i+1].y_pos-20&&sh.y_pos<=p[i+1].y_pos+20&&sh.x_pos<p[i+1].x_pos) { hit=1; switch(i+1) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.y_pos>=p[i+2].y_pos-20&&sh.y_pos<=p[i+2].y_pos+20&&sh.x_pos<p[i+2].x_pos) { hit=1; switch(i+2) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.y_pos>=p[i+3].y_pos-20&&sh.y_pos<=p[i+3].y_pos+20&&sh.x_pos<p[i+3].x_pos) { hit=1; switch(i+3) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } setcolor(YELLOW); line(x+20,y,x+800,y); setcolor(BLUE); delay(100); line(x+20,y,x+800,y); setcolor(WHITE); break; case WEST:/*left*/ if(sh.y_pos>=p[i].y_pos-20&&sh.y_pos<=p[i].y_pos+20&&sh.x_pos>p[i].x_pos) { hit=1; switch(i) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.y_pos>=p[i+1].y_pos-20&&sh.y_pos<=p[i+1].y_pos+20&&sh.x_pos>p[i+1].x_pos) { hit=1; switch(i+1) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.y_pos>=p[i+2].y_pos-20&&sh.y_pos<=p[i+2].y_pos+20&&sh.x_pos>p[i+2].x_pos) { hit=1; switch(i+2) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.y_pos>=p[i+3].y_pos-20&&sh.y_pos<=p[i+3].y_pos+20&&sh.x_pos>p[i+3].x_pos) { hit=1; switch(i+3) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } setcolor(YELLOW); line(x-20,y,x-800,y); setcolor(BLUE); delay(100); line(x-20,y,x-800,y); setcolor(WHITE); break; case SOUTH:/*down*/ if(sh.x_pos>=p[i].x_pos-20&&sh.x_pos<=p[i].x_pos+20&&sh.y_pos<p[i].y_pos) { hit=1; switch(i) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.x_pos>=p[i+1].x_pos-20&&sh.x_pos<=p[i+1].x_pos+20&&sh.y_pos<p[i+1].y_pos) { hit=1; switch(i+1) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.x_pos>=p[i+2].x_pos-20&&sh.x_pos<=p[i+2].x_pos+20&&sh.y_pos<p[i+2].y_pos) { hit=1; switch(i+2) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.x_pos>=p[i+3].x_pos-20&&sh.x_pos<=p[i+3].x_pos+20&&sh.y_pos<p[i+3].y_pos) { hit=1; switch(i+3) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } setcolor(YELLOW); line(x,y+20,x,y+800); setcolor(BLUE); delay(100); line(x,y+20,x,y+800); setcolor(WHITE); break; case NORTH:/*up*/ if(sh.x_pos>=p[i].x_pos-20&&sh.x_pos<=p[i].x_pos+20&&sh.y_pos>p[i].y_pos) { hit=1; switch(i) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.x_pos>=p[i+1].x_pos-20&&sh.x_pos<=p[i+1].x_pos+20&&sh.y_pos>p[i+1].y_pos) { hit=1; switch(i+1) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.x_pos>=p[i+2].x_pos-20&&sh.x_pos<=p[i+2].x_pos+20&&sh.y_pos>p[i+2].y_pos) { hit=1; switch(i+2) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } else if(sh.x_pos>=p[i+3].x_pos-20&&sh.x_pos<=p[i+3].x_pos+20&&sh.y_pos>p[i+3].y_pos) { hit=1; switch(i+3) { case 0: ltr=1; break; case 1: ttb=1; break; case 2: rtl=1; break; case 3: btt=1; break; } } setcolor(YELLOW); line(x,y-20,x,y-800); setcolor(BLUE); delay(100); line(x,y-20,x,y-800); setcolor(WHITE); break; } for(i=0;i<=3;i++) { p[i].x_pos=0; p[i].y_pos=0; } /*refreshing the record*/ return(hit); /* of a positions*/ } /*draws the green boxes */ int environment(int x,int y,int pos) { int i; p[pos].x_pos=x; p[pos].y_pos=y; /*conditions to check for collision checks whether the center of box x,y is within the area (sh_xpos-20,sh_ypos-20) (sh_xpos+20,sh_ypos+20)*/ if(x>=sh.x_pos-20&&x<=sh.x_pos+20&&y>=sh.y_pos-20&&y<=sh.y_pos+20) { put_banner(); getch(); closegraph(); restorecrtmode(); gotoxy(30,12); textcolor(YELLOW); cprintf("YOU HAVE SCORED :%d",hitcount); getch(); exit(0); } setfillstyle(SOLID_FILL,LIGHTGREEN); bar(x-10,y-10,x+10,y+10); delay(20); setfillstyle(SOLID_FILL,BLUE); bar(x-10,y-10,x+10,y+10); setcolor(WHITE); return(0); } /*puts a banner at the End of Game*/ int put_banner() { setcolor(YELLOW); settextstyle(TRIPLEX_FONT,HORIZ_DIR,5); outtextxy((getmaxx()/2)-260,(getmaxy()/2)-20,"SORRY YOU`VE BEEN HIT"); return 0; } void refresh(void) { int i; for(i=0;i<4;i++) { p[i].x_pos=0; p[i].y_pos=0; } }

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword "enum" is used to declare an enumeration. It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The C++ enum constants are static and final implicitly. C++ Enums can be thought of as classes that have fixed set of constants.

The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.

Use the textcolor function to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral. Now, if you want your text to blink then while calling the textcolor() function pass the color and also say BLINK. This will like this: textcolor(BLUE+BLINK).

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.

bar() function is a C graphics function that is used to draw graphics in the C programming language. The graphics.h header contains functions that work for drawing graphics. The bar() function is also defined in the header file. Bar function is used to draw a 2-dimensional, rectangular filled in bar. Coordinates of left top and right bottom corner are required to draw the bar. Left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color use setfillstyle.

This library function is declared in graphics.h and used to draw a circle; it takes centre point coordinates and radius. Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. The code given below draws a circle. Where, (x, y) is center of the circle. 'radius' is the Radius of the circle.

Function moves the current position(cp) to (x,y). The header file graphics.h contains moveto() function which changes the current position to (x, y). Means if you want to move a point from the current position to a new position then you can use this function. outtext is the function used to display the given string on graphics mode. It uses the font style set by settextstyle and the current position. The current position can be changed with moveto function. Alternatively you can use outtextxy function which takes xpos, ypos and string for displaying at a particular location. outtextxy does the work of both moveto and outtext functions.

The header file graphics.h contains closegraph() function which closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the mode it was in before you called initgraph. closegraph() function is used to re-enter in the text mode and exit from the graphics mode. If you want to use both text mode and graphics mode in the program then you have to use both initgraph() and closegraph() function in the program. This function deallocates all memory allocated by graphics system and restores the screen to that mode in which it was presented before you called the initgraph() function.

setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.

restorecrtmode restores the original video mode detected by initgraph. This function can be used in conjunction with setgraphmode to switch back and forth between text and graphics modes. Textmode should not be used for this purpose; use it only when the screen is in text mode, to change to a different text mode. restorecrtmode is implemented in winbgim, but it does not do any work. This is because both the graphics window and the text window are always open during any Windows program, so there is no need to switch back and forth between the two modes.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized. C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on)

setbkcolor() function is used to set the background color in graphics mode. The default background color is black and default drawing color as we know is white. setbkcolor() function takes only one argument it would be either the name of color defined in graphics.h header file or number associated with those colors. If we write setbkcolor(yellow) it changes the background color in Green. The possible color values are from 0 - 15 black, blue, green, cyan, red, magenta, brown, lightgray, darkgray, lightblue, lightgreen, lightcyan, lightred, lightmagenta, yellow, white and blink (128).

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

The header file graphics.h contains cleardevice() function. cleardevice() is a function which is used to clear the screen by filling the whole screen with the current background color. It means that cleardevice() function is used to clear the whole screen with the current background color and it also sets the current position to (0,0). Both clrscr() and cleardevice() functions are used to clear the screen but clrscr() is used in text mode and cleardevice function is used in the graphics mode.

floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled, border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle.

Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.

outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

The header file graphics.h contains getmaxy() function which returns the maximum Y coordinate for current graphics mode and driver. getmaxy returns the maximum (screen-relative) y value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxy returns 199. getmaxy is invaluable for centering, determining the boundaries of a region onscreen, and so on.

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.

Generate random number. Returns a pseudo-random integral number in the range between 0 and RAND_MAX. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand. RAND_MAX is a constant defined in <cstdlib>. The rand() function in C++ is used to generate random numbers; it will generate the same number every time we run the program. In order to seed the rand() function, srand(unsigned int seed) is used. The srand() function sets the initial point for generating the pseudo-random numbers. The rand() function generates numbers randomly.

The kbhit is basically the Keyboard Hit. This function is present at conio.h header file. So for using this, we have to include this header file into our code. The functionality of kbhit() is that, when a key is pressed it returns nonzero value, otherwise returns zero. kbhit() is used to determine if a key has been pressed or not. If a key has been pressed then it returns a non zero value otherwise returns zero.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

The header file graphics.h contains getmaxx() function which returns the maximum X coordinate for current graphics mode and driver. getmaxx() returns the maximum (screen-relative) x value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxx returns 319. getmaxx is invaluable for centering, determining the boundaries of a region onscreen, and so on.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

The header file graphics.h contains line() function which is used to draw a line from a point(x1, y1) to point(x2, y2) i.e. (x1, y1) and (x2, y2) are end points of the line. The function line() draws a line on the graphics screen between two specified points. So this function requires four parameters namely x1, y1, x2, and y2 to represent two points. This function draws a line from (x1, y1) coordinates to (x2, y2) coordinates on the graphics screen.

Positions cursor in text window. The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved. If the coordinates are in any way invalid the call to gotoxy is ignored. Neither argument to gotoxy can be zero.

delay() function is used to hold the program's execution for given number of milliseconds, it is declared in dos.h header file. There can be many instances when we need to create a delay in our programs. C++ provides us with an easy way to do so. We can use a delay() function for this purpose in our code. We can run the code after a specific time in C++ using delay() function.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.








As a function can be created to return a value of a primitive type, a function can be defined to return a reference to primitive type. When 'declaring such a function', you must indicate