943,747 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 431
  • C++ RSS
Sep 18th, 2009
0

please help me with structures

Expand Post »
hello every one.. i m a newbie and was going through structures.the code is as followes:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CD-4+ is offline Offline
8 posts
since Aug 2009
Sep 18th, 2009
0

Re: please help me with structures

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

Re: please help me with structures

Just add
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
at the top of your file for cin and cout usage.
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Sep 18th, 2009
0

Re: please help me with structures

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...
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CD-4+ is offline Offline
8 posts
since Aug 2009
Sep 18th, 2009
0

Re: please help me with structures

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.
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Sep 18th, 2009
0

Re: please help me with structures

.
Last edited by firstPerson; Sep 18th, 2009 at 7:17 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008

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: Fuzzy string search or searching for misspelled words
Next Thread in C++ Forum Timeline: cpp compiler





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


Follow us on Twitter


© 2011 DaniWeb® LLC