Today I had this question in my exam:

write a program in C++ that covers the next points:
-a company needs to enter its employees' data into the computer
-each emplyee has (ID-Job Code-Division Code-Salary)
-should be done using struct
-Input/Output functions should be inside the struct
-use the struct to input a user-defined number of employees (max 50)
-after inputing the employees make sure that the company can enter a division code and the result would be the information about employees in that division.

my answer was the next
and I've got only 40% on it, what did I do wrong, If you please can point out my mistakes

#include<iostream>
using namespace std;
struct employee
{
	int ID;
	int salary;
	int division_code;
	int job_code;
	void input(employee emp[], int i)
	{
		cout << "Employee number "<<i+1<<endl;
		cout << "ID"<<endl;
		cin>>emp[i].name;
		cout << "Job Code"<<endl;
		cin >> emp[i].job_code;
		cout << "Division Code"<<endl;
		cin >> emp[i].division_code;
		cout << "Salary"<<endl;
		cin >> emp[i].salary;
	}
	void output(employee emp[],int i)
	{
		cout <<"ID: "<<emp[i].name<<endl;
		cout <<"Job: #"<<emp[i].job_code<<endl;
		cout <<"Div: #"<<emp[i].division_code<<endl;
		cout <<"Salary: $"<<emp[i].salary<<endl;
	}
	
};
int main()
{
	employee emp[50];
	int division,n;
	cout<<"Enter the number of employees"<<endl;
	cin>>n;
	for (int i=0;i<n;i++)
		emp[i].input(emp,i);
	cout <<"Enter Division code to find the employees working in it"<<endl;
	cin >>division;
	for (i=0;i<n;i++)
	{
		if (emp[i].division_code==division)
		{
			emp[i].output(emp,i);
		}
	}
	return 0;
}

> what did I do wrong
You didn't compile it before handing it in.

Look carefully around line 12
See how the syntax "colouring" has gone all wacky?
That's because your code is broke.

> what did I do wrong
You didn't compile it before handing it in.

Look carefully around line 12
See how the syntax "colouring" has gone all wacky?
That's because your code is broke.

My friend, thanks for the advice, it was a ", yes a "
and It didn't change the fact the answer is wrong, according to my teacher

> My friend, thanks for the advice, it was a ", yes a "
So post your ACTUAL code, direct from your editor.

Not some abridged version based on how you remember it.

Because we're not going to sit here guessing at what you MIGHT have screwed up just for you to reply "oh, that's fixed in the actual code".

Comments
Right :D

> What am I doing wrong???

A lot! .... but nothing that you can't learn from, with a little bit of effort... Your struct contains 2 functions, both of which can directly access the other struct members. Also, every instance of your struct have available to them, your two functions amongst all its other data members. The knowledge above facts would have greatly simplify your task, and you'd get a better grade for your assignment(Test). Also try listening to your teacher/professor once in a while(If he/she gave this for your test, then he/she would, most certainly, have discussed how to do this during class) :P ...

And, as Salem has mentioned - "Post your original code!" - and as aforementioned, why won't your teacher answer this for you?

It's probably better to make the following variables private in your struct (as you have already input/output functions):

int ID;
int salary;
int division_code;
int job_code;

Every data member of a struct is made public by default, however you could use the data access specifiers to encapsulate some data members, like this:

struct employee {
[B]// your other stuff goes here[/B]
[B]// please note that you could use the public access specifier here, but it's[/B]
[B]// not really required, because a struct's data members are public by default,[/B]
[B]// unless specified else[/B]
[B]private:[/B]
  int ID;
  int salary;
  int division_code;
  int job_code;
};

:P

> My friend, thanks for the advice, it was a ", yes a "
So post your ACTUAL code, direct from your editor.

Not some abridged version based on how you remember it.

Because we're not going to sit here guessing at what you MIGHT have screwed up just for you to reply "oh, that's fixed in the actual code".

I Apologize about my rude behavior.
Thanks for you help.
We do our exams on papers, so we can't compile them and try, we have one chance, get right or wrong, that's why I wrote what I remembered.

> What am I doing wrong???

A lot! .... but nothing that you can't learn from, with a little bit of effort... Your struct contains 2 functions, both of which can directly access the other struct members. Also, every instance of your struct have available to them, your two functions amongst all its other data members. The knowledge above facts would have greatly simplify your task, and you'd get a better grade for your assignment(Test). Also try listening to your teacher/professor once in a while(If he/she gave this for your test, then he/she would, most certainly, have discussed how to do this during class) :P ...

And, as Salem has mentioned - "Post your original code!" - and as aforementioned, why won't your teacher answer this for you?

We have a very strict teaching policy, teachers give the tip of the thread and you have to study on your own, finally you get tested in stuff maybe you never heard of, but that's the beauty of it (i think) you have to work hard and try to learn as much as you can, of course questions don't come brutal and scary, but you know you always have to be careful, and we don't meet our teachers again before 4 months.


Every data member of a struct is made public by default, however you could use the data access specifiers to encapsulate some data members.

:P

I actually tried it, it's more neat and lovely, thx for the help

#include<iostream>
      
      using namespace std;
   
      struct employee
   
      {
   
      int ID;
   
      int salary;
   
      int division_code;
   
      int job_code;
   
      void input(employee emp[], int i)
  
      {
  
      cout << "Employee number "<<i+1<<endl;
  
      cout << "ID"<<endl;
  
      cin>>emp[i].ID;
  
      cout << "Job Code"<<endl;
  
      cin >> emp[i].job_code;
  
      cout << "Division Code"<<endl;
  
      cin >> emp[i].division_code;
  
      cout << "Salary"<<endl;
  
     cin >> emp[i].salary;
  
      }
  
      void output(employee emp[],int i)
  
      {
  
      cout <<"ID: "<<emp[i].ID<<endl;
  
      cout <<"Job: #"<<emp[i].job_code<<endl;
  
      cout <<"Div: #"<<emp[i].division_code<<endl;
  
      cout <<"Salary: $"<<emp[i].salary<<endl;
  
      }
  
       
  
      };
      int c;
      int main()
  
      {
  
      employee emp[50];
  
      int division,n;
  
      cout<<"Enter the number of employees"<<endl;
  
      cin>>n;
  
      for (int i=0;i<n;i++)
  
      emp[i].input(emp,i);
  
      cout <<"Enter Division code to find the employees working in it"<<endl;
  
      cin >>division;
  
      for (c=0;c<n;c++)
  
      {
      
      if (emp[c].division_code==division)
  
      {
  
      emp[c].output(emp,c);
  
      }
  
      }
     
      return 0;
  
      }

Hi
The above code and this code is same. And i check it it was corrected. Output was the same as the company desired.But i dont know why you got 40%.
I thing more you wrote name instead of ID may thats your mistake because name is undecleared in struc.I correct it in my code.

Irfan
<snip email>

Comments
Typical noob behaviour: posting in a thread which is already marked as solved, not using of code tags, posting no nonsense and mentioning of your email address, enough reasons for getting bad rep !!
This question has already been answered. Start a new discussion instead.