0

Write a program that prompts the user to input the start time and the stop time in hours and minutes (use 24 hour format), and then print out the time spent in work in hours and minutes.
For example:
Enter start time : 9 30
Enter finish time : 13 15
Time spent at work is 3 hours and 45 minutes

this is my code

#include<iostream>
   using namespace std;

   int main()
   {
   int hour1 , hour2 , min1 , min2 ,spendHour, spendMin;
   cout<< "Enter start Time : " << endl;
   cin >> hour1 >> min1;

   cout << "Enter finished time : " << endl;
   cin >> hour2 >> min2;

   spendHour = hour2 - hour1;
   spendMin = min2 - min1;

   cout << "Time spend at work is " << spendHour << "hour and" << spendMin << "Minutes" << endl;
   return 0;
   }

My output come out with a negative

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by subith86
0

include <time.h>

in your int main(), type this
double differ; // declare variable
differ = difftime (end_time,start_time);

This might help you

0
if (0 > spendMinute)
{
    spendMinute = 60 + spendMinute;
    spendHours-- ;
}

Add this piece of code at line 15. It will work

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.