Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to align at the center the text in c++ program?

+5 votes
asked Apr 22, 2022 by CAHAYAG CRISTINE H (170 points)

3 Answers

0 votes
answered Apr 22, 2022 by KAMLESH SANJAY BAVISKAR (180 points)
by giving the tab space you can easily move text at the center

ex.,

printf("\n\t\t\tTEXT");

you can give tab according to your ide...
0 votes
answered Apr 22, 2022 by Peter Minarik (86,040 points)

There is no single standard function for this.

C++ (and C) were supposed to be platform-independent, but features like these call for platform-specific solutions. You need to know the dimensions of your console for instance and to get this, every operating system has its own solution.

You would need something like this:

  • platform-dependent API to get the width of the console where you output the standard output
  • calculate the length of the text (should be string.size())
  • platform-dependent API to position your cursor
  • put the cursor at (width - length) / 2 and print out the text.
0 votes
answered Apr 25, 2022 by userdotexe (1,340 points)

The answer to another question very similar was

std::string center(std::string input, int width = 113) { 
    return std::string((width - input.length()) / 2, ' ') + input;
}

:)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...