Question
Determine the time period of the day in C++
a. Request an hour in military time between 0 and 23 inclusive. b. If the value for hour is invalid: 1. Output Invalid hour.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

int main()
{
int hour;
int minutes;

while(1)
{
cout<<"\nEnter an hour in military time between 0 and 23 : ";
cin>>hour;
if(hour>=0 && hour<=23)
{
break;
}
else
{
cout<<"\nInvalid hour\n";
}
}
cout<<"Enter minutes: ";
cin>>minutes;
printf("%d:%02d",hour,(minutes%60));

if(hour>12)
{
hour=hour-12;
printf("\n12-hours format: ");
printf("%d:%02d",hour,(minutes%60));
}

if(hour>=0 && hour<=12)
{
cout<<"\nIt is morning.";
}
else if(hour>=13 && hour<=17)
{
cout<<"\nIt is afternoon.";
}
else if(hour>=18 && hour<=19)
{
cout<<"\nIt is twilight.";
}
else
{
cout<<"\nIt is evening.";
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Determine the time period of the day in C++ a. Request an hour in military time...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone solve it with C plz Write a program that inputs a time from the...

    Can someone solve it with C plz Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would...

  • Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;   ...

    Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;    minutes = 0;    isAfternoon = false;    //check to make sure there are 5 characters    if (//condition to check if length of string is wrong)    {        cout << "You must enter a valid military time in the format 00:00" << endl;    }    else    {        //check to make sure the colon is in the correct...

  • programming logic C# visual studio Use an if…else-if…else statement to output the following based on an...

    programming logic C# visual studio Use an if…else-if…else statement to output the following based on an int time entered in military time (ie. 23 == 11:00 p.m., 11 == 11:00 a.m.). You will need to use a ReadLine() and then convert the input to an int value. Based on the input, output the following messages: Input          Output 0-11            Good Morning 12-16          Good Afternoon > 16            Good Evening

  • Java Store Time Clock Objects

    1. Create a Time class which is designed to contain objects representing times in 12-hour clock format. Eg: 6:58am, 11:13pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples.Provide a default constructor, a set method which is given a String (eg: “9:42am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate...

  • How can I modify the program below with this prompt asking "How many minutes from now...

    How can I modify the program below with this prompt asking "How many minutes from now do you expect to be home?", and output a sentence saying "You will get home at HH:MM". ?? It's very URGENT!! Thank you #include <iostream> #include <ctime> using namespace std; int main) time t t struct tm *now t-time (e) now-localtime(&t); int hour = now->tm.hour; // retrieve current hour int min = now->tm..min; // retrieve current min // get current time // adjust for...

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • POD 3: Tick Tock Instructions There are 24 hours in a day, but we all think...

    POD 3: Tick Tock Instructions There are 24 hours in a day, but we all think about them differently. Some people and places in the world work with a 24-hour clock that goes from 0:00 to 23:59, while others work with a 12-hour clock that goes from 12.00AM to 11:59PM 24-hour clock 0.00 (midnight) 1200 (noon) 23:59 (one minute before midnight), A COLLAPSE 12-hour clock: *12.00 AM" (midnight) *12:00 PM" (noon) "11:59 PM" (one minute before midnight), You are going...

  • In Python 3, write a function that will take an integer which represents a time given...

    In Python 3, write a function that will take an integer which represents a time given in military time (0 – 2359) and return a string that represents the time in a 12-hour format. If an invalid time is given, the function must return the string “Invalid time”. Examples 1200 will return the string “12:00 PM” 2240 will return the string “10:40 PM” 0600 will return the string “06:00 AM” 2400 will return the string “Invalid time” 1160 will return...

  • C++ Please! Given this definition of a Time class: class Time { private: int hour, min,...

    C++ Please! Given this definition of a Time class: class Time { private: int hour, min, sec; public: Time() { hour = 0; min = 0; sec = 0; } Time(int h, int m, int s) {      hour = h; min = m; sec = s; } int getHour() const {     return hour; } int getMin() const {     return minute; } int getSec() const {     return sec; } }; Derive a class MilTime from the Time...

  • PoD 4: Greetings all around! Instructions Good day! Bonjour! Good evening! Bonsoir! You are in a...

    PoD 4: Greetings all around! Instructions Good day! Bonjour! Good evening! Bonsoir! You are in a bilingual country! A nice thing to know is how to greet people in any language. You are going to write a program to make sure that we know how to greet the peoples around us. The French word "Bonjour" means "Good day. This is greeting can be used from morning until dusk (let's 18:00 or 6pml. After dusk we might say "Bonsoir" or "Good...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT