Better spacing issues

This is a discussion on Better spacing issues within the C++ Programming forums, part of the General Programming Boards category; Hi guys I am working on some homework at the moment but im not stuck on a logial problem. The ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Better spacing issues

    Hi guys

    I am working on some homework at the moment but im not stuck on a logial problem. The program is working absolutly fine and the output is just what is required.

    My issue comes with the spacing of the output. I am using a 2 dimensional array and the columns are not lined up with the headers. I am using setw() to ensure I get good spacing of each column in the output but they keep going off balance dispite various attempts to change the value of setw().

    I have heard of std::left and was wondering if this could help my spacing issues on this occasion. Here is my code, this is uncompleted so excuse the lack of comments.

    Code:
    #include <iostream>
    #include <iomanip>
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       const int SALES_PERSON = 4; // rows
       const int SALES_ITEMS = 5; // columns
       
       int salesNumber = 1;
       
       int sales[ SALES_PERSON ][ SALES_ITEMS ] = {{ 0, 0 }};
        
       for ( int i = 0; i < SALES_PERSON; )
       {
          for ( int j = 0; j < SALES_ITEMS; j++ )
          {  
             std::cout << "Enter sales total for item " << j 
                       << " for salesman number " << i << " : ";
             std::cin >> sales[ i ][ j ];
          }
          i++;
       }
       
       // table headers
       std::cout << "\n\nSalesman" << std::setw( 12 ) << "Prod 1" << std::setw( 12 ) 
                 << "Prod 2" << std::setw( 12 ) << "Prod 3" << std::setw( 12 )
                 << "Prod 4" << std::setw( 12 ) << "Prod 5" << "\n\n";
                 
       for ( int i = 0; i < SALES_PERSON; i++ )
       {
          for ( int j = 0; j < SALES_ITEMS; j++ )
             std::cout << salesNumber << std::setw( 14 ) << sales[ i ][ j ];
             std::cout << std::endl;
             salesNumber++;
       }
       
       std::cin.get(); // freeze console output window
       std::cin.ignore();
       
       return 0; // return value from int main
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I am using setw() to ensure I get good spacing of each column in the output but they keep going off balance dispite various attempts to change the value of setw().
    What do you expect setw to do? To my understanding you should print each item with the same field-width as the caption (right or left-aligned as you like).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, your headers are in columns of 8-12-12-12-12 (the 8 comes from "Salesman"), while your data is printed out in columns of 1-14-1-14-1-14-1-14. (Note that salesNumber gets printed out for each column!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lowest spacing between numbers
    By Ene Dene in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2007, 04:08 PM
  2. Dev C++ Spacing Problem
    By Darklighter137 in forum C++ Programming
    Replies: 7
    Last Post: 11-13-2006, 10:48 AM
  3. SQL Connection issues
    By jverkoey in forum Tech Board
    Replies: 4
    Last Post: 02-17-2005, 07:52 AM
  4. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM
  5. Spacing?
    By trenzterra in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:42 PM

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