Need help with struct output using a loop!

Need help trying to output my struct to a for loop. I understand how to output the struct but not into a for loop. Can someone help. My professor didn't explain this too well. The last part is my attempt at how I would approach it.

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
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

struct Date
{
	int day;
	int month;
	int year;
};

struct Employee
{ 
		Date  birthdate;
		int age;
		int id;
		float salary;
		int getAge( ) { return age; };
		int getId( )  {return id; };
		float getSalary( ) {return salary; };
        Date getBirthdate( ) { return birthdate;};
}x,y,z;

void main()
{

	x.id = 30042554; // set the values for the two elements of the array
	x.age = 30;
	x.salary = 50000.00;
	x.birthdate.day = 15;
	x.birthdate.month = 01;
	x.birthdate.year = 1985;

	y.id = 40041002;
	y.age = 45;
	y.salary = 70000.00;
	y.birthdate.day = 13;
	y.birthdate.month = 02;
	y.birthdate.year = 1990;

	z.id = 50051003;
	z.age = 25;
	z.salary = 30000.00;
	z.birthdate.day =30;
	z.birthdate.month = 10;
	z.birthdate.year = 1991;

	PrintEmployee(x,y,z); // call PrintEmployee( ) with a proper parameter list
}

void PrintEmployee(Employee&x,Employee&y,Employee&z)
{

	 for(int i=0; i<4; i++)//for loop
	 {
		 cout <<"Employee: "<< endl;
		 cout <<"----------";
		 cout << "Age: " << x.age << endl;

	
	}

	// Output the information to screen by calling the member functions of the struct

	// print out the maximum and minimum salaries 

	//  print out the maximum and minimum ages

}
Last edited on
Also I need to output minimum and maximum salaries and ages.

Where is he finishing bracket for the for() loop??

Also, you would be better off writing a single function that displays a single instance of the data structure. Then, you could call that function for each.

1
2
3
Cout<< whatever.member1<< endl;
Cout << whatever.member2<< endl;
/* and so on.....*/

That's what the for loop is for right? To display a single instance of the data structure......or am I wrong? Could you maybe show an example? And I just for got the bracket. Sorry.
Using a for loop only makes sense if you can use the counter variable of the loop to identify your structure. I think your professor is intending for you to have an array of those objects, rather than having them as 3 different variables x, y and z.

Then you can use the loop to loop over the array, outputting each element in turn.
Look at my reply in this thread:

http://www.cplusplus.com/forum/general/115781/

In there you will see some example code to replicate a stack which you could use to store instances of your Employee type. Only if you're not "allowed" to use a vector, otherwise use a vector<Employee> employees

HTH
Pretty sure I'm not able to use a vector yet
And the thread really doesn't help because I need to use a for loop.
Well, you haven't really shown us anything that you'd want to use a for loop for. I've already explained what I think your professor is looking for, but apparently my answer wasn't good enough for you.
And the thread really doesn't help because I need to use a for loop


Well if you look back at the thread I pointed you to, you can use a for(...) loop with the code I provided with the class Products (you would probably call it something else) but you could have a class of, for example:

1
2
3
4
class Emplyees
{
   ...
};


and then would be able to iterate through all your employees once you had added them:

1
2
3
4
5
6
7
8

Employees employees;
...
for (int idx = 0; idx < employees.Count(); ++idx)
{
   Employee employee = employees.get_at(idx);
   ...
}


You will need to add a getter to the Employees class to get the number of items aka Employee instances in the stack int Count() return count; etc...

That should get you on your way. If you get stuck then show us your latest code.
Last edited on
You could use a for loop to display the structure, but it is easier to write it into a function, because then instead of writing the code every time, you simply call the function... In my experience, it is easiest (especially in large projects) to separate code into as many functions as possible to centralize code and make it easier to manage. So, instead of looping through the instructions, it would look somthing like this:

1
2
3
4
for(unsigned int x = 0; x < whatever; x++)
{
    display_data_structure(my_structure); //easier to read, and modify.
}


prototype for the function might look somthing like this:

you may want to be as specific as possible when writing these kinds of functions. In large projects, you could have many structures.
void display_data_structure(const structure_name&);

i would do:

void display_employee_data(const Employee&);

on another note, you could also make your code easier to manage by making x y and z into a simple array that you can resize.

1
2
3
Employee[3] employee_list({Employee(), Employee(), Employee()});
//now set employee_list[1] through employee_list[3].
//calling members is as simple as employee_list[x].member 
Last edited on
Okay I definitely appreciate all the responses and help. I actually figured it out. I went back to my professor. So now my code looks like this:
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
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

struct Date
{
	int day;
	int month;
	int year;
};

struct Employee
{ 
		Date  birthdate;
		int age;
		int id;
		float salary;
		int getAge( ) { return age; };
		int getId( )  {return id; };
		float getSalary( ) {return salary; };
        Date getBirthdate( ) { return birthdate;};
}company[3];

void PrintEmployee(Employee comapany[], int len)
{

	for (int i=0;i<len;i++) //for loop
	{
		cout<< "\nEmployee " << i + 1 << endl;
		cout << "-----------" << '\n';
		cout << "Age: " <<company[i].age << endl;
		cout<< "Id: " << company[i].id << endl;
		cout<< "Salary: " << company[i].salary<< endl;
		cout<< " BirthDate: " << company[i].birthdate << endl;
		cout << "==========================" << '\n';
	}

}

void main()
{

	company [0].id = 30042554; // set the values for the two elements of the array
	company [0].age = 30;
	company [0].salary = 50000.00;
	company [0].birthdate.day = 15;
	company [0].birthdate.month = 01;
	company [0].birthdate.year = 1985;

	company [1].id = 40041002;
	company [1].age = 45;
	company [1].salary = 70000.00;
	company [1].birthdate.day = 13;
	company [1].birthdate.month = 02;
	company [1].birthdate.year = 1990;

	company [2].id = 50051003;
	company [2].age = 25;
	company [2].salary = 30000.00;
	company [2].birthdate.day =30;
	company [2].birthdate.month = 10;
	company [2].birthdate.year = 1991;


	PrintEmployee(company, 3); // call PrintEmployee( ) with a proper parameter list
	cin.get();
}

My next question is how do I get the birthdate to display since it is made up of another struct. The way it is typed in the code above keeps shooting back an error.
how do I get the birthdate to display since it is made up of another struct. The way it is typed in the code above keeps shooting back an error.

You do realise that, when you decide to withhold useful information, like, say, the error message you're getting, you're making it harder for us to help you, right?

In this case, it's pretty straightforward. You have - quite rightly - made birthdate a private member, so it can't be accessed by code that doesn't belong to the same class.

You have also, quite rightly, created an accessor function getBirthdate(). This is exactly the kind of situation you need to use it for.


Edit: Apologies, I didn't read your code properly. Ignore all of the above.
Last edited on
The error that I am getting is
Error: no operator "<<" matches these operands at line 35.
Last edited on
My apologies - ignore my previous post. I didn't read your code properly.

The problem is the birthdate is of type Date, which is a type that you've defined. The compiler can't possibly know how to stream an object of that type. You have to tell it how to stream it, by creating a << operator for it.

Alternatively, you can just stream the individual members of birthdate. That's probably the easiest thing to do right now, unless you want to learn about overloading operators.
Thanks! I been sitting here for three two hours wondering out how to output. I hadn't learned overloading and I tried to teach myself. Your right the easiest way was to output each member.
You're welcome - I'm glad it worked out.
Topic archived. No new replies allowed.