Array Structures
Need some help with this program i'm working on. I have to define an array of structures for up to 50 employees containing the fileds for name, age, ssn, hourly wage, and years with the company. I have to do the following:
a) write statements that display the name and number of years with the company for the 25th employee.
b) a loop that, for every employee, adds 1 to the number of years with the company and that adds 50 cents to the hourly wage.
Here's my program. It compiles but the output is garbage. I'm trying to make the information for the 26 employees print out. For a and b above, i think i have the code written (it's commented out at the bottom of the program).
Any help will be appreciated.
//File: Asignment6.cpp
//Description: Addresses, Pointers, and Arrays
//Programmer: Luis Ortega
// Date: 2/25/06
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
const int NUMRECS = 26;
struct record1
{
int employee[26];
string name;
int age;
int ssn;
double hwage;
int years;
};
int num =25;
int main()
{
int i;
record1 employee[NUMRECS] = {
("ALPHA, A.", 16, 000000000, 04.00, 1),
("BRAVO, B.", 17, 011111111, 04.50, 2),
("CHARLIE, C.", 18, 02222222, 05.00, 3),
("DELTA, D.", 19, 033333333, 05.50, 4),
("ECHO, E.", 20, 044444444, 06.00, 5),
("FOX, F.", 21, 055555555, 06.50, 6),
("GOLF, G.", 22, 066666666, 07.00, 7),
("HOTEL, H.", 23, 077777775, 07.50, 8),
("INDIA, I.", 24, 888888888, 08.00, 9),
("JULIET, J.", 25, 999999999, 08.50, 10),
("KILO, K.", 26, 111111111, 09.00, 11),
("LIMA, L.", 27, 122222222, 09.50, 12),
("MIKE, M.", 28, 133333333, 10.00, 13),
("NOVEM, N.", 29, 144444444, 10.50, 14),
("OSCAR, O.", 30, 155555555, 11.00, 15),
("PAPA, P.", 31, 166666666, 11.50, 16),
("QUEBEC, Q.", 32, 177777777, 12.00, 17),
("ROMEO, R.", 33, 188888888, 12.50, 18),
("SIERRA, S.", 34, 199999999, 13.00, 19),
("TANGO, T.", 35, 222222222, 13.50, 20),
("UNIFO, U.", 36, 233333333, 14.00, 21),
("VICTOR, V.", 37, 244444444, 14.50, 22),
("WHISKEY, W.", 38, 255555555, 15.00, 23),
("XRAY, X.", 39, 266666666, 15.50, 24),
("YANKEE, Y.", 40, 277777777, 16.00, 25),
("ZULU Z.", 39, 288888888, 15.50, 26)
};
cout << endl;
cout << setiosflags(ios::left);
for (i = 0; i < NUMRECS; i++)
cout << setw(12) << employee[i].name
<< setw(2) << employee[i].age
<< setw(9) << employee[i].ssn
<< setw(5) << employee[i].hwage
<< setw(2) << employee[i].years << endl;
//cout << endl;
//cout << setiosflags(ios::left);
//cout << employee[25].ssn;
//for (i = 0; i < NUMRECS; i++);
//{ employee[i].hwage = employee[i].hwage + .50;
// employee[i].years = employee[i].years +1;
//cout<<employee[i].years;
system("PAUSE");
return 0;
}
[4290 byte] By [
chief] at [2007-11-11 8:09:23]

# 1 Re: Array Structures
Are you getting any warnings or messages from your compiler?
What is the purpose for your record1.employee data member being an array of 26 members? You don't seem to be using it for anything.
Is your print out "garbage" because you are not constructing any of your structures? Perhaps you are having an issue because of the use of an initialization list which has 26 records in parentheses inside of the brackets?
Try using brackets around the data for each structure instead of parentheses
{
{"ALPHA, A.", 16, 000000000, 04.00, 1},
{"BRAVO, B.", 17, 011111111, 04.50, 2},
{"CHARLIE, C.", 18, 02222222, 05.00, 3},
{"DELTA, D.", 19, 033333333, 05.50, 4},
and so on
};
nspils at 2007-11-11 21:01:38 >

# 2 Re: Array Structures
oh, yeah, you might have to escape the comma, too ...
"ALPHA\, A" ...
nspils at 2007-11-11 21:02:34 >

# 3 Re: Array Structures
Thanks for your replies.
I changed the ( )to { } and stopped at the last line of employee 26 saying invalid conversion from const char to int and warnings converting from double to int. I also added the \, and it gave me an error of unknown escape sequence. Is there a different way to code it?
Chief
chief at 2007-11-11 21:03:32 >

# 4 Re: Array Structures
There are several problems with yoru code. First, your first field in the record is an array of 26 integers. It probably should be a scalar int. Because of this, your initialization is also wrong. Secondly, ssn should be a string too, if you want it to contain traikling zeros and other formatted numbers. Otehrwise, avoid a trailing zero in hardcoded literals because they mean octal numbers.
Danny at 2007-11-11 21:04:43 >

# 5 Re: Array Structures
I'm still at it and finally starting to see the light. I got it to print out the info. Now all I need is to do a loop that for every employee, adds 1 to the number years with the company and that adds 50 cents to the hourly wage.
So if you anyone can help me out with the last step i would really appreciate it. I started on that code which is at the end. Here's my new code:
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
const int NUMRECS = 26;
struct record1
{
string name;
int age;
int ssn;
double hwage;
int years;
};
int num = 25;
int employee;
int main()
{
int i;
record1 employee [26] = {
{"ALPHA A.", 16, 235690754, 4.00, 1},
{"BRAVO B.", 17, 409687467, 4.50, 2},
{"CHARLIE C.", 18, 906457823, 5.00, 3},
{"DELTA D.", 19, 856735432, 5.50, 4},
{"ECHO E.", 20, 908562356, 6.00, 5},
{"FOX F.", 21, 756230987, 6.50, 6},
{"GOLF G.", 22, 325789543, 7.00, 7},
{"HOTEL H.", 23, 879356432, 7.50, 8},
{"INDIA I.", 24, 888888888, 8.00, 9},
{"JULIET J.", 25, 999999999, 8.50, 10},
{"KILO K.", 26, 111111111, 9.00, 11},
{"LIMA L.", 27, 122222222, 9.50, 12},
{"MIKE M.", 28, 133333333, 10.00, 13},
{"NOVEM N.", 29, 144444444, 10.50, 14},
{"OSCAR O.", 30, 155555555, 11.00, 15},
{"PAPA P.", 31, 166666666, 11.50, 16},
{"QUEBEC Q.", 32, 177777777, 12.00, 17},
{"ROMEO R.", 33, 188888888, 12.50, 18},
{"SIERRA S.", 34, 199999999, 13.00, 19},
{"TANGO T.", 35, 222222222, 13.50, 20},
{"UNIFO U.", 36, 233333333, 14.00, 21},
{"VICTOR V.", 37, 244444444, 14.50, 22},
{"WHISKEY W.", 38, 255555555, 15.00, 23},
{"XRAY X.", 39, 266666666, 15.50, 24},
{"YANKEE Y.", 40, 277777777, 16.00, 25},
{"ZULU Z.", 39, 288888888, 15.50, 26}
};
cout << endl;
cout << setiosflags(ios::left);
for (i = 0; i < NUMRECS; i++)
{
cout << setw(15) << employee[i].name;
cout << setw(7) << employee[i].age;
cout << setw(14) << employee[i].ssn;
cout << setw(10) << employee[i].hwage;
cout << setw(5) << employee[i].years << endl;
//cout << endl;
};
cout << endl;
cout << endl;
cout << employee[25].name << endl;
cout << employee[25].years << endl;
cout << endl;
cout << endl;
for (i = 0; i < NUMRECS; i++);
{
employee[i].hwage = employee[i].hwage + .50;
employee[i].years = employee[i].years +1;
cout<<employee[i].years;
}
system("PAUSE");
return 0;
}
chief at 2007-11-11 21:05:44 >

# 6 Re: Array Structures
I think your second loop does exactly what you want to, doesn't it? Which problems do you still have to sort out?
Danny at 2007-11-11 21:06:37 >

# 7 Re: Array Structures
To me it looks like it should, but i get no output. All i get is 0.50 in one line and nothing for the other loop of adding 1 to the years. I expect the output to be all the HWAGE +.50 for each line but it's not hapening.
chief at 2007-11-11 21:07:41 >

# 8 Re: Array Structures
chage the cout expression to:
cout<<employee[i].years<<endl;
Danny at 2007-11-11 21:08:44 >

# 9 Re: Array Structures
I've looked several times and dont see an error. Try a cout.flush() before your pause, and a complete rebuild (delete all object files, whatever your IDE calls that).
The one thing that might be a problem, if your compiler is dumb, is you have an int employee and a struct employee. Try different names.
Look into +=
(instead of x = x+5, use x+= 5)
I personally find it easier to read / less bloated. Also, most libraries / objects have a more efficient += operation.
jonnin at 2007-11-11 21:09:48 >

# 10 Re: Array Structures
Hey guys, thanks for your inputs. I still couldn't get the two loops to work at the end so I just added the info to the first loop and made the output look neat and will submit that. Here's what I did in case ya'll interested.
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
const int NUMRECS = 26;
struct record1
{
string name;
int age;
int ssn;
double hwage;
int years;
};
int num = 24;
int employee;
int main()
{
int i;
record1 employee [26] = {
{"ALPHA A.", 16, 235690754, 4.00, 1},
{"BRAVO B.", 17, 409687467, 4.50, 2},
{"CHARLIE C.", 18, 906457823, 5.00, 3},
{"DELTA D.", 19, 856735432, 5.50, 4},
{"ECHO E.", 20, 908562356, 6.00, 5},
{"FOX F.", 21, 756230987, 6.50, 6},
{"GOLF G.", 22, 325789543, 7.00, 7},
{"HOTEL H.", 23, 879356432, 7.50, 8},
{"INDIA I.", 24, 888888888, 8.00, 9},
{"JULIET J.", 25, 999999999, 8.50, 10},
{"KILO K.", 26, 111111111, 9.00, 11},
{"LIMA L.", 27, 122222222, 9.50, 12},
{"MIKE M.", 28, 133333333, 10.00, 13},
{"NOVEM N.", 29, 144444444, 10.50, 14},
{"OSCAR O.", 30, 155555555, 11.00, 15},
{"PAPA P.", 31, 166666666, 11.50, 16},
{"QUEBEC Q.", 32, 177777777, 12.00, 17},
{"ROMEO R.", 33, 188888888, 12.50, 18},
{"SIERRA S.", 34, 199999999, 13.00, 19},
{"TANGO T.", 35, 222222222, 13.50, 20},
{"UNIFO U.", 36, 233333333, 14.00, 21},
{"VICTOR V.", 37, 244444444, 14.50, 22},
{"WHISKEY W.", 38, 255555555, 15.00, 23},
{"XRAY X.", 39, 266666666, 15.50, 24},
{"YANKEE Y.", 40, 277777777, 16.00, 25},
{"ZULU Z.", 41, 288888888, 16.50, 26}
};
cout << endl;
cout << setiosflags(ios::left);
cout<<" "<<" "<<" "<<"HRLY "<<" NEW HRLY "<<" "<<" NEW"<<endl;
cout<<"NAME "<<"AGE "<<"SSN "<<"WAGE "<<" WAGE(+ .50) "<<" YEARS"<<" YEARS(+1)"<<endl;
cout<<"==========="<<" ==="<<" ========="<<" ====="<<" ==========="<<" ====="<<" ========"<<endl;
cout<<" "<<endl;
for (i = 0; i < NUMRECS; i++)
{
cout << setw(15) << employee[i].name;
cout << setw(7) << employee[i].age;
cout << setw(14) << employee[i].ssn;
cout << setw(10) << fixed << setprecision(2) << employee[i].hwage;
cout << setw(15) << fixed << setprecision(2) << employee[i].hwage + .50;
cout << setw(8) << employee[i].years;
cout << setw(5) << employee[i].years + 1 << endl;
};
cout << endl;
cout << endl;
cout <<"The 25th employee is: " << employee[24].name << endl;
cout << endl;
cout <<"The number of years in the company is: " << employee[24].years << endl;
cout << endl;
cout << endl;
system("PAUSE");
return 0;
}
chief at 2007-11-11 21:10:43 >

