Outputting to a table

This is a discussion on Outputting to a table within the C++ Programming forums, part of the General Programming Boards category; Hi there guys. I'm trying to output the contents of a single subscripted array with the size 100 but I ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Outputting to a table

    Hi there guys. I'm trying to output the contents of a single subscripted array with the size 100 but I am seriously struggling.

    I need the table to look something as follows:

    Code:
            0     1      ... 9
     0  +0000 +0000
    10  +0000 +0000
    ...
    90
    Here is the code I've been working with for a while yet have not gotten anywhere. setw() confuses me quite a bit and I don't want to use /t. Each section of the array has either a positive or negative 4 digit value. Forgive if the code if laughable.

    Code:
       for( int j = 0; j < MEMSIZE; j++ )
       {
          static int number = 0;
          
          if( j % 10 == 0 )
          {
             cout << endl;
             cout << setw( 2 ) << noshowpos << number;
          }
            
          cout << setw( 5 ) << showpos << setfill( '0' ) << mem[ j ] << '\t';
          
          number += 10;
       }  
    }
    Any help would be much appreciated!

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        // Display column header
        cout<<"  ";
        for (int col = 0; col < 10; col++)
            cout<< setw(7) << col;
        cout<<'\n';
        // Display row: header-data-newline
        for (int row = 0; row < 11; row++) {
            cout<< setw(2) << right << noshowpos << setfill(' ') << row <<"  ";
            for (int col = 0; col < 10; col++) {
                cout<< setw(5) << left << showpos << setfill('0') << 0 <<"  ";
            }
            cout<<'\n';
        }
    }

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Dude, you rock! Thanks.

  4. #4
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    Edward, could you explain what your code does? I'm intrested in making some sort of table with results from a computer simulation, but I can't seem to figure out exactly how your code works...Well in the big lines I can, whats say in the variable right and noshowpos and what does setfill do?

    Thanks in advanced

    Spanky
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    whats say in the variable right and noshowpos and what does setfill do?
    right aligns the output to the right. showpos prints a +, noshowpos does the opposite. setfill lets you change the empty space characters set by setw. If you set the field width to 5 and only print 4 real values, the default is to print spaces, ie. setfill(' '), which would be " xxxx". In this case I used setfill to specify leading 0's. The above example would then be "0xxxx".

    C++'s iostream manipulators can be confusing. I usually recommend a good reference and just playing around with different combinations until you get a feel for how they work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hash Table outputting incorrect number
    By Paul Skinner in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 05:19 AM
  2. Outputting a table for print...
    By guesst in forum C++ Programming
    Replies: 13
    Last Post: 11-19-2008, 07:15 PM
  3. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  4. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  5. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM

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