how to print this format??

closed account (o2TCpfjN)
the format like this

=====================================================
|| Record || Student ID || Name      || Programme || Year ||
=====================================================
|| 1    || 1       || a        || a       || 1   ||
=====================================================

Student ID , Name ,Programme and Year need the user input
we do not know the size of the data
how to do this????
Last edited on
Try setw() and setfill() manipulators
closed account (o2TCpfjN)
if the data do not same the space is not same too
the format is fixed
such as
=====================================================
|| Record || Student ID || Name      || Programme || Year ||
=====================================================
|| 1    || 11111111  || aaaaaaaaaaaa || aaaaaaaaa || 1   ||
=====================================================

the '||' are in the same position
Just a sample:
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
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main()
{
     int Record, ID, Year;
     string Name, Programme;

     //Just random values, edit them to see what happens
     Record = 5;
     ID = 123456;
     Name = "Hello World";
     Programme = "Something";
     Year = 2;

     cout << setw(70) << setfill('=') << '\n';
     cout << "|| Record  || Student ID  || Name         || Programme     || Year ||\n";
     cout << setw(70) << '\n';
     cout.setf(ios_base::left,ios_base::adjustfield);
     cout << "||" << setw(9) << setfill(' ') << Record << "||";
     cout << setw(13) << ID << "||";
     cout << setw(14) << Name << "||";
     cout << setw(15) << Programme << "||";
     cout << setw(6) << Year << "||\n";
     cout.setf(ios_base::right,ios_base::adjustfield);
     cout << setw(70) << setfill('=') << '\n';
     return 0;
}
closed account (o2TCpfjN)
thank you very much

but what is the meaning of
'cout.setf(ios_base::left,ios_base::adjustfield);' ??

my teacher had not teach this one
any statement can instead it???
cout.setf();
sets the alignment for setw():

1
2
3
4
5
6
7
cout.setf(ios_base::left,ios_base::adjustfield);
cout << setw(8) << setfill('_') << "abc";
//output: abc_____

cout.setf(ios_base::right,ios_base::adjustfield);
cout << setw(8) << setfill('_') << "abc";
//output: _____abc 


I think you could use it just like this: cout.setf(ios_base::left);

(Edit) For another manipulator to do that, see http://www.cplusplus.com/forum/beginner/5880/#msg26290
Last edited on
Topic archived. No new replies allowed.