Hi I have created a program in C++, (which I am very new too). This program basically asks the used for two times such as 3:57 and the duration 1:05 and it will tell you the sum and the difference between the two. The problem is I am getting negative remainders such as if I put an input of 1:18 and 10:39 I expect 11:57 and 2:39 but I get 11:57 and -9:-21 as result when I run the program. What should I do? This is for an assignment and we cannot use if statements. The instructions say if we are having this problem we should " This is easily done by adding a day (or two or three) to before when calculating the difference." I do not know what this means and have been struggling on this. i pass most tests besides when the time goes negative code is below:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int run()
{
cout << "Give me a time (such as 3:57) and a duration" << endl;
cout << "(such 1:05), and will tell you the sum" << endl;
cout << "(that is the time that follows the given time" << endl;
cout << "by the given duration), and difference (the time that proceeds the given time by that duration)." << endl;
cout << "Time: " << endl;
int timeHours;
int timeMinutes;
char discard;
cin >> timeHours >> discard >> timeMinutes;
cout <<"Duration: " << endl;
int durationHours;
int durationMinutes;
cin >> durationHours >> discard >> durationMinutes;
int time = timeHours * 60 + timeMinutes;
int duration = durationHours * 60 + durationMinutes;
int after = time + duration;
int before = time - duration ;
int afterHours = after / 60 % 12 ;
int afterMinutes = after % 60;
int beforeHours = before / 60 ;
int beforeMinutes = before % 60;
cout << endl;
cout << durationHours <<":" << durationMinutes << " hours after and before, "
<< timeHours << ":" <<timeMinutes << " is [" << afterHours << ":" << setw(2) <<
setfill('0')<< afterMinutes <<", "
<< beforeHours << ":" << beforeMinutes << "]" << endl;
return(0);
}
Any help to make this program work properly is greatly appreciated. I am new to C++ and very frustrated.