View Single Post
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