Formatting my table...

Here is my code:
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Programmer:  G**** E******
// Program:     Program Assignment 1
// Class:       152, Fall, 2010
// Compiler:    Bloodshed DevC++
// Date:        Oct 1, 2010
// Filename:    E******G****Assignment2.cpp

#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

void printHeading ();
string getStatus (int, double, double &, double &);
void printSummary (int, double, double, double, double, double);
int getQualPts (char);
int getHoursPassed (char, int);

int  main()
{
     ifstream in ("SemGrades.txt");
     string lastName = "?",           
            firstName = "?",          
            iD = "?",                 
            status = "?";               
     int students = 0,                  
         hours = 0,                  
         attHours = 0,               
         totalHoursAttempted = 0,      
         hoursPassed = 0,
         studentHoursPassed = 0,              
         totalHoursPassed = 0,         
         qualPts = 0,
         totalQualPts = 0;

     char letterGrade = '?';            
     double GPA = 0.0,                
            semesterGPA = 0.0,          
            totalGPA = 0.0,
            honors = 0,
            probation = 0;             
            
     if (in.fail())                   
        cout << "Can't open input file \n" << endl;      
     else cout << "Input file is open \n" << endl;    
     
     printHeading ();                  
     
while (in.peek() != EOF)              
{    
       attHours = 0;
       qualPts = 0;
       studentHoursPassed = 0;
       
       in >> lastName >> firstName >> iD;       
       students++;                          

         while (in.peek() != '\n')              
{
             in >> hours >> letterGrade;          
             
             qualPts += hours * getQualPts(letterGrade); 
              
             hoursPassed = getHoursPassed(letterGrade, hours);
             
             studentHoursPassed += hoursPassed;
             
             attHours += hours;                   
}
             totalHoursPassed += studentHoursPassed;

             totalQualPts += qualPts;
             
             totalHoursAttempted += attHours;     
             
             GPA = static_cast<double>(qualPts) / attHours;
             
             status = getStatus(studentHoursPassed, GPA, honors, probation);
             
             cout << lastName << " "
                  << setw(1) << firstName 
                  << setw(18) << iD
                  << setw(5) << attHours 
                  << setw(7) << studentHoursPassed
                  << setw(7) << qualPts;
             cout << fixed << showpoint << setprecision(3);
             cout << setw(7) << GPA
                  << setw(20) << status << "\n";

       in.ignore(1, '\n');
}
       double averageSemesterGPA = static_cast<double>(totalQualPts) / totalHoursAttempted;

    printSummary (students, totalHoursAttempted, totalHoursPassed, averageSemesterGPA, honors, probation);

    system("pause");
    return 0;
}

void printHeading ()
{    
        cout << setw(46) << "Semester Grade Report\n\n";
                
        cout << setw(37) << "Att" 
             << setw(10) << "Passed" 
             << setw(6) << "Qual" 
             << setw(17) << "Academic\n";
             
        cout << "Student Name" 
             << setw(20) << "Student ID" 
             << setw(7) << "Hours" 
             << setw(7) << "Hours" 
             << setw(6) << "Pts" 
             << setw(6) << "GPA" 
             << setw(11) << "Status\n\n";
}

string getStatus (int hoursPassed, double GPA, double & honors, double & probabtion)
{
     string status = "?";
     
     if (GPA == 4.0 && hoursPassed >= 12)
{
        status = "President's List";
        honors++;
        return status;
}
     if (3.5 <= GPA && GPA <= 3.999 && hoursPassed >= 12)
{
     
        status = "Dean's List";
        honors++;
        return status;
}
     else if (GPA < 2.0)
{
        status = "Probation";
        probabtion++;
        return status;
}
     else  status = "     ";
     

}

void printSummary (int students, double totalHoursAttempted, double totalHoursPassed, double avgSemGPA, double honors, double probation)
{     
    double averageHours = totalHoursAttempted;
    double averageHoursPassed = totalHoursPassed;
    
    averageHours /= students;
    averageHoursPassed /= students;
    honors = (honors / students) * 100;
    probation = (probation / students) * 100;
    
     cout << "\nSUMMARY STATISTICS\n";
     cout << "Total # students enrolled: " << students << endl;
     cout << fixed << showpoint << setprecision(1);
     cout << "Average # hours attempted: " << averageHours << endl;
     cout << setw(27) << "Average # hours passed: " << averageHoursPassed << endl;
     cout << fixed << showpoint << setprecision(3);
     cout << setw(27) << "Average GPA: " << avgSemGPA << endl;
     cout << fixed << showpoint << setprecision(1);
     cout << setw(27) << "Students with Honors: " << honors << "%\n";
     cout << setw(27) << "Students on Probation: " << probation << "%\n\n";
}

int getQualPts (char letter)
{
      int QualPts = 0;
       
       switch (letter)
{
       case 'A' : QualPts = 4;
       break;
       case 'B' : QualPts = 3;
       break;
       case 'C' : QualPts = 2;
       break;
       case 'D' : QualPts = 1;
       break;
       case 'F' : QualPts = 0;
       break;
       default : QualPts = 0;
}
       return QualPts;
}

int getHoursPassed (char letter, int hours)
{
        int hoursPassed = 0;
       
       switch (letter)
{
       case 'A' :
       case 'B' : 
       case 'C' : 
       case 'D' :
       return hoursPassed = hours;
       
       default : hoursPassed = 0;
}
       return hoursPassed;
}


My output is has everything I need for the assignment, but I have racked my brain trying to figure out how to get the "printHeading()" function to have equal distances between the names---> ID---> hours, etc.

Is it that I have to find the length of the strings, and go from that? It confuses me a little and was wondering if someone could point me in the right direction.

And a preemptive thank you to any help.


I would post a picture of the output, but I don't know how =(.
Whats wrong with using the space bar.
ie;
1
2
3
4
5
6
7
 cout << "Student Name" 
             << setw(20) << "Student ID" 
             << setw(7) << "Hours  " 
             << setw(7) << "Hours          " //<--press space bar until lined up or adequate space etc
             << setw(6) << "Pts  " 
             << setw(6) << "GPA" 
             << setw(11) << "Status\n\n";
closed account (D80DSL3A)
Are you just trying to put an equal # of spaces between words?
If so, forget setw() and print spaces.
1
2
3
4
5
6
7
cout << "Student Name" 
             << "     " << "Student ID" // 5 blank spaces between each word
             << "     " << "Hours" 
             << "     " << "Hours" 
             << "     " << "Pts" 
             << "     " << "GPA" 
             << "     " << "Status\n\n";

Haha, is my face red.

This is how fried my brain is, I have the printHeading() set up right, but its the lines (83-91) where it repetitively prints the data from the file.

1
2
3
4
5
6
7
8
9
 cout << lastName << " "
                  << setw(1) << firstName 
                  << setw(18) << iD
                  << setw(5) << attHours 
                  << setw(7) << studentHoursPassed
                  << setw(7) << qualPts;
          cout << fixed << showpoint << setprecision(3);
          cout << setw(7) << GPA
                  << setw(20) << status << "\n";


It prints out all jagged.



Cleese, John   111-11-111  12  11  21  1.750  probation
Idle, Eric   222-22-222  14  11  32  2.286
Palin, Michael  333-33-333 6  6  21  3.5000
Jones, Terry   444-44-444  12  15  44  2.933


And so on.

Any thoughts??
Topic archived. No new replies allowed.