Thread: aligning text

  1. #1
    Unregistered
    Guest

    aligning text

    could someone offer some help

    i have used the search feature and read up on setw() and still no luck

    I have some data which i am holding in a vector of structs and would like to print them in a formatted list

    i can't even get this simple program to line up some strings of varying length

    i have tried all sorts of combos (with and without my "" added)
    I am compiling in bloodhsed

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    string a("This is a test");
    string b("This is only a test");
    string c("Line me up");
    
    cout << "" << setw(20) << a;
    cout << "" << setw(11)<< c << endl;
    
    cout << setw(20) << b;
    cout << "" << setw(11) << c << endl;
    
          system("PAUSE");
          return 0;
    }
    output:
    Code:
    This is a test                    Line me up
    This is only a test                    Line me up
    can someone please tell me what i am doing wrong so i can align text with strings of varying length

    thanks

  2. #2
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    string a("This is a test");
    string b("This is only a test");
    string c("Line me up");
    
    cout << setw(20) << a;
    cout << setw(30)<< c << endl;
    
    cout << setw(20) << b;
    cout << setw(30) << c << endl;
    
          system("PAUSE");
          return 0;
    }
    remember setw() is a right justifying tool so if you had this string:
    "Hello" and its setw(10)
    it would be like this: bbbbbHello, the b's being spaces.

    For yours you have 11 after the initial string which will not work, i think it right justify's from the left hand side of the screen not the text infront of it. Try it. It should be somewhere towards the middle of the screen, i think.
    Last edited by xlnk; 03-31-2002 at 10:43 PM.
    the best things in life are simple.

  3. #3
    Unregistered
    Guest
    I cut and paste your suggestion and the output was still wrong

    I also tried using cout.setf(ios::left)

    in which case it is my understanding using your example
    with "hello" setw(10) the output should be:
    hello_____(next output string)

    so why wouldn't

    cout.setf(ios::left);
    cout << setw(10) << "Hello";
    cout << setw(10) << "Hello";

    give the following:

    Hello_____Hello_____

    Okay and it does work that way.
    But it doesn't work with the strings?????

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    string a("This is a test");
    string b("This is only a test");
    string c("Line me up");
    
    cout.setf(ios::left);
    cout << setw(10) << "Hello";
    cout << setw(10) << "Hello";
    cout << endl;
    
    cout << setw(30) << a;
    cout << setw(30) << b;
    
    cout << endl;
    
          system("PAUSE");
          return 0;
    }

  4. #4
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    I do it like this

    Code:
    .....
    cout<<setw(40)<<"Hello"<<endl;
    cout<<setw(40)<<"hi     "<<endl;
    .....
    Outputs:
    Hello
    hi

    it works when I put that spaces after the small word to match the size of the larger word
    +++
    ++
    + Sekti
    ++
    +++

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