Thread: Centering text

  1. #1
    Unregistered
    Guest

    Centering text

    Anyone have a good algorithm for centering text in a framed greeting?

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Using:

    vector<string> greeting;
    and
    string line;
    and
    greeting.push_back(line);

    for example

    see Accelerated C++ section 2.5.4 The complete framing program.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void PrintCenteredText(char* String)
    {
       for(int i = 0; i < ((80 - strlen(String)) / 2); i++) cout << " ";
       cout << String;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    BMJ
    Guest
    Centering text is easy... using 'gotoxy' (assuming you mean DOS/console):
    Code:
    centeredX = (SCREEN_WIDTH-strlen(TEXT_TO_BE_CENTERED))/2;
    centeredY = (SCREEN_HEIGHT)/2;
    gotoxy(centeredX, centeredY);

  5. #5
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    stupid login!

  6. #6
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    I dont get it.. I'm using Dev-C++.. this is my code:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    {
      gotoxy(10,10);
      printf("Hello World");
      return 0;
    }
    And this is the compiling stats:

    Building Makefile: "E:\Dev-C++\Projects\Temp\Makefile.win"
    Executing make...
    make.exe all -f "E:\Dev-C++\Projects\Temp\Makefile.win"
    g++.exe -c main.cpp -o main.o -I"E:\DEV-C++\include" -I"E:\DEV-C++\include" -s

    main.cpp: In function `int main()':
    main.cpp:7: `gotoxy' undeclared (first use this function)

    main.cpp:7: (Each undeclared identifier is reported only once for each function
    it appears in.)

    g++.exe main.o -o "temp.exe" -L"E:\DEV-C++\lib" -I"E:\DEV-C++\include" -I"E:\DEV-C++\include" -s

    Execution terminated


    Please Help
    what does signature stand for?

  7. #7
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Muahhahha, gotoxy, the non-standard function in conio.h
    Read the tutorials to find out how to make your own gotoxy

  8. #8
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Anyway.. this doesnt work either: SetConsoleCursorPosition .. unless I don't know how to make it.. plz help (short-examples/tutorials)
    what does signature stand for?

  9. #9
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    SetConsoleCursorPosition is a windows function... you need to #include <windows.h>

    Code:
    #include <windows.h>
    
    ...
    
    void WinConsole_gotoxy(int x, int y)
    {
          COORD coord = {x, y};
         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }

  10. #10
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    It is a nation born in the quiet part of the mind,
    that has no fantasy of omnipotence,
    no God but nature, no net of one vow,
    no dark corner of the poor, no fugue-work of hate,
    no hierarchies of strength, knowledge, or love,
    no impure water spasming from rock, no swarm of polluted flies,
    no ash-heap of concrete, gypsum, and glass,
    no false mercy or truths buried in excrement;
    and in this nation of men and women,
    no face in the mirror reflecting more darkness
    than light, more strife than love, no more strife
    than in my hands now, as I sit on a rock,
    tearing up bread for red and white carp
    pushing out of their element into mine.
    You didn't phrase you question clear. It could mean
    take this poem as
    a string of charecters and try to center it so that
    there's about a 5 charecter margin on both size and your
    line is 60 charecters. Of course this isn't clear either.

  11. #11
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    *looks at the date of the original post*

    WTF?

  12. #12
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    hopefully it didn't take the original poster 4 days to come
    up with a solution.

  13. #13
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Originally posted by BMJ
    SetConsoleCursorPosition is a windows function... you need to #include <windows.h>

    Code:
    #include <windows.h>
    
    ...
    
    void WinConsole_gotoxy(int x, int y)
    {
          COORD coord = {x, y};
         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    Thanks.. I got it now.. another thing, how'd I set a text's foreground colour?
    what does signature stand for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM