I have to alter my hello world program so that it prints my initials to the middle of the screen. It says i can use any of the escape sequences that help me it says. Can anyone help

Dani AI

Generated

A concise, practical approach: compute the console center, move the cursor there, then print the initials. 's tab suggestion is the quickest hack but will break on different window sizes; pointed out the old Turbo C-era cursor functions, which work in that environment but are non-portable. The example below uses an ANSI cursor-move sequence (widely supported on modern Unix terminals and on recent Windows if virtual-terminal processing is enabled). Replace the fixed cols/rows with a runtime query for your platform when you want it to work reliably.

#include <iostream>
#include <string>

int main() {
    std::string initials = "S61";               // change to your initials
    int cols = 80, rows = 24;                  // replace with actual terminal size
    int col = (cols - (int)initials.size())/2 + 1; // 1-based column
    int row = rows/2;                          // middle row

    // ANSI: ESC[<row>;<col>H moves cursor to (row,col)
    std::cout << "\x1b[" << row << ";" << col << "H" << initials << std::endl;

    return 0;
}

How to make this robust:

  • Obtain actual terminal size at runtime (POSIX: ioctl(TIOCGWINSZ) or use ncurses; Windows: GetConsoleScreenBufferInfo or enable VT processing and use ANSI).
  • Use a fixed-width font and integer math shown above so the text truly centers.
  • If ANSI sequences are not supported, fall back to a native API (ncurses on Unix, Win32 console functions on Windows).

Troubleshooting tips:

  • If the initials appear off, print the computed row/col to verify values.
  • On Windows older than 10, ANSI may not work without extra setup; use the Win32 console API instead.
  • For classroom/legacy Turbo C setups, the older conio-style functions work, but avoid them for portable code.

Recommended Answers

All 4 Replies

Do you want it just to say your name, or say your name and ask you to quit. Can you post what you already have.

Deja Vu all over again! This post is eerily similar to the earlier post you made that folks were replying to. Did you give up on them?

What I hear you asking is how escapes can help move your initials to the center of the screen? Like the TAB escape of \t perhaps?

printf("Hello World\t\t\tS61\n"); // three tabs enough? try and see!

in Turbo C/C++, you can use the gotoxy(x, y) function to move the cur on the screen.

by the way,you must include <conio.h> in your source files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.