Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Author:Briant Anaya
- //Goal: Succesfully get total hours worked for worker and display hours worked for a specific month. All data is collected from a file
- //Preprocessor declaration
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- //User Defined Data Type
- struct Calendar
- {
- string month;
- int hours4month;
- Calendar()
- {
- month = "Not entered";
- hours4month = 0;
- }
- };
- int main()
- {
- //Ask user for file and open file
- cout << "Please enter path to file: ";
- string path;
- getline(cin, path);
- ifstream file;
- file.open(path);
- if (file.is_open())
- {
- cout << "File opened successfully." << endl;
- int i = 0;
- int hours[100];
- string months[100];
- Calendar work[12];
- int sumHrs = 0;
- string temp;
- while (file.peek() != EOF)//Keep reading from file and taking data until the end of file character has been rached
- {
- file >> temp;
- months[i] = temp.substr(0, 3);
- file >> hours[i];
- sumHrs += hours[i];
- i++;
- }
- cout << "Total hours worked: " << sumHrs << endl;
- for (int x = 0; x < i; x++)//Populate the Struct Month array with unique months and specific hours for that month
- {
- for (int z = 0; z < 12; z++)
- {
- if (work[z].month == months[x])
- {
- work[z].hours4month += hours[x];
- break;
- }
- else if (work[z].month == "Not entered")
- {
- work[z].month = months[x];
- work[z].hours4month += hours[x];
- break;
- }
- }
- }
- for (int p = 0; p < i; p++)//Display months with hours
- {
- if (work[p].hours4month > 0)
- {
- cout << work[p].month << " " << work[p].hours4month << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement