Lining up Data

Lines 220-233 are outputing data in the two rows like
xxxxx xxxxx
xxxxx xxxxx
and so forth. The problem I'm having is lining up the second column of data. I tried using setw, but because the last names in the data are different lengths, the data never lines up correctly.
Lines 120-135 are the ShowMe functions I'm using to display the data.
Any help is much appreciated

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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
#include <cctype>
#include <cstring>
using namespace std;
void pause();

class Info
{
	private:
	char First[13];
	char Last[13];
	char ID[12];
	char Date[9];
	float Pay;
	float Hours;


	public:
    //Class ctors
    Info()//default constructor
    {
        //cout << "\n\n\tDefault Info constructor was called..\n\n";

        strncpy(First,"Unknown",13);
        strncpy(Last,"Unknown",13);
		strncpy(ID,"Unknown",12);
		strncpy(Date,"Unknown",9);
        Pay = 0.0f;
        Hours = 0.0f;
    }

    Info(char* fn, char* ln, char* id, char* dHired, float pRate, float hWorked)//overloaded constructor
    {
        cout << "\n\n\tOverloaded cPerson constructor was called..\n\n";
        /*setFirstName(fn);
        setLastName(ln);
        setIDN(id);
		setdateHired(dHired);
		setpayRate(pRate);
		sethoursWorked(hWorked);*/

		strncpy(First,fn,13);
        strncpy(Last,ln,13);
		strncpy(ID,id, 12);
		strncpy(Date,dHired,9);
		Pay = pRate;
		Hours = hWorked;
    }
	Info(Info&  rhs)//copy constructor
    {
        cout << "\n\tCopy constructor called..\n";
        strncpy(this->First,rhs.First,13);
        strncpy(this->Last,rhs.Last,13);
		strncpy(this->ID,rhs.ID,12);
		strncpy(this->Date,rhs.Date,9);
        this->Pay = rhs.Pay;
		//this->Hours = rhs.Hours;
		(*this).Hours = rhs.Hours;
    }
    //Class dtor
    ~Info()//destructor
    {


    }
    char * getFirstName(void) { return this->First; }
	void setFirstName(char * fn)
    {
		strncpy(this->First,fn,13);
        return;
    }
	char * getLastName(void) { return Last; }
    void setLastName(char * ln)
    {
        strncpy(Last,ln,13);
        return;
    }
	char * getIDNumber(void) {return ID;}
	void setIDNumber(char * id)
	{
		strncpy(ID,id,12);
		return;
	}
	char * getDateHired(void) {return Date;}
	void setDateHired(char * dHired)
	{
		strncpy(Date,dHired,9);
		return;
	}

	float getPayRate(void) const { return Pay; }
    void setPayRate(float pRate)
    {
        Pay = pRate;
        return;
    }

	int getHoursWorked(void) const { return Hours; }
    void setHoursWorked(int hWorked)
    {
       	Hours = hWorked;
        return;
    }

	string Name()
	{
		stringstream full;
		full<<Last<<", "<<First;
		return full.str();
	}

	void ShowMe(ostream& target)
    {
        target << First << " " <<Last<< endl
         	   << Date << endl
          	   << ID << endl
			   << Hours << " @ " << Pay << endl << endl;
        return;
    }
    void ShowMe(ostream& target, Info & E)
    {
        target <<First << " " <<Last<< setw(20)<<E.First << " " <<E.Last<<endl
         	   << Date << setw(25)<<E.Date<<endl
          	   << ID << setw(25)<<E.ID<<endl
			   << Hours << " @ " << Pay << setw(25)<<E.Hours << " @ " << E.Pay<< endl << endl;
        return;
    }
	string DateHired()
	{
	    string temp;
	    temp = Date;
	    temp.insert(2,"/",1);
	    temp.insert(5,"/",1);
	    return temp;
	}
    friend ostream & operator << (ostream & lhs, Info & rhs)
    {
        lhs << rhs.First<<endl
			<< rhs.Last<<endl
			<<rhs.ID<<endl
			<<rhs.Date<<endl
			<<rhs.Hours<<" @ "
			<<rhs.Pay<<endl;
        return lhs;
    }

	float GrossPay();
	void Report();
};

float average(float , int);
string newdate(string );
int main()
{
    fstream BinIn;
    fstream DataFile;
    Info Employees[100];
    int sub=0;
	float TotalGross = 0.0f;
	float AvgGross;


    BinIn.open(".\\Datafiles\\Lab6B.bin",ios::in|ios::binary);
    if(BinIn.fail())
    {
        cout<<"Unable to Process\n";
        return 999;
    }
    while(BinIn.eof() == false)
    {
        BinIn.read(reinterpret_cast<char *>(&Employees[sub]),sizeof(Info));
        sub++;
    }
    int maxElements = sub-1;
	cout<<setw(50)<<"Bay West Industries"<<endl
		<<setw(47)<<"Payroll Report"<<endl<<endl;

	cout<<left<<setw(26)<<"Employee"
        <<setw(12)<<"Date Hired"
        <<setw(14)<<"ID"
        <<setw(7)<<"Hours"
        <<setw(7)<<"Rate"
        <<setw(12)<<"Grosspay"<<endl;


    cout<<setfill('-')
        <<setw(78)<<'-'<<endl;
    cout<<setfill(' ');

    for(int index=0; index<maxElements; index++)
    {

		Employees[index].Report();

		cout<<setw(10)<<showpoint<<Employees[index].GrossPay();
		cout<<endl;
		TotalGross = TotalGross + Employees[index].GrossPay();
    }


	cout<<setfill('-')
        <<setw(78)<<'-'<<endl;
    cout<<setfill(' ');

	cout<<setw(65)<<"Total Employees: "<<maxElements<<endl
		<<setprecision(2)<<fixed
		<<setw(65)<<"Total Gross Pay: "<<TotalGross<<endl
		<<setw(66)<<"Average Gross Pay:  "<<average( TotalGross, maxElements)<<endl<<endl
		<<setw(57)<<"Report Prepared By: Stephanie Precht"<<endl<<endl;
    pause();
    DataFile.open(".\\DataFiles\\employeeInfo.txt",ios::out);
    for(int index=0; index<maxElements; index++)
    {
        if(maxElements-index==1)
        {
            Employees[index].ShowMe(cout);
            Employees[index].ShowMe(DataFile);
        }
        else
        {
            Employees[index].ShowMe(cout, Employees[index+1]);
            Employees[index].ShowMe(DataFile, Employees[index+1]);
        }
        index++;
    }
	pause();
	cout<<Employees[maxElements-1];
    pause();
}
float average(float Total, int maxElements )
{

	return Total / maxElements;

}
float Info::GrossPay()
{
	return Hours * Pay;
}
void Info::Report()
{
        cout<<left<<setw(26)<<Name()
			<<setw(12)<<DateHired()
        	<<setw(15)<<ID
			<<setprecision(1)<<fixed
        	<<setw(3)<<Hours
        	<<right<<setprecision(2)<<setw(7)<<Pay;
		return;
        	//<<setw(10)<<showpoint<<gross<<endl<<endl;
		//TotalGross = TotalGross + gross;
}




void pause()
{
	do
	{
		printf("\n%s","Press any key..");
	}while(!getch());
    printf("\n\n");
	return;
}
Maybe try something like this:
1
2
3
4
5
6
7
8
9
10
    void ShowMe(ostream & target, const Info & E)
    {
        target << left;
        target << setw(26) << wholename() << E.wholename() << '\n'
               << setw(26) << Date        << E.Date        << '\n'
               << setw(26) << ID          << E.ID          << '\n';
        ostringstream ss;
        ss << Hours << " @ " << Pay;
        target << setw(26) << ss.str()    << E.Hours << " @ " << E.Pay << "\n\n";
    }

where wholename() is a private member function which concatenates
"First + ' ' + Last" and returns the result as a std::string.
(Alternatively, handle the names in a similar fashion to hours @ pay).
Last edited on
This works perfectly, thank you so much for your help!
Topic archived. No new replies allowed.