Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. //Author:Briant Anaya
  2. //Goal: Succesfully get total hours worked for worker and display hours worked for a specific month. All data is collected from a file
  3.  
  4. //Preprocessor declaration
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. using namespace std;
  9.  
  10.  
  11. //User Defined Data Type
  12. struct Calendar
  13. {
  14.     string month;
  15.     int hours4month;
  16.  
  17.     Calendar()
  18.     {
  19.         month = "Not entered";
  20.         hours4month = 0;
  21.     }
  22. };
  23.  
  24. int main()
  25. {
  26.     //Ask user for file and open file
  27.     cout << "Please enter path to file: ";
  28.     string path;
  29.     getline(cin, path);
  30.     ifstream file;
  31.     file.open(path);
  32.  
  33.     if (file.is_open())
  34.     {
  35.         cout << "File opened successfully." << endl;
  36.         int i = 0;
  37.         int hours[100];
  38.         string months[100];
  39.         Calendar work[12];
  40.         int sumHrs = 0;
  41.         string temp;
  42.  
  43.         while (file.peek() != EOF)//Keep reading from file and taking data until the end of file character has been rached
  44.         {
  45.             file >> temp;
  46.             months[i] = temp.substr(0, 3);
  47.             file >> hours[i];
  48.             sumHrs += hours[i];
  49.             i++;
  50.  
  51.         }
  52.         cout << "Total hours worked: " << sumHrs << endl;
  53.  
  54.    
  55.         for (int x = 0; x < i; x++)//Populate the Struct Month array with unique months and specific hours for that month
  56.         {
  57.             for (int z = 0; z < 12; z++)
  58.             {
  59.                 if (work[z].month == months[x])
  60.                 {
  61.                     work[z].hours4month += hours[x];
  62.                     break;
  63.                 }
  64.                
  65.                 else if (work[z].month == "Not entered")
  66.                 {
  67.                     work[z].month = months[x];
  68.                     work[z].hours4month += hours[x];
  69.                     break;
  70.                 }
  71.             }
  72.         }
  73.  
  74.         for (int p = 0; p < i; p++)//Display months with hours
  75.         {
  76.             if (work[p].hours4month > 0)
  77.             {
  78.                 cout << work[p].month << " " << work[p].hours4month << endl;
  79.             }
  80.         }
  81.  
  82.     }
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement