Fun Lesson
Colours
Colour can really brighten up your programs and make them far more interesting. In this lesson i will
teach text colour, textbackground colour and screen background colour.
For colour to work, we must use another output method : cprintf("bla bla"); I will show how to put
variables into that output later. Color is easy, for text color you write: textcolor(number); where number
is a number between 0 and 15, because console programs (text) only have 16 colors. For text background colors use:
textbackground(number); where number is again 0-15.
Example:
//Program to show colours
#include <conio.h>
#include <iostream.h>
void main()
{
int i;
for(i=0;i<16;i++)
{
textcolor(i);
cprintf("Colour: ");
cout << i << endl;
}
getch();
}
As i have not taught you variables in cprintf("..."); i used cout << i; instead.
This will run and output the word Colour: 16 times in 16 different colours, note that colour 0 is black
therefore you will not see it as the background is black also. Textbackground is exactly the same, just switch
the textcolor(i); to
textbackground(i); and you will get different background colors.
To change the screen background colour, we use a trick, the command clrscr(); clears the screen, but if you
change the background colour before you use clrscr(); it will make the screen that background colour. Try putting -
textbackground(i);
clrscr();
getch();
in the loop above.
Positioning
Personally this is my favourite command, you can output text anywhere on the screen. Console programs have a resolution
of 80*25 which means that you can put 80 characters across and 25 down, this is important to remember as we use co-ordinates
to place text. The command gotoxy(x,y); moves the cursur to that x,y co-ordinate.
Example:
//Positioning program
#include <conio.h>
#include <iostream.h>
void main()
{
gotoxy(30,12);
cout << "This text is centered";
gotoxy(1,25);
cout << "Bottom left";
gotoxy(70,1);
cout << "Top left";
getch();
}
I hope this interests you into programming, also i will write a program below to demonstrate some things weve learned.
My Program
#include <conio.h>
#include <iostream.h>
void Sqaure();
void Line();
void Name();
void main()
{
Sqaure();
Line();
Name();
}
void Square()
{
int i;
textbackground(10);
for(i=0;i<10;i++)
{
gotoxy(30,i+5);
cprintf(" ");
}
textcolor(11);
gotoxy(37,9);
cprintf("Sqaure?");
textbackground(6);
getch();
clrscr();
}
void Line()
{
textcolor(13);
int i;
for(i=1;i<80;i++)
{
gotoxy(i,5);cprintf("*");
gotoxy(i,15);cprintf("*");
}
gotoxy(37,10);cout <<"Lines?";
getch();
textbackground(1);
clrscr();
}
void Name()
{
char Name[20];
int Length, loop=0;
cout << "Please enter your first name: ";
cin >> Name;
Length = strlen(Name);
textbackground(0);
clrscr();gotoxy(37,10);
while(loop < Length)
{
textcolor(loop+1);
cprintf("%C", Name[loop]);
loop++;
}
getch();
}
Try it, get it to work, and have fun.
The cprintf("%C",Name[loop]); means that i want to print out a character - %C , an int would be %d, a float
%f , a string %s , to find more go click - Here.
To view the working program for the above example - Click Here.
Thankyou for learning C++, i hope you will take it further.