Question

In: Computer Science

I need program to calculate the number of seconds since midnight. For example, suppose the time...

I need program to calculate the number of seconds since midnight.
For example, suppose the time is 1:02:05 AM.
Since there are 3600 seconds per hour and 60 seconds per minutes,
it has been 3725 seconds since midnight (3600 * 1 + 2 * 60 + 5 = 3725).
The program asks the user to enter 4 pieces of information: hour, minute, second, and AM/PM.
The program will calculate and display the number of seconds since midnight.
Modify the program by adding error checking loops. Hour must be a number from 1 to 12.
Minute and second must be numbers from 0 to 59. Also check whether “AM” or “PM” is entered.
Every time an invalid value is entered,
display an error message and ask the user to re-enter a valid value immediately.
[Hint: be very careful when the hour is 12].

The following is an example:

Enter hour: 0
Hour must be from 1 to 12.
Enter hour: 13
Hour must be from 1 to 12.
Enter hour: 12
Enter minute: -1
Minute must be from 0 to 59.
Enter minute: 60
Minute must be from 0 to 59.
Enter minute: 14
Enter second: -1
Second must be from 0 to 59.
Enter second: 60
Second must be from 0 to 59.
Enter second: 47
Enter AM or PM: FM
Please enter AM or PM
Enter AM or PM: PM
Seconds since midnight: 44087

Solutions

Expert Solution

#include <iostream>

using namespace std;

int readInt(int min, int max, string msg) {
        int x;
        cout << msg;
        cin >> x;
        while(x < min || x > max) {
                cout << "Value must be from " << min << " to " << max << "." << endl;
                
                cout << msg;
                cin >> x;
        }
        return x;
}

int main() {
        int h, m, s;
        string amPm;

        h = readInt(1, 12, "Enter hour: ");
        m = readInt(0, 59, "Enter minute: ");
        s = readInt(0, 59, "Enter second: ");

        cout << "Enter AM or PM:";
        cin >> amPm;

        while(amPm != "AM" && amPm != "PM") {
                cout << "Please enter AM or PM" << endl;    
                cout << "Enter AM or PM:";
                cin >> amPm;
        }

        long seconds = 0;
        seconds += s;
        seconds += m * 60;

        if(h == 12) {
                h = 0;
        }

        seconds += h * 60 * 60;

        if(amPm == "PM") {
                seconds += 12 * 60 * 60;
        }

        cout << "Seconds since midnight: " << seconds << endl;

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
I need an example of how to calculate correlation in excel
I need an example of how to calculate correlation in excel
I need to time series plot the following data. I am confused since there are multiple...
I need to time series plot the following data. I am confused since there are multiple sections (441, 442, 443, 444, 445, 452, 4521. This is just two years worth of the data. Year Month Period 441 442,443 444 445 448 452 4521 1992 Jan 1 1.84 1.85 1.83 0.88 2.58 2.39 2.60 1992 Feb 2 1.83 1.85 1.83 0.89 2.62 2.34 2.53 1992 Mar 3 1.87 1.90 1.83 0.88 2.63 2.35 2.53 1992 April 4 1.89 1.91 1.91 0.89...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
I need yo know this answer by midnight tonight. thanks. you will need to construct your...
I need yo know this answer by midnight tonight. thanks. you will need to construct your own data sheet for this experiment. DATA ANALYSIS steps 1-2: The colorimeter wsa used to measure teh absorbance of each of a series of [FeSal]+ solutions. A standard curve was constructed by plotting the absorbance vs. the concentration (M). The slope and y-intercept were found to be 1510 M^-1 and 0.001, respectively. PROCEDURE: An aspirin sample was prepared, following the procedure given in the...
NEED IT FOR TOMORROW MIDNIGHT SUNDAY THE 8th PLEASE Calculate RNOA and decompose into net operating...
NEED IT FOR TOMORROW MIDNIGHT SUNDAY THE 8th PLEASE Calculate RNOA and decompose into net operating profit margin and operating asset turnover (Module 4). Describe inventory valuation and depreciation methods for the two companies and compare methods between the two companies (Module 6). Are there any substantial differences that should be adjusted for when comparing performance for the two companies? Balance Sheet For company A: CONSOLIDATED BALANCE SHEETS - USD ($) $ in Millions Feb. 03, 2018 Jan. 28, 2017...
Currency is an example of a number format. TRUE FALSE To extract the seconds from the...
Currency is an example of a number format. TRUE FALSE To extract the seconds from the time entered in cell B1 as 12:00:52 PM, type =SECOND(B1) in cell C1 and press ENTER. TRUE FALSE To combine multiple cells into one combined cell, which of the following do you use? Column Width command Center button Merge and Center button Increase Indent button In formulas, calculations in square brackets are calculated first. TRUE FALSE Nathan wants to run a macro from the...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
I need to prove this with some sort of counting... Suppose there are some number of...
I need to prove this with some sort of counting... Suppose there are some number of people in a room and we need need to consider all possible pairwise combinations of those people to compare their birthdays and look for matches.
I wrote a c++ program that is suppose to calculate the interest on a CD account...
I wrote a c++ program that is suppose to calculate the interest on a CD account but, I cant get the formula to calculate it correctly. please help. #include <iostream> #include <cmath> using namespace std; struct account { double balance; double interest_rate; int term; }; void info(account& accountinfo); int main(void) { double calc1, calc2; account accountinfo; info(accountinfo); calc1 = (accountinfo.interest_rate / 100) + 1; calc2 = pow(calc1, accountinfo.term); accountinfo.balance = accountinfo.balance * calc2; cout << " " << endl <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT