• 0

Centering Texts in C++


Question

In my school I am learning to develop small applications in C++ and there I have to center a title. It's real pain in butt to count all this lines write the codes and use the setw() etc.. is there any easy output code to center the text autmatically like we see in html.

<center><center>

Thanks in advance!

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

are you talking about a console app? you could just write a little center() function that counts the number of characters in a string and appends the correct number of spaces depending on your line size. if that's what you're asking...

Link to comment
Share on other sites

  • 0

i'm not sure what you mean by little applications but....

normally if i'm centering something i use:

cout.setf(ios::center);

followed by

cout << "Your Text" << '\n';

Link to comment
Share on other sites

  • 0
i'm not sure what you mean by little applications but....

normally if i'm centering something i use:

cout.setf(ios::center);

followed by

cout << "Your Text" << '\n';

584828471[/snapback]

Sweet, I was unaware of that as well. Thanks!

Link to comment
Share on other sites

  • 0

Does this actually work for people?

g++    Test.cpp  -o Test

Test.cpp: In function `int main()':

Test.cpp:5: error: `center' is not a member of `std::ios'

Test.cpp:

#include <iostream>
using namespace std;

int main() {
        cout.setf(ios::center);
        cout << "Test";
        return 0;
}

Link to comment
Share on other sites

  • 0
Does this actually work for people?

Test.cpp:

#include <iostream>
using namespace std;

int main() {
        cout.setf(ios::center);
        cout << "Test";
        return 0;
}

584830138[/snapback]

Doesn't work for me. I suspect that ios::center is deprecated.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.