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.
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.
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
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:
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(unsignedint 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
#include "stdafx.h"
#include <string>
#include <iostream>
usingnamespace 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.
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.