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 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 a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
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...
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 <<...
Write a program that request a time interval in seconds and display it in hours, minutes,...
Write a program that request a time interval in seconds and display it in hours, minutes, second format. (java)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT