Program to Add Two Times (in minutes - seconds ) using C++ Structures

#include <iostream>
using namespace std;

struct TotalTime{
    int min;
    float sec;
}t1 , t2, sum;

int main()
{
    cout << "Enter information for 1st time," << endl;
    cout << "Enter minutes: ";
    cin >> t1.min;
    cout << "Enter seconds: ";
    cin >> t1.sec;

    cout << "\nEnter information for 2nd time" << endl;
    cout << "Enter minutes: ";
    cin >> t2.min;
    cout << "Enter seconds: ";
    cin >> t2.sec;

    sum.min = t1.min+t2.min;
    sum.sec = t1.sec+t2.sec;

    // changing to MINUTES if SECONDS is greater than or equal to 60
    if(sum.sec >= 60)
    {
        ++ sum.min;
        sum.sec -= 60;
    } 

    cout << endl << "Sum of time = " << sum.min << " minutes  " << sum.sec << " seconds";
    return 0;
}


No comments

Post your comments

Powered by Blogger.