How do you make letter grades with plus/minus line up

so i have my program completed i just cant line up the letter grades at line 145. it looks like this


ID Course Letter
Grade Grade
1 80.50 B-
2 27.50 F
3 97.52 A+
4 96.51 A
5 80.64 B-
6 89.18 B+
7 73.84 C
8 91.48 A-
9 73.50 C
10 77.60 C+
11 78.48 C+
12 90.21 A-

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
 #include<fstream>
#include<string>
#include<iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
    ifstream sampleGrades("sampleGrades.txt");
    ofstream outputGrades("outputGrades.txt");
    
    string firstLine;
    int TotalStudents;
    
    int ID = 0;
    double Attendance, hw1, hw2, hw3, hw4, hw5, hw6, hw7, exam1, exam2, finalExam;
    
    double GradePercent;
    
    double totalAp = 0, totalA = 0, totalAm = 0, AvgAttendanceA = 0.0;
    int countAp = 0, countA = 0, countAm = 0, AcountTot = 0;
    
    double totalBp = 0, totalB = 0, totalBm = 0, AvgAttendanceB = 0.0;
    int countBp = 0, countB = 0, countBm = 0, BcountTot = 0;
    
    double totalCp = 0, totalC = 0, totalCm = 0, AvgAttendanceC = 0.0;
    int countCp = 0, countC = 0, countCm = 0, CcountTot = 0;
    
    double totalD = 0, AvgAttendanceD = 0.0;
    int countD = 0, DcountTot = 0;
    
    double totalF = 0, AvgAttendanceF = 0.0;
    int countF = 0, FcountTot = 0;
    
    int NumA = 0;
    int NumB = 0;
    int NumC = 0;
    int NumD = 0;
    int NumF = 0;
    
    string LetterGrade = "";
    
    if (sampleGrades.is_open())
    {
        getline(sampleGrades, firstLine);
        
        sampleGrades >> TotalStudents;
        cout << "Number of students: " << TotalStudents << endl;
        
        cout <<         "Letter  Count  Avg. Attendance"<< endl;
        
        outputGrades << "ID  Course  Letter" << endl;
        outputGrades << "    Grade   Grade" << endl;
        
        
        
        while (!sampleGrades.eof())
        {
            sampleGrades >> ID >> Attendance >> hw1 >> hw2 >> hw3 >> hw4 >> hw5 >> hw6 >> hw7 >> exam1 >> exam2 >> finalExam;
            
            LetterGrade = "Error: This is wrong!!";
            
            GradePercent = 100*(Attendance*0.10/25 + (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7)/700*0.35 + exam1/25*0.15 + exam2/25*0.15 + finalExam/40 * 0.25);
            
            if (GradePercent >= 97 && GradePercent <= 100)
            {
                LetterGrade = " A+ ";
                NumA++;
                totalAp += Attendance;
                countAp++;
            }
            else if (GradePercent >= 93 && GradePercent < 97)
            {
                LetterGrade = " A ";
                NumA++;
                totalA += Attendance;
                countA++;
            }
            else if (GradePercent >= 90 && GradePercent < 93)
            {
                LetterGrade = " A- ";
                NumA++;
                totalAm += Attendance;
                countAm++;
            }
            else if (GradePercent >= 87 && GradePercent < 90)
            {
                LetterGrade = " B+ ";
                NumB++;
                totalBp += Attendance;
                countBp++;
            }
            else if (GradePercent >= 83 && GradePercent < 87)
            {
                LetterGrade = " B ";
                NumB++;
                totalB += Attendance;
                countB++;
            }
            else if (GradePercent >= 80 && GradePercent < 83)
            {
                LetterGrade = " B- ";
                NumB++;
                totalBm += Attendance;
                countBm++;
            }
            else if (GradePercent >= 77 && GradePercent < 80)
            {
                LetterGrade = " C+ ";
                NumC++;
                totalCp += Attendance;
                countCp++;
            }
            else if (GradePercent >= 73 && GradePercent < 77)
            {
                LetterGrade = " C ";
                NumC++;
                totalC += Attendance;
                countC++;
            }
            else if (GradePercent >= 70 && GradePercent < 73)
            {
                LetterGrade = " C- ";
                NumC++;
                totalCm += Attendance;
                countCm++;
            }
            else if (GradePercent >= 60 && GradePercent < 70)
            {
                LetterGrade = " D ";
                NumD++;
                totalD += Attendance;
                countD++;
            }
            else if (GradePercent >= 0 && GradePercent < 60)
            {
                LetterGrade = " F ";
                NumF++;
                totalF += Attendance;
                countF++;
            }
            
            outputGrades << ID << setw(8) << setprecision(2) << fixed << GradePercent << setw(8) <<LetterGrade << endl;
            
            AcountTot = countAp + countA + countAm;
            BcountTot = countBp + countB + countBm;
            CcountTot = countCp + countC + countCm;
            DcountTot = countD;
            FcountTot = countF;
            
            AvgAttendanceA = (totalAp + totalA + totalAm )/ (countAp + countA + countAm);
            AvgAttendanceB = (totalBp + totalB + totalBm) / (countBp + countB + countBm);
            AvgAttendanceC = (totalCp + totalC + totalCm) / (countCp + countC + countCm);
            AvgAttendanceD = totalD / countD;
            AvgAttendanceF = totalF / countF;
            
        }
        
        
        cout << "  A" << setw(10) << AcountTot << setw(8) << AvgAttendanceA << endl;
        cout << "  B" << setw(10) << BcountTot << setw(10) << setprecision(2) << fixed << AvgAttendanceB << endl;
        cout << "  C" << setw(10) << CcountTot << setw(10) << AvgAttendanceC << endl;
        cout << "  D" << setw(10) << DcountTot << setw(10) << AvgAttendanceD << endl;
        cout << "  F" << setw(10) << FcountTot << setw(10) << AvgAttendanceF << endl;
        
        sampleGrades.close();
        outputGrades.close();
    }
    else
        cout << "Unable to open file";
    
    return 0;
}
Last edited on
Could you use find (from <algorithm>) to check if the grade contains a '+' or '-' character, then adjust the setw amount accordingly?
1
2
3
4
5
6
7
8
unsigned int letterGradePadw = 0;
if((find(LetterGrade.begin(), LetterGrade.end(), '-') != LetterGrade.end()) || 
   (find(LetterGrade.begin(), LetterGrade.end(), '+') != LetterGrade.end()) )
   letterGradePadw = 8;
else
   letterGradePadw = 7;

outputGrades << setprecision(2) << ID << setw(8) << setprecision(2) << fixed << GradePercent << setw(letterGradePadw) <<LetterGrade << endl;


Or you could try using a regex to search for the + or -.
hmm. go through my input.txt file and find which are A A- ? there are 137 students that might take awhile lol . I thought there might be a specific simple code like a setprecion that would line all the letters up from the left hand side. because right now they are lined up from the right hand side so its jagged ya know.
Last edited on
The easiest thing to do would be to change your string literals so that they're actually in the right format for what you want to do.

For instance, grades without a + or - should be in the format : "C "
and grades with a + or - should be in the format "C+" or "C-"
Topic archived. No new replies allowed.