This program is suppose to be a very easy code of a simple time machine. I have it all layed out and working, but the out put it not computing correctly.. I know it is something easy, but i have been staring at this code for a bit now, so it could easly be missed. what is wrong, or what am i missing?

here is my code:

#include <iostream>

using namespace std;

int computeDifference(int hours1, int mins1, int hours2, int mins2, bool isPM1, bool isPM2); //calculates the time difference between the starting and ending time in minutes

int main()
{
    int hours1, mins1, hours2, mins2;
    bool isPM1 = 0, isPM2 = 0;
    char time1, time2;
    
    cout << "Please enter the starting hour: \n";
    cin >> hours1;
    cout << "Please enter the starting minute: \n"; 
    cin >> mins1;
    cout << "Please indicate whether it is AM or PM (enter A for AM; enter P for PM) \n";
    cin >> time1;
    if ((time1 == 'p') || (time1 == 'P'))
       {
       isPM1 = 1;
       }
   
    cout << "Please enter the hour you would like to travel to: \n";
    cin >> hours2;
    cout << "Please enter the minute of the hour you would like to travel to: \n";
    cin >> mins2;
    cout << "Please indicate whether it is AM or PM (enter A for AM; enter P for PM) \n";
    cin >> time2;
    if ((time2 == 'p') || (time2 == 'P'))
       {
       isPM2 = 1;
       }
    
    
    
    cout << "The time difference is: ";
    cout << computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2);
    if (computeDifference(hours1, mins1, hours2, mins2, isPM1, isPM2) == 1)
       cout << " minute.\n";
    else
       cout << " minutes.\n";


    
    system("PAUSE");
    return 0; 
}
int computeDifference(int hours1, int mins1, int hours2, int mins2, bool isPM1, bool isPM2)
{
 int difference, total_mins1, total_mins2;

 if (isPM1)
 {
    if ((hours1 >= 1) && (hours1 < 12))
    {
       hours1 += 12;
    }
 }
 if (isPM2)
 {
    if (hours2 >= 1 && hours2 < 12)
    {
    hours2 += 12;
    }
 }
 total_mins1 = (hours1 * 60) + mins1;
 total_mins2 = (hours2 * 60) + mins2;

 if ((hours1 >= hours2) || ((hours1 == hours2) && (mins1 > mins2)))
 {
 total_mins2 += 1440;          
 }
 
 
 difference = total_mins2 - total_mins1;
 
 if (difference > 1440)
 {
    difference -= 1440;
 }
 return difference;
}

Recommended Answers

All 2 Replies

I'm not sure what you are trying to do between lines 70-81. if you want to find the difference between the 2 times you do return total_min1 - total_min2; . If you want to get to the absolute difference between the two times than you can use the abs function provided in <cstdlib> like this.

return abs(total_mins1 - total_mins2);

Or you could do this.

return total_mins1 - total_mins2 > 0 ? total_mins1 - total_mins2 : -1 * total_mins1 - total_mins2;

// or to make it easier to read

int total_difference = total_mins1 - total_mins2;
return total_difference > 0 ? total_difference : -1 * total_difference;

Alright. i got it thank you for the help

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.