943,607 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3790
  • C++ RSS
Dec 2nd, 2007
0

Reading/writing .txt files into an array

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  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.

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Dec 2nd, 2007
0

Re: Reading/writing .txt files into an array

That while statement on line 13 is incorrect because eof() doesn't work like that. Instead, code it something like this
C++ Syntax (Toggle Plain Text)
  1. string line;
  2. while(getline(infile, line) )
  3. {
  4. // now split this string into individual parts for the structure
  5. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2007
0

Re: Reading/writing .txt files into an array

That while statement on line 13 is incorrect because eof() doesn't work like that. Instead, code it something like this
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Dec 2nd, 2007
0

Re: Reading/writing .txt files into an array

Also thanks for changing the code
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Dec 2nd, 2007
0

Re: Reading/writing .txt files into an array

Click to Expand / Collapse  Quote originally posted by cl3m0ns ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Dec 2nd, 2007
0

Re: Reading/writing .txt files into an array

Click to Expand / Collapse  Quote originally posted by WaltP ...
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?
Reputation Points: 10
Solved Threads: 0
Light Poster
cl3m0ns is offline Offline
30 posts
since Oct 2007
Dec 2nd, 2007
0

Re: Reading/writing .txt files into an array

The input line can be split using stringstream class
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2007
0

Re: Reading/writing .txt files into an array

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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006

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: Just a lil fun
Next Thread in C++ Forum Timeline: C++ for loops





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


Follow us on Twitter


© 2011 DaniWeb® LLC