Output format Printf and Cout!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std ;

typedef struct{
        char name[20];
        char surname[30] ;
        int id ;
}Record ;

void displayPrintf( const Record *rList  , int size )
{
     printf( "DISPLAY WITH PRINTF\n\n" );
     printf( "%-20s %-20s %5s\n" , "Name" , "Surname" , "Id" );
     printf( "%-20s %-20s %5s\n" , "----" , "-------" , "--" );
     
     for( int i = 0 ; i < size  ; i++ )
        printf( "%-20s %-20s %5d\n" , rList[i].name , rList[i].surname , rList[i].id );
}

void displayCout( const Record *rList , int size )
{
     cout << "DISPLAY WITH COUT\n\n"  << endl ;
     cout << setw( -20 ) << "Name" << setw( 20 ) << "Surname"  << setw( 5 ) << "Id" << endl;
     cout << setw( -20 ) << "----" << setw( 20 ) << "-------"  << setw( 5 ) << "--" << endl;
     
     for( int i = 0 ; i < size ; i++ )
          cout << setw( -20 ) << rList[i].name << setw( 20 ) << rList[i].surname << setw( 5 ) << rList[i].id << endl;
}
int main( void )
{
    Record a[] = { {"John" , "McCavin" , 2 } ,
                   {"George","Washington" , 1 } ,
                   {"Steve" ,"Carter" , 3 }};
    
    displayPrintf( a  , 3 );
    cout << endl;
    displayCout( a , 3 );
                                          
    system( "PAUSE");
    return ( 0 ) ;
}

I tried to make output format of cout similar to output format of printf.
Any solution ?
Last edited on
Ok.I found a solution.
1
2
3
4
     for( int i = 0 ; i < size ; i++ )
          cout  << setw( 18 ) << left << rList[i].name << setw( 20 ) << left << rList[i].surname 
		  << left << setw( 15 ) <<  rList[i].id << endl;
		  //cout <<  setw( -20 ) << rList[i].name   << setw( 20 ) << rList[i].surname << setw( 15 ) << rList[i].id << endl; 

Topic archived. No new replies allowed.