please help me with structures

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 8
Reputation: CD-4+ is an unknown quantity at this point 
Solved Threads: 0
CD-4+'s Avatar
CD-4+ CD-4+ is offline Offline
Newbie Poster

please help me with structures

 
0
  #1
Sep 18th, 2009
hello every one.. i m a newbie and was going through structures.the code is as followes:
  1. #include<string>
  2. using namespace std;
  3.  
  4. #define n_emp 3
  5.  
  6. struct workerdb {
  7. string name;
  8. short age;
  9. int salary;
  10. }employee[n_emp];
  11.  
  12. void printdb (workerdb);
  13.  
  14. int main() {
  15. int n;
  16. for (n=0;n<n_emp;n++) {
  17. cout<<"name: "<<endl;
  18. getline(cin,employee[n].name);
  19. cout<<endl;
  20. cout<<"age: "<<endl;
  21. cin>>employee[n].age;
  22. cout<<endl;
  23. cout<<"salary: "<<endl;
  24. cin>>employee[n].salary;
  25. cout<<endl;
  26. }
  27.  
  28. for (n=0;n<n_emp;n++) {
  29. printdb (employee[n]);
  30. }
  31. return 0;
  32. }
  33.  
  34. void printdb(workerdb employee) {
  35. cout<<"name: "<<employee.name<<"\t";
  36. cout<<"age: "<<employee.age<<"\t";
  37. cout<<"salary: "<<employee.salary<<endl;
  38. }
the problem is when i run the program.. for the first structure it asks for the name.. from the next it skips of name input but asks for other field.Thus when the database is complete i gate just the first name but rest structures it remains empty... please run it if i am not clear...

regards
cd-4+
Last edited by CD-4+; Sep 18th, 2009 at 1:24 am.
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: please help me with structures

 
0
  #2
Sep 18th, 2009
Short answer - don't mix cin and getline.
They don't play well together.
Plenty of past posts and answers for this problem, look around.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 246
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training

Re: please help me with structures

 
0
  #3
Sep 18th, 2009
Just add
  1. cin.ignore();
at the bottom of your for() loop for input and this will ignore the '\n' character from the last cin.

Also it looks like you do not have
  1. #include <iostream>
at the top of your file for cin and cout usage.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: CD-4+ is an unknown quantity at this point 
Solved Threads: 0
CD-4+'s Avatar
CD-4+ CD-4+ is offline Offline
Newbie Poster

Re: please help me with structures

 
0
  #4
Sep 18th, 2009
hi .. i m sorry actually while copying i may have missed out.. but that wasn't the problem.. well i don't know if this way is good i found a new way...
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. #define n_emp 3
  6.  
  7. struct workerdb {
  8. string name;
  9. short age;
  10. int salary;
  11. }employee[n_emp];
  12.  
  13. void printdb (workerdb);
  14.  
  15. int main() {
  16. int n;
  17. for (n=0;n<n_emp;n++) {
  18. cout<<"name: "<<endl;
  19. cin>>(employee[n].name);
  20. cout<<endl;
  21. cout<<"age: "<<endl;
  22. cin>>employee[n].age;
  23. cout<<endl;
  24. cout<<"salary: "<<endl;
  25. cin>>employee[n].salary;
  26. cout<<endl;
  27. }
  28.  
  29. for (n=0;n<n_emp;n++) {
  30. printdb (employee[n]);
  31. }
  32. return 0;
  33. }
  34.  
  35. void printdb(workerdb employee) {
  36. cout<<"name: "<<employee.name<<"\t";
  37. cout<<"age: "<<employee.age<<"\t";
  38. cout<<"salary: "<<employee.salary<<endl;
  39. }
thanks for your time on this .

regards
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 246
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training

Re: please help me with structures

 
0
  #5
Sep 18th, 2009
The whole reason why you would use the getline() function is to pick up spaces. If I were to input the name "Jim" with just cin, this would work but if I were to give him a last name "Jim McTavish" then it would only take in "Jim" and ignore the space and everything after. With getline() you would get the whole name.

By using cin.ignore() I was able to get it working fine with your code and this adjustment.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,120
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 143
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: please help me with structures

 
0
  #6
Sep 18th, 2009
.
Last edited by firstPerson; Sep 18th, 2009 at 7:17 pm.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC