C++ program to print your name randomly on the screen with colored text

Print your name on output screen randomly with colored text and background color using C++ program: Here you will learn some of the in-built functions that are using to print randomly, colored, print text of specified position etc. [Last updated : February 26, 2023]

Printing your name randomly on the screen with colored text

This program will print the name until we do not press any key through keyboard, there are text colors and text background color is used to print the name.

Name will be print on random positions.

Following functions are used in this program:

1) kbhit() - This function returns true if key is pressed, in the program we are using while(!kbhit()), that mean program will run until any is not pressed. This function is declared in conio.h.

2) randomize() and rand() – Functions are used to generate random numbers, here we are generating random text colors and background colors till color value 16, value of X and Y co-ordinates. Functions are declared in stdlib.h.

3) textcolor(color) – This function is used to define text color, its value will be 0 to 15 (in hexadecimal: 0x00 to 0x0F). Function is declared in conio.h.

4) textbackground(color) – This function is used to define text background color, its value will be 0 to 15 (in hexadecimal: 0x00 to 0x0F). Function is declared in conio.h.

5) gotoxy(x,y) – To define X and Y co-ordinates where text will be printed.

6) cprintf(string..) - This function is similar to printf, but it is used to print the colored text on console output screen. Function is declared in conio.h.

7) delay(milliseconds) – This function is used to hold the program’s execution for given milliseconds, it is using to run loop after given milliseconds in the program. Function is declared in dos.h.

Note: program is compiled and executed on TurboC3 compiler.

C++ code to print your name randomly on the screen with colored text

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

int main()
{

    int color;
    int bcolor;
    int x, y;
    int d;

    //init random to return random value each time
    randomize();
    //clears the screen
    clrscr();

    //infinite loop until key is not pressed
    while (!kbhit()) {
        //gerenate randon text and background color
        color = rand() % 16;
        bcolor = rand() % 16;

        //define text color
        textcolor(color);
        //define background color
        textbackground(bcolor);
        //check if both colors are same, continue the loop
        if (color == bcolor)
            continue;

        //generate random X and Y Co-ordinates
        x = rand() % 75;
        y = rand() % 20;
        //define position to print the text
        gotoxy(x, y);
        //print the text
        cprintf("IncludeHelp");
        //delay
        delay(100);
    }

    return 0;
}

Output

print your name randomly in C++


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.