CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Posts
    11

    Centering Text With SetW Or SetFill Without The Use Of <Strings> Or Function?

    Hello All,

    My question is simply how do I center the lines on top of one another using setw (without the use of an additional header file or an additional function)?

    I've attempted
    Code:
    setfill(' ')
    to no avail as well.

    I can manually set the numbers of setw of course, but I was hoping for alternative smarter fix (without having to pain staking manually set & test each setw).

    Note for reason I cannot use an additional header file or function: I've always wanted to know if this could even be done without the use of the <strings> header file (and without the use of an accompanying function). That, and I wanted to reduce the amount of lines of code if possible. If there's not an easier way that's fine (just wanted to know if setw or setfill could get the centering job done alone).

    I didn't think it could be done (I believe with the header files I have and place & with no additional functional help one would have to simply manually set each setw).

    This one has always intrigued me. As always any help is appreciated.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        char Enter;
        
        cout << setw(0) << "FIRST LINE HAS 28 CHARACTERS" << endl << endl;
        cout << setw(0) << "SECOND LINE HAS 18" << endl << endl;
        cout << setw(0) << "THIRD LINE HAS 28 CHARACTERS" << endl << endl;
        cout << setw(0) << "FOURTH LINE HAS 18";
    	
        cin.ignore();
        cin.get(Enter);
        return 0;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Centering Text With SetW Or SetFill Without The Use Of <Strings> Or Function?

    I too have my doubts. It would be easier to just write that function with the help of std::string anyway.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Centering Text With SetW Or SetFill Without The Use Of <Strings> Or Function?

    Quote Originally Posted by Alyssa Saila View Post
    I've always wanted to know if this could even be done without the use of the <strings> header file (and without the use of an accompanying function). That, and I wanted to reduce the amount of lines of code if possible. If there's not an easier way that's fine (just wanted to know if setw or setfill could get the centering job done alone).
    Somehow you need to know the length of the string. How are you going to accomplish that without calling some function?

    Just write a function using std::string. That in itself reduces the number of lines of code in your main() program and makes your program more readable anyway.
    Code:
    void WriteCenteredString( int nTotalLineWidth, const std::string& strToCenter)
    {
    }
    Then you just call this function any number of times you want with any string you want, with any total line length you wish.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 31st, 2012 at 04:28 AM.

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

Featured