Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm>
  6. using namespace std;
  7. struct HoursWorked
  8. {
  9. struct Date
  10. {
  11. string month;
  12. int day;
  13. string year;
  14. };
  15. int hours;
  16. Date date;
  17. string toString();
  18. };
  19. void openFile(std::ifstream & input);
  20. int populateArray(HoursWorked e[], ifstream & in);
  21. int main()
  22. {
  23. ifstream textFile;
  24. openFile(textFile);
  25. HoursWorked arr[100];
  26. int counter = populateArray(arr, textFile);
  27. for (size_t i = 0; i < counter; i++)
  28. {
  29. cout << arr[i].toString();
  30. }
  31. }
  32.  
  33. void openFile(std::ifstream & input)
  34. {
  35. std::string pathFile;
  36. bool openFileSucess = false;
  37. while (!openFileSucess)
  38. {
  39. input.clear();
  40. std::cout << "Please input a filename to read from \n";
  41. getline(std::cin, pathFile);
  42. pathFile.erase(remove(pathFile.begin(), pathFile.end(), '\"'), pathFile.end()); //Removes quotation marks to increase brevity of testing.
  43. input.open(pathFile);
  44. if (input)
  45. {
  46. input.peek();
  47. if (input.eof())
  48. {
  49. std::cout << "File oppened is empty! Please select another file.";
  50. }
  51. else
  52. {
  53. std::cout << "File oppened sucessfully!\n";
  54. openFileSucess = true;
  55. }
  56. }
  57. else
  58. {
  59. std::cout << "File path not found, please enter a proper path.\n";
  60. }
  61. }
  62. }
  63.  
  64. int populateArray(HoursWorked e[], ifstream & in)
  65. {
  66. string inputString;
  67. int hours;
  68. int counter = 0;
  69. if (in.eof())
  70. {
  71. cout << "Data file is empty. \n";
  72. return 0;
  73. }
  74. while (!in.eof())
  75. {
  76. if (counter < 100)
  77. {
  78. in >> inputString >> hours;
  79. e[counter].hours = hours;
  80. e[counter].date.month = inputString.substr(0, 3);
  81. e[counter].date.day = stoi(inputString.substr(4, 5));
  82. e[counter].date.year = inputString.substr(7, 11);
  83. counter++;
  84. }
  85. else
  86. {
  87. cout << "ERROR : Exceeded Maximium Capacity for employee records. Only 30 records are processed.\n";
  88. return counter;
  89. }
  90. }
  91. return counter;
  92. }
  93.  
  94. string HoursWorked::toString()
  95. {
  96. string ret = "The month is " + date.month + "\n" + "The date is " + to_string(date.day) + "\n"
  97. + "The year is " + date.year + "\n";
  98. return ret;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement