Formatting output

Hi, this problem has been bothering me for a little while now because I can't seem to locate the problem. My problem is I can't get the formatting for my output to come out perfectly aligned.

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
46
47
48
49
50
51
52
53
54
55
 outfile.width(20);
    outfile << left << "Name";
    outfile.width(20);
    outfile <<  left <<"Test1";
    outfile.width(20);
    outfile <<  left << "Test2";
    outfile.width(20);
    outfile << left <<  "Test3";
    outfile.width(20);
    outfile << left <<  "Test4";
    outfile.width(20);
    outfile << left << "Average" << endl;
    outfile << "-----------------------------------------------------"
    "----------------------------------"<< endl;

    for (i=0; i<n; i++){
        outfile.width(20);
        outfile << left << name[i];

        outfile.width(20);
        if(test1[i]==lowestGrade)
            outfile << right << dropped;
        else{
            outfile.width(20);
            outfile << right << test1[i];
        }

        outfile.width(20);
        if(test2[i]==lowestGrade)
            outfile << right << dropped;
        else{
            outfile.width(20);
            outfile << right << test2[i];
        }

        outfile.width(20);
        if(test3[i]==lowestGrade)
            outfile << right << dropped;
        else{
            outfile.width(20);
            outfile << right << test3[i];
        }

        outfile.width(20);
        if(test4[i]==lowestGrade)
            outfile << right << dropped;
        else{
            outfile.width(20);
            outfile << right << test4[i];

        outfile.setf(ios::fixed,ios::floatfield);
        outfile.precision(2);
        outfile.width(20);
        outfile << right << average[i] << endl;
        }


It keeps coming out a little off (it seems like even though it shouldn't, the length of the name for each student is effecting it). I can't seem to find the problem and looking at other forums hasn't helped. Any suggestions?
Last edited on
How is names defined? How long are the names?


Please show a sample of your output (inside code tags to preserve the formatting).

Also note that there is a manipulator called setw() that you can use with the insertion operator<<.

outfile << setw(20) << dropped;

And remember the right/left manipulator is "sticky" meaning it stays the same until you change it to something else. So I would recommend changing your first output to something like:
1
2
 for (i=0; i<n; i++){
        outfile << left  << setw(20) << name[i] << right;

Then you could remove the "right" from all of your other functions.

Likewise with the ios::fixed, ios::floatfield, these are also "sticky" and only need to be set once.

Names is a string and the longest one is 20.

Name                  Test1          Test2          Test3          Test4          Average        
------------------------------------------------------------------------------
Albert Einstein                    53             75             92             68          72.00
Charless Babbage                   96             72             85             92          86.25
Claude Shannon                     91             86             87             41          76.25
John vonNeumann                    40             93             92             78          75.75
Niklaus Wirth                      59             83             91             67          75.00
Ada Lovelace                       91             84             98             63          84.00
Thomas Bayes                       70             81             92            100          85.75
Sofia Kovalevskaya                 31             46             53             61          47.75
Blaise Pascal                      90             72             80             75          79.25
Grace Hopper                       81             92             73             80          81.50
Isaac Newton                       32             85             93             67          69.25


Thanks for the help!

edit: At the risk of sounding stupid, when I pasted it here it's coming out in straight columns (albeit just not under the right header), but it's really zigging and zagging a little based on the length of the name in column one
Last edited on
Topic archived. No new replies allowed.