Issue with column alignment

Nov 24, 2018 at 8:48pm
I am working on a program for my class, and am having trouble getting the columns to print correctly. Here is my relevant 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
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct studentType
{
	string studentFName;
	string studentLName;
	int testScore;
	char grade;
};
studentType students[20];
ifstream studentDataInput;
ofstream studentDataOutput;

void inputData(int, studentType students[], ifstream&);
void letterGrade(int, studentType students[], char&);
void printstudentData(int, studentType students[]);
void highestScore(int, studentType students[], int);
void highestGrades(int, studentType students[], ofstream&);

int main()
{
	int i = 0;
	int highScore = 0;
	studentDataInput.open("StudentDataInput.txt");
	studentDataOutput.open("StudentDataOutput.txt");
	inputData(i, students, studentDataInput);
	letterGrade(i, students, students[i].grade);
	printstudentData(i, students);
	highestScore(i, students, highScore);
	highestGrades(i, students, studentDataOutput);
	system("pause");
	return 0;
}

void printstudentData(int j, studentType students[])
{
	studentDataOutput << setw(12) << "Student Name" << setw(25) << "Test Score" << setw(15) << "Grade" << endl;
	for (j = 0; j < 20; j++)
	{
		studentDataOutput << students[j].studentLName << ", " << setw(18) << left << students[j].studentFName;
		studentDataOutput << setw(20) << left << setfill(' ') << students[j].testScore;
		studentDataOutput << setw(5) << students[j].grade << endl;
	}
	studentDataOutput << endl;
}



Student Name               Test Score          Grade
Donald, Duckey            85                  B    
Goofy, Goof              89                  B    
Balto, Brave             93                  A    
Smitn, Snow              93                  A    
Wonderful, Alice             89                  B    
Akthar, Samina            85                  B    
Green, Simba             95                  A    
Egger, Donald            90                  A    
Deer, Brown             86                  B    
Jackson, Johny             95                  A    
Gupta, Greg              75                  C    
Happy, Samuel            80                  B    
Arora, Danny             80                  B    
June, Sleepy            70                  C    
Cheng, Amy               83                  B    
Malik, Shelly            95                  A    
Tomek, Chelsea           95                  A    
Clodfelter, Angela            95                  A    
Nields, Allison           95                  A    


The output I am getting shows the test scores out of alignment if the last name is longer. If I setw for the last name, there are extra spaces between the last name and the comma.

I'd need the columns to align up for Test Scores & Grades. I don't know how to do vectors so I wouldn't understand suggestions for that (read briefly about them while trying to research this problem.)
Last edited on Nov 24, 2018 at 8:54pm
Nov 24, 2018 at 9:39pm
Concatenate the first and last names with the comma and then print that with a given field width. And remember that the next column should be right-justified since it's numeric.
1
2
    studentDataOutput << left << setw(30)
        << (students[j].studentLName + ", " + students[j].studentFName);

Nov 24, 2018 at 10:04pm
This worked, thanks a ton!!!
Topic archived. No new replies allowed.