Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- struct HoursWorked
- {
- struct Date
- {
- string month;
- int day;
- string year;
- };
- int hours;
- Date date;
- string toString();
- };
- void openFile(std::ifstream & input);
- int populateArray(HoursWorked e[], ifstream & in);
- int main()
- {
- ifstream textFile;
- openFile(textFile);
- HoursWorked arr[100];
- int counter = populateArray(arr, textFile);
- for (size_t i = 0; i < counter; i++)
- {
- cout << arr[i].toString();
- }
- }
- void openFile(std::ifstream & input)
- {
- std::string pathFile;
- bool openFileSucess = false;
- while (!openFileSucess)
- {
- input.clear();
- std::cout << "Please input a filename to read from \n";
- getline(std::cin, pathFile);
- pathFile.erase(remove(pathFile.begin(), pathFile.end(), '\"'), pathFile.end()); //Removes quotation marks to increase brevity of testing.
- input.open(pathFile);
- if (input)
- {
- input.peek();
- if (input.eof())
- {
- std::cout << "File oppened is empty! Please select another file.";
- }
- else
- {
- std::cout << "File oppened sucessfully!\n";
- openFileSucess = true;
- }
- }
- else
- {
- std::cout << "File path not found, please enter a proper path.\n";
- }
- }
- }
- int populateArray(HoursWorked e[], ifstream & in)
- {
- string inputString;
- int hours;
- int counter = 0;
- if (in.eof())
- {
- cout << "Data file is empty. \n";
- return 0;
- }
- while (!in.eof())
- {
- if (counter < 100)
- {
- in >> inputString >> hours;
- e[counter].hours = hours;
- e[counter].date.month = inputString.substr(0, 3);
- e[counter].date.day = stoi(inputString.substr(4, 5));
- e[counter].date.year = inputString.substr(7, 11);
- counter++;
- }
- else
- {
- cout << "ERROR : Exceeded Maximium Capacity for employee records. Only 30 records are processed.\n";
- return counter;
- }
- }
- return counter;
- }
- string HoursWorked::toString()
- {
- string ret = "The month is " + date.month + "\n" + "The date is " + to_string(date.day) + "\n"
- + "The year is " + date.year + "\n";
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement