Need help with alignment

This is a discussion on Need help with alignment within the C++ Programming forums, part of the General Programming Boards category; OS: Win 2K Environment: Desktop I cannot get this program to display the figures in the last two rows correctly. ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Need help with alignment

    OS: Win 2K Environment: Desktop

    I cannot get this program to display the figures in the last
    two rows correctly. See the different results I obtained.
    If I take out the "scientific" notation code out the result
    will even get worse.
    ----------------- - - - - - - -
    My codes:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    int main()
    {
    cout.setf(ios::fixed | ios::scientific);
    static double values[] = {1.23, 35.44, 653.70, 4358.224};
    static char* names[] = {"zoot", "Jimmy", "Al", "Stan"};

    for (int i=0; i < 4; i++)
    {
    // commented codes not good for use
    //cout << setiosflags(ios::left | ios::showpoint);
    //cout << setw(6) << names[i] << endl;
    //cout << resetiosflags(ios::left);
    //cout << setiosflags(ios::right);
    //cout << setw(10) << values[i] << endl;

    cout.setf(ios::left);
    cout << setw(6);
    cout << names[i];
    cout << resetiosflags(ios::left);
    //cout.setf(ios::fixed | ios::right | ios::showpoint);
    cout.setf(ios::fixed | ios::right);
    // cout.setf(ios::right);
    cout << setw(11)
    //<< setprecision(2)
    << values[i]
    << endl;
    }
    return 0;
    } // end of main
    --=-=-=-=-=-=-=-
    Result without using setprecision 2 and showpoint:
    zoot 1.23000
    Jimmmy 35.4400
    Al 653.700
    Stan 4358.22
    Press any key to continue
    -------
    Result with set precision 2 and showpoint:
    zoot 1.2
    Jimmmy 35.
    Al 6.5e+002
    Stan 4.4e+003
    Press any key to continue
    ----------
    ================================
    Result without showpoint:
    zoot 1.2
    Jimmmy 35
    Al 6.5e+002
    Stan 4.4e+003
    Press any key to continue
    ------------------
    Result without showpoint and setprecision. Got good result
    here except the trailing zero from Al amount is missing
    and the figures of the last two lines aren't lined up properly.
    zoot 1.23
    Jimmmy 35.44
    Al 653.7
    Stan 4358.22
    Press any key to continue

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    How exactly do you want it to look?

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    OT: Nice avatar Prelude

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