How to print 3 arrays in column format

trying to print this in column format and i'm struggling
this prints like this now                     this is how i want it to print
Employee Name                             employee name          employee Id
joe                                                 joe                                   3
mike                                               mike                                4

employee id
3
4
cout <<"Employee Name";
         cout << endl;
         for(int i = 0; i < 7; i++)
         {        
         cout << employee_names[i] << endl;
         }
         cout << endl;
         cout <<"Employee ID";
         for(int i = 0; i < 7; i++)
         {
         cout << empid[i] << endl;
         }
         cout << endl;

I just can't seem to understand how arrays print out and how to manipulate data the way I want
WestonGroupAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:

cout << left << setw(20) << "Employee name" << setw(15) << "Employee ID" << endl;
for (int i = 0; i < 7; ++i) {
    cout << left << setw(20) << employee_names[i] << setw(15) << empid[i] << endl;
}

Open in new window

0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi WestonGroup,

Printing tab characters (\t) between items is the easiest, though it doesn't always align exactly as you might want.

And then there's the issue of left/right aligning data within its own field.  Text is usually left aligned, integers right aligned, and real numbers align on the decimal point.

The C output routines are very good at it, but that's considered 'throwback'

  fprintf (stdout, " %-20s     %4d\n", TextValue, IntegerValue);


Good Luck,
Kent
0
 
jkrCommented:
Use <iomanip>, e.g.


#include <iomanip>
using namespace std;
 
cout  << setw(16) <<"Employee Name" << setw(16) <<"Employee ID";
         cout << endl;
         for(int i = 0; i < 7; i++)
         {        
         cout  << setw(16) << employee_names[i]  << setw(16) empid[i] << endl;
         }
 
 
// play with '16' to suit your needs

Open in new window

0
 
jkrConnect With a Mentor Commented:
Ooops, that should have been
#include <iomanip>
using namespace std;
 
cout  << setw(16) <<"Employee Name" << setw(16) <<"Employee ID";
         cout << endl;
         for(int i = 0; i < 7; i++)
         {        
         cout  << setw(16) << employee_names[i]  << setw(16) << empid[i] << endl;
         }
 
 

Open in new window

0
 
Deepu AbrahamR & D Engineering ManagerCommented:
cout <<"Employee Name";
cout<<"\t";
cout <<"Employee ID";
cout<<endl;
for(int i = 0; i < 7; i++)
 {        
         cout << employee_names[i] ;
         cout<<"\t"; //Tab seperated
        cout << empid[i] << endl;
 }

Does it help?
Best Regards,
DeepuAbrahamK
0
All Courses

From novice to tech pro — start learning today.