A simple text question.

This is a discussion on A simple text question. within the C++ Programming forums, part of the General Programming Boards category; Hello, I am new to C++, this is only my second post on this forum. Does any one know how ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    A simple text question.

    Hello, I am new to C++, this is only my second post on this forum.

    Does any one know how to center text?

    Thanks in Advance, August

    P.S. My compiler doesn't have <windows.h>

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    In a console window? Just pad with an appropriate number of leading spaces equal to half the screen width in characters minus half the length of the string you are trying to center.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I know how to do that allready.
    I was just wandering if someone knew an eaiser way to do it.

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    i seem to recall there is a setw( ) function lying around somewhere, that might be handy but I dont remember a whole lot about it....
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    Try using

    Code:
    cout.width(#of characters that the console screen can hold);
    cout.setf(ios::center);
    cout<<"Hello World";
    First u are setting a field of certain amt of spaces.
    Then u re saying that u want to center the text
    Then u print "Hello World" to the center.

    I hope i am right cause it would be a shame to tell u incorrect info.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I hope i am right cause it would be a shame to tell u incorrect info.
    In that case you might want to consider refraining from further posts until you're sure of your answers. ios::center does not exist in standard C++. It may be an extension for your compiler, but it's not a common one. Fortunately, you can remove the call to setf completely and something like this would work:
    Code:
    #include <iomanip>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string msg = "Hello, world!";
      const int screen_width = 80;
    
      cout.width ( screen_width / 2 );
      cout<< msg <<endl;
    }
    Further tweaking should be obvious if needed.
    My best code is written with the delete key.

  7. #7
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Just a slight modification:

    Code:
    cout.width((screen_width - msg.length()) / 2);

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just a slight modification
    aka. tweak, as in "Further tweaking should be obvious if needed."
    My best code is written with the delete key.

  9. #9
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    It was needed . . . I just made it a little more obvious

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Perhaps "if desired" would have been better wording.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    no, i liked the first wording
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple text analyzsis program
    By prominababy in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 10:15 AM
  2. text question
    By pherall2k in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2006, 01:26 AM
  3. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 12:59 PM
  4. GLenum = editbox text? (Win32/OGL question)
    By psychopath in forum Windows Programming
    Replies: 4
    Last Post: 08-27-2004, 09:23 AM
  5. text simple question
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 04-26-2002, 09:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21