Help with <iomanip>

I'm unable to get a properly outlined output of the results when the entire program is run. Could anyone help me with this function or introduce me to another that might do a better job.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 
void Grade()
{
    cout<<"\t\t\tThe results of "<<sName[i]<<" "<<oName[i]<<" of "<<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
    cout<<"\t\t|"<<setw(5)<<"Course"<<"\t      |"<<setw(5)<<"Credits"<<"\t|"<<setw(5)<<"Score"<<"\t   |"<<setw(5)<<"Grade"<<"\t   |"<<setw(5)<<"Grade Point"<<"\t|"<<setw(6)<<"Comment"<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
    for (i=0;i<=noOfCours;i++){
if(totScore[i]>=80 ){
    graD[i]="A";
    graDept[i]=4.0;
    Comment[i]="Excellent";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
    //cout<<"A \t"<<graDept<<endl;
}
else if(totScore[i]>=75 ){
    graD[i]="B+";
    graDept[i]=3.5 ;
    Comment[i]="Very Good";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else if(totScore[i]>=70 ){
    graD[i]="B";
    graDept[i]=3.0;
    Comment[i]="Good";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else if( totScore[i]>=65){
    graD[i]="C+";
    graDept[i]=2.5;
    Comment[i]="Fairly Good";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else if( totScore[i]>=60){
    graD[i]="C";
    graDept[i]=2.0;
    Comment[i]="Average";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else if( totScore[i]>=55){
    graD[i]="D+";
    graDept[i]=1.5;
    Comment[i]="Below Average";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else if( totScore[i]>=50){
    graD[i]="D";
    graDept[i]=1.0;
    Comment[i]="Marginal Pass";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else if(totScore[i]>=45){
    graD[i]="E";
    graDept[i]=0.5;
    Comment[i]="Below Marginal Pass";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else if(totScore[i]>=0){
    graD[i]="F";
    graDept[i]=0.0;
    Comment[i]="Fail";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
else{
    graD[i]="-";
    graDept[i]=0.0;
    Comment[i]="Fail";
    cout<<"\t\t|"<<setw(5)<<Subj[i]<<"      |"<<setw(5)<<Credits[i]<<"\t|"<<setw(5)<<totScore[i]<<"\t|"<<setw(5)<<graD[i]<<"\t   |"<<setw(5)<<graDept[i]<<"\t|"<<setw(6)<<Comment[i]<<setw(6)<<"|"<<endl;
    cout<<"\t\t*"<<setw(5)<<"-------------*"<<setw(5)<<"---------*"<<setw(5)<<"----------*"<<setw(5)<<"------------*"<<setw(5)<<"------------*"<<setw(12)<<"-----------*"<<endl;
}
}
}
That's pretty impossible to read... why don't you give an example of how it should look instead? You can put it between the [output][/output] tags to preserve formatting.
___________________________________________________________
| Subject | Credits | Score | Grade | Grade Point | Comment |
___________________________________________________________
| Calculus | 3 | 83 | A | 4.0 | Excellent |
___________________________________________________________
| Calculus | 3 | 12 | F | 0.0 | Fail |
___________________________________________________________
| Calculus | 3 | 69 | C+ | 2.5 | Fairly Good |
___________________________________________________________
this is how i want it to display the results. sorry about the clumsy code though. its basically some small code that is repeated throughout. :) :D
You should put all those cout statements after your if and else statements.

1
2
3
4
5
6
7
8
9
10
cout << /* headings */ << endl;
if (/* stuff */)
    //stuff
else if (/* stuff */)
    //...
else
{
    //stuff
}
cout << /* your data */ << endl;


One thing I notice is that you're setting all your output fields to a width of 5 characters. I don't think that's correct, seeing as how "Calculus" is 8 characters long and "Fairly Good" is 11 characters long.

Also, your data should line up with the columns, so the field for subject should be at least 7 characters ("Subject"), field for credits should be at least 7 characters ("Credits"), field for score at least 5 characters ("Score"), etc.

Another thing you want to keep in mind is that each line on the console is 80 characters long, so you want to keep your total output at 80 characters or below.

I suggest maybe something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
Subject and Comment are the only things that have data which might
be longer than their headings.  Here I'm giving them a field width of 12
characters while the others get the minimum possible.  Each heading is
separated by " | "
*/
cout << "| " << setw(12) << "Subject" << " | Credits | Score | Grade | GPA | "
     << setw(12) << "Comment" << " |\n"; //63 characters total length
//stuff
cout << "| " << setw(12) << /* subject */ << " | " << setw(7) << /* credits */
     << " | " << setw(5) << /* score */ << " | " << setw(5) << /* GPA */
     << " | " << setw(12) << /* comment */ << " |\n";
cout << string(63, '-') << endl; //don't need to actually type out 63 dashes 
Topic archived. No new replies allowed.