| | |
What am I doing wrong???
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 7
Reputation:
Solved Threads: 0
Today I had this question in my exam:
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)
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
c++ Syntax (Toggle Plain Text)
#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; }
Last edited by Gewalop; May 28th, 2009 at 2:28 pm.
•
•
Join Date: Apr 2009
Posts: 7
Reputation:
Solved Threads: 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.
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".
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".
> What am I doing wrong???
A lot! .... but nothing that you can't learn from, with a little bit of effort... Your
...
And, as Salem has mentioned - "Post your original code!" - and as aforementioned, why won't your teacher answer this for you?
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."
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."
It's probably better to make the following variables private in your
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 (as you have already input/output functions): C++ Syntax (Toggle Plain Text)
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 {
// 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."
•
•
Join Date: Apr 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
> 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".
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... Yourstructcontains 2 functions, both of which can directly access the otherstructmembers. Also, every instance of yourstructhave 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?
I actually tried it, it's more neat and lovely, thx for the help
•
•
Join Date: May 2009
Posts: 27
Reputation:
Solved Threads: 2
C++ Syntax (Toggle Plain Text)
#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>
Last edited by Ancient Dragon; May 28th, 2009 at 11:11 pm. Reason: add code tags, snip email
![]() |
Similar Threads
- My CD-RW plays but won't burn. What could be wrong?? Help Please? (Storage)
- Retrieve email I sent to the wrong person (Web Browsers)
- Ram voltage wrong?? (Motherboards, CPUs and RAM)
- Am I going about this the wrong way (IT Professionals' Lounge)
- wats wrong with imesh??? (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: tolower function does not work on certain letters
- Next Thread: Can anyone please find whats wrong in this code
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






