4

I am working on a C++ console app. I want to execute and print all the stuff at the centre of app window screen (horizontally + vertically) as shown below.

 --------------------------------
|                               |
|                               |
|         User : xyz            |
|         Pass : ****           |
|                               |
|                               |
 --------------------------------

I want to run my whole program as pointed out above. Is there any way to do so? Any help or suggestion would be appreciated.

ashu
  • 1,756
  • 4
  • 22
  • 41
  • What have you tried? Are you using `iostreams`? Is the width of the area completely fixed, or will it be whatever width the user has their terminal? – BoBTFish Jun 07 '13 at 09:12
  • @nouney Os is Windows – ashu Jun 07 '13 at 09:13
  • @BoBTFish I am using iostreams Width of app window is not fixed. It will be whatever width the user has their terminal... – ashu Jun 07 '13 at 09:16
  • 2
    @AshutoshGangwar : take a look at this post so : [ncurses-like for windows](http://stackoverflow.com/questions/2713698/ncurses-like-system-for-windows) – nouney Jun 07 '13 at 09:16

3 Answers3

4

How about this (LIVE EXAMPLE):

#include <iostream>
#include <string>
#include <vector>


void centerify_output(std::string str, int num_cols) {
    // Calculate left padding
    int padding_left = (num_cols / 2) - (str.size() / 2);

    // Put padding spaces
    for(int i = 0; i < padding_left; ++i) std::cout << ' ';

    // Print the message
    std::cout << str;
}


int main() {
    std::vector<std::string> lines = {
        "---------------------------------",
        "|                               |",
        "|                               |",
        "|         User : xyz            |",
        "|         Pass : ****           |",
        "|                               |",
        "|                               |",
        "---------------------------------",
    };

    int num_cols = 100;

    // VIRTUAL BORDER
    std::cout << std::endl;
    for(int i = 0; i < num_cols; ++i) std::cout << ' ';
    std::cout << '|' << std::endl;

    // OUTPUT
    for(int i = 0; i < lines.size(); ++i) {
        centerify_output(lines[i], num_cols);
        std::cout << std::endl;
    }

    // VIRTUAL BORDER
    std::cout << std::endl;
    for(int i = 0; i < num_cols; ++i) std::cout << ' ';
    std::cout << '|' << std::endl;
}

You get the idea. When centering the output vertically, you just put padding end lines at the top of the console.

Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
3

Example for WIN:

#include <windows.h>

int main()
{ 
    HANDLE screen = GetStdHandle( STD_OUTPUT_HANDLE );

    COORD max_size = GetLargestConsoleWindowSize( screen );

    char s[] = "Hello world!";

    COORD pos;
    pos.X = (max_size.X - sizeof(s) ) / 2;
    pos.Y = max_size.Y / 2;
    SetConsoleCursorPosition( screen, pos );

    LPDWORD written;
    WriteConsole( screen, s, sizeof(s), written, 0 );

    return 0;
}
Boris
  • 597
  • 2
  • 10
2

If you want to keep your application in a console, but want to do some layouts I'd recommend using ncurses as it gives you more control on where you print, and also gives you a possibility to create menus, message boxes and other GUI-like stuff.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
Losiowaty
  • 7,911
  • 2
  • 32
  • 47