In: Computer Science
Create a function called time2Greeting.
It takes a time (in military time) and returns a string with the right greeting.
Good Morning 4AM to before noon.
Good Afternoon Noon to before 5PM
Good Evening from 5PM to 11PM
What are you doing up at this hour? between 11 and 4AM
For illegal values, say:
That is not a valid time.
Example:
What is your name? John
What time is it? 1315
Good afternoon, John.
C++ programming
Please find the C++ code for the following:
Code:
#include <iostream>
#include<cstring>
using namespace std;
//Defined a method which takes a time (int) and returns a string
with the right greeting.
string time2Greeting(int militaryTime)
{
//Check for - 4AM to before noon.
if(militaryTime>=0400 && militaryTime<1200)
return "Good Morning";
//Check for - Noon to before 5PM
else if(militaryTime>=1200 &&
militaryTime<1700)
return "Good Afternoon";
//Check for - from 5PM to 11PM
else if(militaryTime>=1700 &&
militaryTime<2300)
return "Good Evening";
//Check for between 11 and 4AM
else if( (militaryTime>=2300 && militaryTime<2400) ||
(militaryTime>=0000 && militaryTime<0400))
return "What are you doing up at this hour?";
//Else part
else
return "That is not a valid time.";
}
int main()
{
string name;
int Mtime;
//Prompt the name and time from the user
cout<<"What is your name? ";
cin>>name;
cout<<"What time is it? ";
cin>>Mtime;
//call the function and then print the output
cout<<time2Greeting(Mtime)<<", "<<name;
return 0;
}
Please check the
compiled program and its output for your reference:
Output:
(I believe that I made the code simple and understandable. If you
still have any query, Feel free to drop me a comment)
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...