Lab07b - Time Clock

About this Page

User inputs the time he/she began and finished work. The program calculates the amount of time the user worked. The program displays the amout of time the user worked

/* Title     - Time Clock
 * File name - Lab07
 * Programmer- 415 Erich Musick
 * IPO       - Input - The user inputs the time he/she began and finished work
               Processing - The program calculates the amount of time the user
                worked
               Output - The program displays the amout of time the user worked
*/

//---------------------Start Header Files------------------------------------

#include "waittoclose.h"    // Add to project waittoclose.cpp
#pragma hdrstop
//#include "iostream.h"
//#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen.h"      // Add to project randgen.cpp
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h"     // DO NOT ADD TO PROJECT apmatrix.cpp

//----------------------End Header Files-------------------------------------

//-------------------------Start Main----------------------------------------
int main() {
  int startHour;
  int startMinute;
  int startAllMinutes;
  char colon;
  int endHour;
  int endMinute;
  int endAllMinutes;
  const int minutesInAnHour = 60;
  const int minutesInAday = 1400;
  int minDiffPlusMinInADay;
  int totalMinutesWorked;
  int minutesWorked;
  int hoursWorked;

  /* Prompt the user for the time he/she began working */
  cout << "Start  : ";
  cin >> startHour;
  cin >> colon;
  cin >> startMinute;

  /* Prompt the user for the time he/she finished working */
  cout << "End    : ";
  cin >> endHour;
  cin >> colon;
  cin >> endMinute;

  /* Convert the start hours to minutes and add them to startAllMinutes */
  startAllMinutes = startHour * minutesInAnHour;
  startAllMinutes = startAllMinutes + startMinute;

  /* Convert the end hours to minutes and add them to endAllMinutes */
  endAllMinutes = endHour * minutesInAnHour;
  endAllMinutes = endAllMinutes + endMinute;

  /* Find the total number of minutes worked, even the time passes midnight*/
  minDiffPlusMinInADay = minutesInAday + endAllMinutes - startAllMinutes;
  totalMinutesWorked = minDiffPlusMinInADay % minutesInAday;

  /* Convert the total number of minutes worked to hours and minutes */
  hoursWorked = totalMinutesWorked / minutesInAnHour;
  minutesWorked = totalMinutesWorked % minutesInAnHour;

  /* Output total time worked */
  cout << "Worked : "
       << setfill('0')
       << setw(2)
       << hoursWorked
       << ":"
       << setfill('0')
       << setw(2)
       << minutesWorked
       << endl;
  return 0;
}
//--------------------------End Main-----------------------------------------

Return to C++ Snippetts