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

write an algorithm function called balance() in javascript that takes in a string and returns a...
write an algorithm function called balance() in javascript that takes in a string and returns a strinf with balanced parantheses only. the string should be able to contain only parantheses, numbers, and letters. include comments of each portion of your code balance(“()()”) should return “()()” balance(“())”) should return “()” balance(“a(b)c”) should return “a(b)c” balance(“a(b)c())”) should return “a(b)c()”
(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
Write a function that takes a string, returns void and has the effect of shifting the...
Write a function that takes a string, returns void and has the effect of shifting the vowels of the string to the left. For example, if the input string was “hello class”, then after calling this function the string should contain “holla cless” (note that the first vowel is moved to the location of the last vowel). You can use an auxillary string if you want, but a nicer solution would work “in-place”
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
Ø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...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT