Fundamentals of C Programming/Useful functions

From Wikibooks, open books for an open world
Jump to: navigation, search

In this chapter, you will get some codes that you can use to make your program user-friendly. We see many programs daily. Can we make such programs using the 16 bit DOS environment? No, we cannot. But we can try to go as near to it as possible. With that in mind, there are certain functions that have been provided below that you may like to include in your program.

Boxes[edit]

In the programs we encounter everyday, we see that the output is in selected parts of the screen and not from the extreme left corner as in the programs we have seen so far. So how to construct such a box in DOS? Here is the code to do so:

Double line box[edit]


void drawbox(int boxa,int boxb,int boxc,int boxd)
{
	int i,j;
	char s=205,c=201,q=186,d=187,e=200,f=188;
	gotoxy(boxa,boxb);
	cout<<c;
	gotoxy(boxa,boxd-1);
	cout<<e;
	gotoxy(boxc-1,boxb);
	cout<<d;
	gotoxy(boxc-1,boxd-1);
	cout<<f;
	for(i=boxa+1;i<boxc-1;i++)
	{
		for(j=boxb;j<boxd;j++)
		{
			gotoxy(i,j);
			cout<<s;
		}
	}
	for(i=boxa;i<boxc;i++)
	{
		for(j=boxb+1;j<boxd-1;j++)
		{
			gotoxy(i,j);
			cout<<q;
		}
	}
	for(i=boxa+1;i<boxc-1;i++)
	{
		for(j=boxb+1;j<boxd-1;j++)
		{
			gotoxy(i,j);
			cout<<" ";
		}
	}
	gotoxy(boxa+1,boxb+1);
}

The coordinates passed to this function are the x and y coordinates of the top-left and the bottom-right corner of the box that we want to print. Then special characters for each corner are printed on the specified points on the screen. Then the horizontal and vertical lines are printed using the for loops. The space within them is then cleared using for loops and printing the ' ' character on the coordinates.You can try experimenting with the values that are passed on to drawbox() and see the result.The function call of this function can be like:

        drawbox(7,2,37,24);
	drawbox(45,2,75,24);

These two calls will create two boxes on the screen. Even if you are not able to go into the depth of this function, you can use it with ease by pasting the code before the main() function. [[Category:]]Fundamentals of C Programming