943,602 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 485
  • C++ RSS
May 28th, 2009
0

What am I doing wrong???

Expand Post »
Today I had this question in my exam:

C++ Syntax (Toggle Plain Text)
  1. write a program in C++ that covers the next points:
  2. -a company needs to enter its employees' data into the computer
  3. -each emplyee has (ID-Job Code-Division Code-Salary)
  4. -should be done using struct
  5. -Input/Output functions should be inside the struct
  6. -use the struct to input a user-defined number of employees (max 50)
  7. -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.
  8.  

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

c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3. struct employee
  4. {
  5. int ID;
  6. int salary;
  7. int division_code;
  8. int job_code;
  9. void input(employee emp[], int i)
  10. {
  11. cout << "Employee number "<<i+1<<endl;
  12. cout << "ID"<<endl;
  13. cin>>emp[i].name;
  14. cout << "Job Code"<<endl;
  15. cin >> emp[i].job_code;
  16. cout << "Division Code"<<endl;
  17. cin >> emp[i].division_code;
  18. cout << "Salary"<<endl;
  19. cin >> emp[i].salary;
  20. }
  21. void output(employee emp[],int i)
  22. {
  23. cout <<"ID: "<<emp[i].name<<endl;
  24. cout <<"Job: #"<<emp[i].job_code<<endl;
  25. cout <<"Div: #"<<emp[i].division_code<<endl;
  26. cout <<"Salary: $"<<emp[i].salary<<endl;
  27. }
  28.  
  29. };
  30. int main()
  31. {
  32. employee emp[50];
  33. int division,n;
  34. cout<<"Enter the number of employees"<<endl;
  35. cin>>n;
  36. for (int i=0;i<n;i++)
  37. emp[i].input(emp,i);
  38. cout <<"Enter Division code to find the employees working in it"<<endl;
  39. cin >>division;
  40. for (i=0;i<n;i++)
  41. {
  42. if (emp[i].division_code==division)
  43. {
  44. emp[i].output(emp,i);
  45. }
  46. }
  47. return 0;
  48. }
Last edited by Gewalop; May 28th, 2009 at 2:28 pm.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Gewalop is offline Offline
17 posts
since Apr 2009
May 28th, 2009
0

Re: What am I doing wrong???

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 28th, 2009
0

Re: What am I doing wrong???

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Gewalop is offline Offline
17 posts
since Apr 2009
May 28th, 2009
0

Re: What am I doing wrong???

Possibly the fact that struct keyword mostly is used for containers?
Last edited by mostermand; May 28th, 2009 at 2:41 pm.
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
May 28th, 2009
1

Re: What am I doing wrong???

> 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".
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 28th, 2009
0

Re: What am I doing wrong???

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

And, as Salem has mentioned - "Post your original code!" - and as aforementioned, why won't your teacher answer this for you?
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 28th, 2009
0

Re: What am I doing wrong???

It's probably better to make the following variables private in your struct (as you have already input/output functions):
C++ Syntax (Toggle Plain Text)
  1. int ID;
  2. int salary;
  3. int division_code;
  4. 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 {
// your other stuff goes here
// please note that you could use the public access specifier here, but it's
// not really required, because a struct's data members are public by default,
// unless specified else
private:
  int ID;
  int salary;
  int division_code;
  int job_code;
};
Last edited by tux4life; May 28th, 2009 at 4:29 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 28th, 2009
0

Re: What am I doing wrong???

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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.


Click to Expand / Collapse  Quote originally posted by amrith92 ...
> 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) ...

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.



Click to Expand / Collapse  Quote originally posted by tux4life ...
Every data member of a struct is made public by default, however you could use the data access specifiers to encapsulate some data members.

I actually tried it, it's more neat and lovely, thx for the help
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Gewalop is offline Offline
17 posts
since Apr 2009
May 28th, 2009
-1

Re: What am I doing wrong???

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct employee
  6.  
  7. {
  8.  
  9. int ID;
  10.  
  11. int salary;
  12.  
  13. int division_code;
  14.  
  15. int job_code;
  16.  
  17. void input(employee emp[], int i)
  18.  
  19. {
  20.  
  21. cout << "Employee number "<<i+1<<endl;
  22.  
  23. cout << "ID"<<endl;
  24.  
  25. cin>>emp[i].ID;
  26.  
  27. cout << "Job Code"<<endl;
  28.  
  29. cin >> emp[i].job_code;
  30.  
  31. cout << "Division Code"<<endl;
  32.  
  33. cin >> emp[i].division_code;
  34.  
  35. cout << "Salary"<<endl;
  36.  
  37. cin >> emp[i].salary;
  38.  
  39. }
  40.  
  41. void output(employee emp[],int i)
  42.  
  43. {
  44.  
  45. cout <<"ID: "<<emp[i].ID<<endl;
  46.  
  47. cout <<"Job: #"<<emp[i].job_code<<endl;
  48.  
  49. cout <<"Div: #"<<emp[i].division_code<<endl;
  50.  
  51. cout <<"Salary: $"<<emp[i].salary<<endl;
  52.  
  53. }
  54.  
  55.  
  56.  
  57. };
  58. int c;
  59. int main()
  60.  
  61. {
  62.  
  63. employee emp[50];
  64.  
  65. int division,n;
  66.  
  67. cout<<"Enter the number of employees"<<endl;
  68.  
  69. cin>>n;
  70.  
  71. for (int i=0;i<n;i++)
  72.  
  73. emp[i].input(emp,i);
  74.  
  75. cout <<"Enter Division code to find the employees working in it"<<endl;
  76.  
  77. cin >>division;
  78.  
  79. for (c=0;c<n;c++)
  80.  
  81. {
  82.  
  83. if (emp[c].division_code==division)
  84.  
  85. {
  86.  
  87. emp[c].output(emp,c);
  88.  
  89. }
  90.  
  91. }
  92.  
  93. return 0;
  94.  
  95. }

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>
Last edited by Ancient Dragon; May 28th, 2009 at 11:11 pm. Reason: add code tags, snip email
Reputation Points: -9
Solved Threads: 2
Light Poster
mirfan00 is offline Offline
29 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: tolower function does not work on certain letters
Next Thread in C++ Forum Timeline: Can anyone please find whats wrong in this code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC