Alignment

This is a discussion on Alignment within the C++ Programming forums, part of the General Programming Boards category; I currently got this 2 lines on the outfile text file: Code: s1012 Bennet, Anne D 56.20 P s3343 Johnson, ...

  1. #1
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48

    Alignment

    I currently got this 2 lines on the outfile text file:
    Code:
    s1012  Bennet, Anne        D  56.20  P
    s3343  Johnson, Samuel     D  68.60  Cr
    I want to display "D" on the 28 column(space) in both lines. I don't want to use setw() because I want both lines to be align on the same position, so D and D are shown on a straight vertical line (D sits on top of D). Also I want the Cr to be left-adjusted, so this is what I mean:
    Code:
    P
    Cr
    C should be on the same vertical line as P, NOT
    Code:
     P
    Cr
    What is the code to do such alignment?
    You can't guard me 1 on 1!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    You use field widths (setw()) with justification:
    setw() - left justify

    gg

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You will want to use ostream manipulators and flags if you are using C++ I/O. Look it up in your favorite reference, or look at the link below. Specifically, to left justify output use:

    cout.setf(ios::left);

    as indicated in the link below.

    http://www.cppreference.com/io_flags.html#format_flags
    You're only born perfect.

  4. #4
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Thankx guys, I now know the left justified but what about showing the D in the 28 space part?
    You can't guard me 1 on 1!

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    setw() creates a box. For example, if you use setw(20), that creates a box that is 20 characters wide. You can use 'left' or 'right' to align the output in that box to either the left or the right. It seems to me, you want all your output left aligned, which is done like this:

    cout<<left<<(your output);

    All you have to do is create different size boxes for each piece of your ouput and align the output on the left side of the box, for example:
    Code:
    cout<<left
        <<setw(6)<<studentID
        <<setw(20)<<studentName
        << (etc.);
    To get the proper spacing between data, make the box wider than the actual width of the output.

    #include<iomanip>
    Last edited by 7stud; 05-02-2005 at 07:06 AM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    7stud:
    using left rather than cout << setf(ios::left); was nifty. Thanks.

    wiz23:
    To create the effect you wanted I declared a struct called line which contained 5 fields, a string to hold the className, a string to hold the studentName, a char to hold the classTime, a float to hold the average and a miscellaneous to hold the last field. I also overloaded the << operator for the struct declaration and made it a friend function. Then I used the template provided by 7 stud to line up the fields, but
    to get the average field to line up and show like you want I used the following snippet placed in the appropriate spot:

    setw(7) << fixed << showpoint << setprecision(2) << rhs.average

    I then declared 2 instances of type Line, filled in the fields with the data you provided and ued standard cout syntax to display the Lines and presto--got the output desired.

    Give it a shot on your own. Post code/questions/error messages/etc. when you get stuck and we'll walk you through it. With the above it shouldn't be that hard, assuming you know about structs/classes and operator overloading. If you don't the techniques should be adaptable to code not using structs/classes and operator overloading, but I'd think they would make it easier than using 5 parrallel arrays of data and pulling one field from each array to create the output one field at a time. I'd suggest writing a little program by itself to practice the techniques without cluttering up your major program. When you have the program focusing on getting the data output in the format you desire, then rewrite the process into your larger program. This process has saved me a lot of grief in the past.
    You're only born perfect.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Thankx guys, I now know the left justified but what about showing the D in the 28 space part?
    If the width of all the boxes preceding the "D" add up to 27, then no matter how wide the D box is, if the D box is left justified, the "D" will be in column 28. Note: you want to make the D box wider than 1 because you want some spacing between the "D" and whatever output follows.
    Last edited by 7stud; 05-02-2005 at 09:45 AM.

  8. #8
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I still having trouble getting the right alignment on the outfile text file, the expected output should look like this:
    Code:
    s4569  Barnett, Linda          D  44.80  F
    s5333  Reefs, Christopher      D  79.80  D
    But my one, it look like this:
    Code:
    s4569  Barnett, Linda      D  44.80  F
    s5333  Reefs, Christopher      D  79.80  D
    I have set this already:
    Code:
    outfile<<studentid<<"  "<<surname<<", "<<givenname<<left<<setw(10)<<time<<"  "<<score<<"  "<<grade<<endl;
    When the surname and givenname has more letters, it just move more space to the right in which I can't get the same results as the expected output. How can this be fixed?
    You can't guard me 1 on 1!

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When the surname and givenname has more letters, it just move more space to the right in which I can't get the same results as the expected output. How can this be fixed?
    You need to create a box for each piece of data--not just some of them. The width of each box should be at least as wide as the longest piece of data that will go in that box. In addition, the width of each box should be a little wider than the longest data to provide some spacing between it and the next piece of data.

    All you have to do is create different size boxes for each piece of your ouput and align the output on the left side of the box, for example:
    Last edited by 7stud; 05-03-2005 at 06:45 AM.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In other words, don't use a string of spaces to separate the fields, use boxes to hold the data of each field. The boxes are created with setw(size) where size is the size of the box you want to create. Then you can align the output within the box using whatever manipulator you want, in this case left seems most appropriate. Once set, the alignment holds for all boxes, but the width needs to be indicated for each box. That's why the template was:
    Code:
    outfile << left	//all output will be left justified
    		 << setw(7) //first box will be seven char wide
    						 //there will be 5 char, left justified
    						 //in the box. There will be two spaces
    						//between the end of this data and the 
    					   //start of the next box since each piece
    					   //data will be 5 char
    		<< setw(20)//the next box will be 20 char wide
    						//names are of variable size, unlike 
    						//data in the first box, so the number
    						//of spaces between the end of the 
    						//name and the next box will vary.
    		<< setw(3)//box 3 will start at position 28.
    					   //there will be 1 char and 2 spaces in
    					   //this box.
    		//etc.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alignment tutorial needed!!
    By mynickmynick in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2008, 04:41 AM
  2. Dynamic struct alignment?
    By matthew180 in forum C Programming
    Replies: 7
    Last Post: 06-15-2007, 08:34 AM
  3. memory boundry alignment with stuctures
    By ed bitwise in forum C Programming
    Replies: 3
    Last Post: 05-08-2006, 11:33 AM
  4. Ok, I'm dumb, byte alignment for the third time
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 05:19 PM
  5. trouble with printf alignment
    By hyaline in forum C Programming
    Replies: 5
    Last Post: 09-22-2001, 01:39 AM

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