text alignment

can someone tell me how i can fix my text alignment for test scores and grade? i want them in a column, but they are all out of line.
This is how my output text file is aligning.
http://s193.photobucket.com/user/skhjr/media/studentout_zpsgfr3zm82.png.html
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
 #include <iostream>
#include <string>
#include <fstream>
#include <iomanip> 

using namespace std; 

// struct for student
struct studentType {
	string studentFName;
	string studentLName;
	int testScore; 
	char grade;
};

// global 
studentType Student[20];     
ifstream myFile;
ofstream outFile;


// function define area
void read();
void assignGrade (); 
int HighestScore(studentType [], int listSize); 
void print(); 

 


int main()
{
	myFile.open("student.txt");
	outFile.open("output.txt");
	read();
	assignGrade();
	print(); 
	
	

	myFile.close();
	outFile.close(); 
	
	system("pause");
	return 0;
} 



// function implementation area
void read() {
	int i;
	for(i = 0; i < 20; i++) 
	{
		myFile >> Student[i].studentFName >> Student[i].studentLName >> Student[i].testScore; 
	//	cout<< "test!!!!!!" << Student[i].studentLName << endl;
	//	cout << Student[i].studentFName << " " << Student[i].studentLName << " " << Student[i].testScore << endl;      //works
	}

	
}

void assignGrade() {
	int i;
	for(i=0; i < 20; i++)
	
		if(Student[i].testScore >= 90)
			Student[i].grade = 'A';
		else if (Student[i].testScore >= 80)
			Student[i].grade = 'B';
		else if (Student[i].testScore >= 70)
			Student[i].grade = 'C';
		else if (Student[i].testScore >= 60)
			Student[i].grade = 'D';
		else
			Student[i].grade = 'F';
}

int HighestScore(studentType Student[], int listSize) 
{
	int  maximum = Student[0].testScore;
       for(int i=0; i<20; i++){

		   if(Student[i].testScore > maximum) {
			   maximum = Student[i].testScore;
			   
          }
    }
	   return maximum; 
 }
	
	
void print() 
{ 
cout<<left<<setw(30)<<"Student Name"<<right<<setw(10)<<"TestScore"<<right<<setw(7)<<"Grade"<<endl;
int maximum; 
maximum = HighestScore (Student,20);
int i; 
     for (i=0; i < 20; i++)
	 {
	cout << left << Student[i].studentLName << ", " << Student[i].studentFName;
	cout << setw(25) << right << Student[i].testScore  << setw(7) << Student[i].grade << endl;
	 }	

cout<< '\n' <<"Highest Test Score: "<< maximum <<endl;
cout << "The students with the highest test score are: " << endl;
for (int i=0; i < 20; i++)
	{
   if (Student[i].testScore == maximum )
	cout << Student[i].studentLName << ", " << Student[i].studentFName << endl; 
   }
} 
Last edited on
1
2
3
4
5
    for (i = 0; i < 20; i++)
    {
        cout << left << setw(30) << Student[i].studentLName + ", " + Student[i].studentFName;
        cout << setw(10) << right << Student[i].testScore << setw(7) << Student[i].grade << '\n';
    }
Everything you print must have a specified width. You are not printing names with a width.

In order to do that, you must concatenate the names into a single string.

1
2
3
4
  cout << left  << setw(30) << (Student[i].studentLName+", "+Student[i].studentFName);
  cout << right << setw(10) << Student[i].testScore;
  cout << right << setw(7)  << Student[i].grade;
  cout << endl;

Hope this helps.
Topic archived. No new replies allowed.