I am trying to create a multiplication table that is left aligned with a line under the header and a line next to the first column on the left. What I have here gives me what I want but without the lines. Can anyone help?

1. #include <iostream>
2..#include <iomanip>
3. using namespace std;

4. //program that computes a multiplication table, basic formatting or

5. int main() {

6.   int i,j;

7.    //first print out the header
8.    cout << setw(5) << std::left << " ";
9.    for(i=1; i<=10; i++) {   
10.        cout << setw(5) << i;
11.   }
12.    cout << endl;
13.   //now print out the table
14.   for(i=1; i<=10; i++) {
15.        //print out the row  
16.        cout << setw(5) <<std::left  << i;
17. for(j=1; j<=10; j++) {
18.            cout << setw(5) << std::left << i*j;
19.        }
20.        cout << endl;        
21.    }


22.    return 0;
23.}

Edited 3 Years Ago by happygeek: fixed formatting

in addition to what you are currently doing, you also need to print the characters for the lines. for example:

#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
   cout << setw(4) << right << "|";
   for( int i=1; i<=10 ; ++i ) cout << left << setw(5) << i ;
   cout << "\n------------------------------------------------------\n" ;

   for( int i=1; i<=10; ++i ) 
   {
       cout << setw(3) << left << i << '|' ;
       for( int j = 1; j<=10 ; ++j )
             cout << setw(5) << left << i*j ;
       cout << endl;
   }
 }

Thank you! How do I create a space between each horizontal line?

change line 16 to: cout << "\n |\n" ;

Horizontal is ---- not |

Blank lines can be added as Vij says by outputting a \n or endl

Horizontal is ---- not |

we need a blank hoizontal line:
<!blank> | a b c d ....
< blank> |
<!blank> | a b c d ....

Horizontal is ---- not |

Blank lines can be added as Vij says by outputting a \n or endl

I don't mean to be mean but why don't you read the thread carefully before trying to be clever.
The "|" is put there to keep consistent formatting in Vj's original post.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   cout << setw ( 4 ) << right << "|";
   for ( int i = 1; i <= 10 ; ++i ) cout << left << setw ( 5 ) << i ;
   cout << "\n------------------------------------------------------\n" ;

   for ( int i = 1; i <= 10; ++i )
   {
      cout << setw ( 3 ) << left << i << '|' ;
      for ( int j = 1; j <= 10 ; ++j )
         cout << setw ( 5 ) << left << i*j ;
      cout << "\n   |\n" ;
   }
}

My output

thwee@thwee-desktop:~$ g++ -Wall pedantic.cc
thwee@thwee-desktop:~$ ./a.out
   |1    2    3    4    5    6    7    8    9    10   
------------------------------------------------------
1  |1    2    3    4    5    6    7    8    9    10   
   |
2  |2    4    6    8    10   12   14   16   18   20   
   |
3  |3    6    9    12   15   18   21   24   27   30   
   |
4  |4    8    12   16   20   24   28   32   36   40   
   |
5  |5    10   15   20   25   30   35   40   45   50   
   |
6  |6    12   18   24   30   36   42   48   54   60   
   |
7  |7    14   21   28   35   42   49   56   63   70   
   |
8  |8    16   24   32   40   48   56   64   72   80   
   |
9  |9    18   27   36   45   54   63   72   81   90   
   |
10 |10   20   30   40   50   60   70   80   90   100  
   |

Which is what she said "how can i create a space between each horizontal line."

how can i create a space between each horizontal line

Of course you could argue she should have said newline instead of space, but having spaces as below:-

- - - - - - - - - - - - - - - - - -

serves no purpose.

This article has been dead for over six months. Start a new discussion instead.