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

Recommended Answers

All 2 Replies

include <time.h>

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

This might help you

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.