w3resource

C++ Exercises: Find the total number of minutes between two given times


Minutes Between Two Times

Write a C++ program to find the total number of minutes between two given times (formatted with a colon and am or pm).

Visual Presentation:

C++ Exercises: Find the total number of minutes between two given times

Sample Solution:

C++ Code :

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// Function to calculate the difference in minutes between two times
int count_minutes(string time_str) {
    // Variables to store time components
    string num1, num2;
    string hr1, hr2, min1, min2;
    int index = time_str.find('-');
    bool colon = false;

    // Iterating through the time string to extract hour and minute values
    for (int x = 0; x < time_str.length(); x++) {
        if (x >= index) {
            colon = false;
            for (x; x < time_str.length(); x++) {
                if (time_str[x] == ':') {
                    colon = true;
                    continue;
                }

                if (time_str[x] == 'A' || time_str[x] == 'P') {
                    num2.push_back(time_str[x]);
                    num2.push_back(time_str[x + 1]);
                }

                if (colon && isdigit(time_str[x])) {
                    min2.push_back(time_str[x]);
                } else if (isdigit(time_str[x])) {
                    hr2.push_back(time_str[x]);
                }
            }
        } else {
            if (time_str[x] == ':') {
                colon = true;
                continue;
            }

            if (time_str[x] == 'A' || time_str[x] == 'P') {
                num1.push_back(time_str[x]);
                num1.push_back(time_str[x + 1]);
            }

            if (colon && isdigit(time_str[x])) {
                min1.push_back(time_str[x]);
            } else if (isdigit(time_str[x])) {
                hr1.push_back(time_str[x]);
            }
        }
    }

    // Calculating the hour difference
    int hr = 0;
    if (stoi(hr1) == stoi(hr2) && num1 == num2 && stoi(min1) > stoi(min2)) {
        hr = 24 - (stoi(hr1) - stoi(hr2));
    } else if (stoi(hr1) > stoi(hr2) && num1 == num2 && stoi(min1) < stoi(min2)) {
        hr = 24 - (stoi(hr1) - stoi(hr2));
    } else if (num1 == num2 || (hr2 == "12" && hr1 != "12")) {
        hr = stoi(hr2) - stoi(hr1);
    } else {
        hr = (12 - stoi(hr1)) + stoi(hr2);
    }

    // Calculating the total time difference in minutes
    int time;
    if (min1 != "00") {
        time = (hr * 60 - stoi(min1)) + stoi(min2);
    } else {
        time = (hr * 60 + stoi(min1)) + stoi(min2);
    }

    return time;
}

// Main function to test the count_minutes function with different time inputs
int main() {
    cout << "Minutes between 12:01AM to 12:00PM: "<< count_minutes("12:01PM-12:00PM") << endl;
    cout << "Minutes between 2:12AM to 2:8AM: "<< count_minutes("2:12AM-2:8AM") << endl;
    cout << "Minutes between 1:04PM to 1:29PM: "<< count_minutes("1:04PM-1:29PM") << endl;
    cout << "Minutes between 2:00PM to 10:00PM: "<< count_minutes("2:00PM-10:00PM") << endl;
    return 0;
}

Sample Output:

Minutes between 12:01AM to 12:00PM: 1439
Minutes between 2:12AM to 2:8AM: 1436
Minutes between 1:04PM to 1:29PM: 25
Minutes between 2:00PM to 10:00PM: 480

Flowchart:

Flowchart: Find the total number of minutes between two given times.

For more Practice: Solve these Related Problems:

  • Write a C++ program to calculate the total minutes between two times given in 12-hour format with AM/PM.
  • Write a C++ program that converts two time strings into minutes since midnight and then computes the difference.
  • Write a C++ program to determine the number of minutes between two times by parsing the input string and handling edge cases at midnight.
  • Write a C++ program that reads two times with a colon separator and AM/PM markers, then calculates the total elapsed minutes accurately.

Go to:


PREV : Check Arithmetic or Geometric Sequence.
NEXT : Sum of Digits Between Two Integers .

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.