Having weird issue with setw

On my outputData function my first output "Name: " is coming in at different alignments. while my other outputs are just fine.

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
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

struct President
{
	string first_name;
	string last_name;
	int begin_year;
	int end_year;
	string party_affil;
};

void loadData(President data[], const int SIZE);
int search(President data[], const int SIZE, string search_str);
void outputData(President data[], const int SIZE);
//**********************************************************************
int main()
{
	const int SIZE = 17;
	President data[SIZE];
	string search_str;

	loadData(data, SIZE);
	search(data, SIZE, search_str = "Van Buren");
	search(data, SIZE, search_str = "Polk");
	outputData(data, SIZE);

	return 0;
}
//**********************************************************************
void loadData(President data[], const int SIZE)
{
	fstream inFile;
	inFile.open("prez_data.txt");

	for (int i = 0; i < SIZE; i++)
	{
		data[i].first_name = "";
		data[i].last_name = "";
		data[i].begin_year = 0;
		data[i].end_year = 0;
		data[i].party_affil = "";
	}
	
	for (int i = 0; i < SIZE; i++)
	{
		getline(inFile, data[i].first_name);
		getline(inFile, data[i].last_name);
		inFile >> data[i].begin_year;
		inFile >> data[i].end_year;
		inFile.ignore();
		getline(inFile, data[i].party_affil);
	}

//	for (int i = 0; i < SIZE; i++)
//	{
//		cout << data[i].first_name << endl;
//		cout << data[i].last_name << endl;
//		cout << data[i].begin_year << endl;
//		cout << data[i].end_year << endl;
//		cout << data[i].party_affil << endl;
//		cout << endl;
//	}

}
//**********************************************************************
int search(President data[], const int SIZE, string search_str)
{
	bool location = false;
	int position = 0;
	ofstream outFile;
	outFile.open("a3.txt", ios::app);

	cout << "Search Results: " << endl;
	cout << endl;
	outFile << "Search Results: " << endl;
	outFile << endl;
	
	for (int i = 0; i < SIZE; i++)
	{
		if (data[i].last_name == search_str)
		{
			location = true;
			position = i;
		}
	}
		if (location == true)
		{
			cout << "President: " << data[position].last_name
				<< " Found" << endl;
			cout << endl;
			outFile << "President: " << data[position].last_name
				<< " Found" << endl;
			outFile << endl;
			return 0;
		}
		else
		{
			cout << "President: " << search_str
				<< " Not Found" << endl;
			cout << endl;
			outFile << "President: " << search_str
					<< " Not Found" << endl;
			outFile << endl;
			return -1;
		}
	
	outFile.close();
}
//**********************************************************************
void outputData(President data[], const int SIZE)
{
	ofstream outFile;
	outFile.open("a3.txt", ios::app);

	outFile << "*********************************************" << endl;
	outFile << "President Listing: " << endl;
	outFile << endl;

	for (int i = 0; i < SIZE; i++)
	{
		outFile << "Name: " << setw(7) << right 
				<< data[i].first_name
				<< " " << data[i].last_name << endl;
		outFile << "Begin: " << setw(3) << right  
				<< data[i].begin_year << endl;
		outFile << "End: " << setw(6) << right 
				<< data[i].end_year << endl;
		outFile << "Party: " << setw(3) << right
				<< data[i].party_affil << endl;
		outFile << endl;
	}

	outFile.close();

}


Output Expected:


Name:  George Washington
Begin: 1789
End:   1797
Party: Federalist

Name:  James Madison
Begin: 1809
End:   1817
Party: Democrat Republican

Name:  Millard Fillmore
Begin: 1850
End:   1853
Party: Whig

Name:  Andrew Jackson
Begin: 1829
End:   1837
Party: Democrat


The output I'm getting:


Name:  George Washington
Begin: 1789
End:   1797
Party: Federalist

Name:   James Madison
Begin: 1809
End:   1817
Party: Democrat Republican

Name: Millard Fillmore
Begin: 1850
End:   1853
Party: Whig

Name:  Andrew Jackson
Begin: 1829
End:   1837
Party: Democrat
Last edited on
Do you realize that setw() doesn't add spaces to the next field? The setw() function only works when the number used in the setw() function is larger than the next item to be printed.

if that is the case would I need to a larger setw() space off the first field? Ex: setw(20) or how do I fix this problem?

Because I tried setting a larger gap between the first and second field and I'm still having the same issue.
Last edited on
There are a couple of things here. using the right manipulator doesn't help. The requirement is that the output is aligned to the left.

Second, setw() is used inappropriately. You could get the required output without using setw at all.
However for the sake of illustration, here's how it might be used:
 
outFile << left; // needed just once 


1
2
3
        outFile << setw(7) << "Name:" << data[i].first_name
                << " " << data[i].last_name << endl;
        outFile << setw(7) << "Begin:" << data[i].begin_year << endl;

... and so on.
Thank you so much Chervil! That worked perfectly. I really appreciate the help. Also thanks for letting me know that you only need to declare align once. I was unaware of that.
Topic archived. No new replies allowed.