-1

I'm new to C++ I have a ASCII art that looks like this

    string title =
        "  _____ ____  _   _  _____ _      ______  _____ _____ _ \n"
        " / ____/ __ \\| \\ | |/ ____| |    |  ____|/ ____/ ____| |\n"
        "| |   | |  | |  \\| | (___ | |    | |__  | (___| (___ | |\n"
        "| |   | |  | | . ` |\\___ \\| |    |  __|  \\___ \\\\___ \\| |\n"
        "| |___| |__| | |\\  |____) | |____| |____ ____) |___) |_|\n"
        " \\_____\\____/|_| \\_|_____/|______|______|_____/_____/(_)\n";

I want to paste this ASCII art in the console but I don't want it to be on the left side I want it to be centered how would I go about centering this ASCII art in the console.

SplatTab
  • 11
  • 3
  • Add spaces before each line – Pranav Hosangadi Feb 10 '22 at 02:27
  • can add a string(#ofspaces, ' ') before every line to create a string with repeating space – Asphodel Feb 10 '22 at 02:28
  • 2
    Unrelated to the problem, but [raw string literal](https://en.cppreference.com/w/cpp/language/string_literal) may be helpful so that you don't have to escape each and every `\`. – Yksisarvinen Feb 10 '22 at 02:31
  • 2
    To center this stuff, first you need to solve the problem of discovering the width of the console. You might want to consider the _ncurses_ library, which simplifies a lot of the nasty details of manipulating console text. – paddy Feb 10 '22 at 02:37
  • [This](https://stackoverflow.com/questions/16980361/how-to-align-output-to-center-of-screen-c) should help. Also try some modern library like [fmt](https://fmt.dev/latest/syntax.html) which gives easier and type safe string formatting. – Louis Go Feb 10 '22 at 02:46

2 Answers2

1

Alright I found this solution where you can make a array of the lines of the ascii art and then add 50 spaces before each line.

int main()
{
    string title[6] = {"  _____ ____  _   _  _____ _      ______  _____ _____ _ ", " / ____/ __ \\| \\ | |/ ____| |    |  ____|/ ____/ ____| |", "| |   | |  | |  \\| | (___ | |    | |__  | (___| (___ | |", "| |   | |  | | . ` |\\___ \\| |    |  __|  \\___ \\\\___ \\| |", "| |___| |__| | |\\  |____) | |____| |____ ____) |___) |_|", " \\_____\\____/|_| \\_|_____/|______|______|_____/_____/(_)"};

    for (size_t i = 0; i < 6; i++)
        cout << string(50, ' ') + title[i] << endl;

    return 0;
}
SplatTab
  • 11
  • 3
0

Here's an example if you're using fmt with center alignment.


#include <string>
#include <fmt/core.h>

void print(std::string s){
    int width = 100;
    fmt::print("{:*^{}}\n", s, width); // '*' fills the blank space so it's easier to see the center alignment effect.
}

int main(){
    print("  _____ ____  _   _  _____ _      ______  _____ _____ _ ");
    print(" / ____/ __ \\| \\ | |/ ____| |    |  ____|/ ____/ ____| |");
    print("| |   | |  | |  \\| | (___ | |    | |__  | (___| (___ | |");
    print("| |   | |  | | . ` |\\___ \\| |    |  __|  \\___ \\\\___ \\| |");
    print("| |___| |__| | |\\  |____) | |____| |____ ____) |___) |_|");
    print(" \\_____\\____/|_| \\_|_____/|______|______|_____/_____/(_)");
    
    
    return 0;
}


Synax for fmt

Live Demo

Output

**********************  _____ ____  _   _  _____ _      ______  _____ _____ _ **********************
********************** / ____/ __ \| \ | |/ ____| |    |  ____|/ ____/ ____| |**********************
**********************| |   | |  | |  \| | (___ | |    | |__  | (___| (___ | |**********************
**********************| |   | |  | | . ` |\___ \| |    |  __|  \___ \\___ \| |**********************
**********************| |___| |__| | |\  |____) | |____| |____ ____) |___) |_|**********************
********************** \_____\____/|_| \_|_____/|______|______|_____/_____/(_)**********************
Louis Go
  • 2,213
  • 2
  • 16
  • 29