Reading/writing .txt files into an array

Reply

Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Reading/writing .txt files into an array

 
0
  #1
Dec 2nd, 2007
I am writing a program and I have a .txt file that has a some information about people. I would like to write that information into an array and then display that information about the person.

The problem is that I don't know how many names will be in the .txt file, or how to write that information to the file.

Here is what I have so far.

  1. #include <iostream>
  2. #include "StaffEmployee.h"
  3. #include "HourlyEmployee.h"
  4. #include <iomanip>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. void upload()
  10. {
  11. ifstream infile;
  12. infile.open ("hourly.txt");
  13. while (!infile.eof())
  14. {
  15.  
  16. }
  17.  
  18. }
  19.  
  20. int main()
  21. {
  22. const int size = 20;
  23. HourlyEmployee a[size];
  24. HourlyEmployee fellow;
  25. HourlyEmployee person(10, 55, "Electronics", "Alan", "Good", "E1234") ;
  26.  
  27. fellow.display();
  28. person.display();
  29.  
  30.  
  31.  
  32.  
  33. return 0;
  34. }

and here is my hourlyEmployee class.

  1. #include "Employee.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. #ifndef HOURLYEMPLOYEE_H
  8. #define HOURLYEMPLOYEE_H
  9.  
  10. class HourlyEmployee:public Employee
  11. {
  12. private:
  13. double hourlyWage;
  14. double hoursWorked;
  15. string deptName;
  16.  
  17. public:
  18. HourlyEmployee()
  19. {
  20. hourlyWage = 0;
  21. hoursWorked = 0;
  22. deptName = "unknown";
  23. }
  24.  
  25. HourlyEmployee(double hWage, double hWorked, string dName, string first, string last, string ID) : Employee(first, last, ID)
  26. {
  27. hourlyWage = hWage;
  28. hoursWorked = hWorked;
  29. deptName = dName;
  30. }
  31. void set_hourlyWage(double hWage)
  32. {
  33. hourlyWage = hWage;
  34. }
  35. void set_hoursWorked(double hWorked)
  36. {
  37. hoursWorked = hWorked;
  38. }
  39. void set_deptName(string dName)
  40. {
  41. deptName = dName;
  42. }
  43. double get_hourlyWage()
  44. {
  45. return hourlyWage;
  46. }
  47. double get_hoursWorked()
  48. {
  49. return hoursWorked;
  50. }
  51. string get_deptName()
  52. {
  53. return deptName;
  54. }
  55. void compute_salary()
  56. {
  57. double salary = 0;
  58. salary = hoursWorked * hourlyWage;
  59. cout << "2 Week Salary: $" << salary;
  60. cout << endl;
  61. }
  62. void display()
  63. {
  64. Employee::display();
  65. double salary = 0;
  66. salary = hoursWorked * hourlyWage;
  67. cout << "2 Week Salary: $" << salary;
  68. cout << endl;
  69. cout << endl;
  70. }
  71. };
  72.  
  73. #endif

I don't really know how to put the .txt information into the array so if anyone could give me some idea's or any help at all that would be great.

Thanks
Last edited by Ancient Dragon; Dec 2nd, 2007 at 8:48 pm. Reason: added line numbers to existing code tags -- this is not a rules violation but just a convenience.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Reading/writing .txt files into an array

 
0
  #2
Dec 2nd, 2007
That while statement on line 13 is incorrect because eof() doesn't work like that. Instead, code it something like this
  1. string line;
  2. while(getline(infile, line) )
  3. {
  4. // now split this string into individual parts for the structure
  5. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Reading/writing .txt files into an array

 
0
  #3
Dec 2nd, 2007
Originally Posted by Ancient Dragon View Post
That while statement on line 13 is incorrect because eof() doesn't work like that. Instead, code it something like this
  1. string line;
  2. while(getline(infile, line) )
  3. {
  4. // now split this string into individual parts for the structure
  5. }
Thanks for that but do you know how to write the information into an array? Or for that matter how to split this string into individual parts for the structure?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Reading/writing .txt files into an array

 
0
  #4
Dec 2nd, 2007
Also thanks for changing the code
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading/writing .txt files into an array

 
0
  #5
Dec 2nd, 2007
Originally Posted by cl3m0ns View Post
Thanks for that but do you know how to write the information into an array? Or for that matter how to split this string into individual parts for the structure?
Yes. But it depends on lots of things -- format of the file, what the array looks like, what delimiters are in the string. Details help make decisions.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: cl3m0ns is an unknown quantity at this point 
Solved Threads: 0
cl3m0ns's Avatar
cl3m0ns cl3m0ns is offline Offline
Light Poster

Re: Reading/writing .txt files into an array

 
0
  #6
Dec 2nd, 2007
Originally Posted by WaltP View Post
Yes. But it depends on lots of things -- format of the file, what the array looks like, what delimiters are in the string. Details help make decisions.

the .txt file is...

Joe Cruise E1111
Bill Bones E2222
Sue Smith E3333
Tom Turpin E4444
Ron Stewart E5555
Mindy Sweet E7777

and the class file employee has string firstName, string lastName, string employeeID.

i want to use an array in my int main to write those names and ID's to the class....


is that possible and if so how would I even start to do it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Reading/writing .txt files into an array

 
0
  #7
Dec 2nd, 2007
The input line can be split using stringstream class
  1. #include <sstream>
  2. ...
  3. ...
  4. stringstream stream(line);
  5. stream >> firstName >> lastName >> employeeID;
Last edited by Ancient Dragon; Dec 2nd, 2007 at 10:30 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading/writing .txt files into an array

 
0
  #8
Dec 2nd, 2007
Yes, it's possible. Make an array of the class employee. As you read the file, load the class. You can then access employee[x].FirstName, etc.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC