Question

In: Computer Science

Create a function called time2Greeting. It takes a time (in military time) and returns a string...

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

Solutions

Expert Solution

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...


Related Solutions

(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the...
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the area of a regular hexagon given the length of the side. Show that the function works in a worksheet by inserting the formula somewhere.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
ØWrite a method that takes a string representation of time in hours:minutes:seconds and returns the total...
ØWrite a method that takes a string representation of time in hours:minutes:seconds and returns the total number of seconds •For example, if the input is “0:02:20” it would return 140 ØIf the input string is missing the hours component it should still work •For example, input of “10:45” would return 645 ØWrite an overloaded form of the method that takes in only hours and minutes and returns the total number of seconds •It should leverage the first method you wrote...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT