What am I doing wrong???

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 7
Reputation: Gewalop is an unknown quantity at this point 
Solved Threads: 0
Gewalop Gewalop is offline Offline
Newbie Poster

What am I doing wrong???

 
0
  #1
May 28th, 2009
Today I had this question in my exam:

  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

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: What am I doing wrong???

 
0
  #2
May 28th, 2009
> 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: Gewalop is an unknown quantity at this point 
Solved Threads: 0
Gewalop Gewalop is offline Offline
Newbie Poster

Re: What am I doing wrong???

 
0
  #3
May 28th, 2009
Originally Posted by Salem View Post
> 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: What am I doing wrong???

 
0
  #4
May 28th, 2009
Possibly the fact that struct keyword mostly is used for containers?
Last edited by mostermand; May 28th, 2009 at 2:41 pm.
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: What am I doing wrong???

 
1
  #5
May 28th, 2009
> 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".
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 135
Reputation: amrith92 is on a distinguished road 
Solved Threads: 13
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: What am I doing wrong???

 
0
  #6
May 28th, 2009
> 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?
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: What am I doing wrong???

 
0
  #7
May 28th, 2009
It's probably better to make the following variables private in your struct (as you have already input/output functions):
  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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: Gewalop is an unknown quantity at this point 
Solved Threads: 0
Gewalop Gewalop is offline Offline
Newbie Poster

Re: What am I doing wrong???

 
0
  #8
May 28th, 2009
Originally Posted by Salem View Post
> 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.


Originally Posted by amrith92 View Post
> 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.



Originally Posted by tux4life View Post
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: mirfan00 has a little shameless behaviour in the past 
Solved Threads: 2
mirfan00 mirfan00 is offline Offline
Light Poster

Re: What am I doing wrong???

 
-1
  #9
May 28th, 2009
  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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC