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;
}

Dani AI

Generated

A short, focused checklist and fixes that address what graders typically mark on this kind of exam answer. Several people in the thread already gave useful hints — flagged compilation problems, and raised design/encapsulation points, and posted a corrected run — the items below consolidate those ideas into concrete, exam-ready guidance.

Scope and declarations: every identifier must be declared in the scope where it’s used. A common paper-test slip is declaring a loop counter inside one for-loop and then reusing the same name later without a declaration; that produces a compile error and heavy penalty. Typos and undeclared members or stray punctuation cause the same outcome. Those are the fastest faults graders find.

Struct responsibilities and API: have each employee object own its own I/O. Give the struct instance-level methods (e.g., input() and output()) that read/write that instance’s members rather than accepting the whole array plus an index. That makes main a simple loop that calls emp[i].input(), and later another loop to filter by division. Encapsulation (private data + public accessors) improves clarity if allowed by the exam brief.

Robustness and presentation: validate the entered employee count (1..50) and handle bad numeric input (check stream state and clear if needed). When filtering by division, print a clear “no matches” message if none match. Consistent prompts and labeled output help graders follow program logic quickly. For modern code, std::vector and range-for are preferable, but a fixed-size array is acceptable if the question specifies a maximum.

Fixing scoping/typos, making I/O instance-based, adding input validation, and improving output clarity converts a fragile submission into a verifiable, maintainable solution that compiles and is easy for a grader to read.

Recommended Answers

All 8 Replies

> 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

Possibly the fact that struct keyword mostly is used for containers?

> 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".

commented: Right :D +7

> 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>

commented: 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 !! -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.