Results 1 to 3 of 3

Thread: setw

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    10

    setw

    When using setw with cout, how do i center align the text/numbers within the specified width?

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You can't. That's not how setw() works.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You could do something like:

    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    void setXPos(int x);
    
    int main() {
    
        char text[20] = "Text Here!";
        int len = 0;
    
        len = (80 - strlen(text)) / 2;
    
        setXPos(len);
        cout << text << endl << endl;
    
        return 0;
    }
    
    
    void setXPos(int x) {
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
        COORD coord;
    
        GetConsoleScreenBufferInfo(hConsole, &csbiInfo);
    
        coord.X = x;
        coord.Y = csbiInfo.dwCursorPosition.Y;
    
        SetConsoleCursorPosition(hConsole, coord);
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width