C++ program to print a message based on the time of the day

              A day has morning, afternoon, evening and night. If the program displays a greeting message based on the time, it will be nice. Let us try this concept in C++.

There are three methods to print the message.

  • 1.      Based on the user input.
  • 2.      Based on the system time.
  • 3.      By using functions.

Let us implement this.

1.    Based on the user input.

    This program reads the input from the user. According to the input, the time message is displayed.

#include <iostream>

using namespace std;

int main() {

    int t_hour;

    cout << "Enter the hour from the value 0-23: ";

    cin >> t_hour;

    if (t_hour >= 0 && t_hour < 12) {

        cout << "Good Morning! Have a nice day" << endl;

    } else if (t_hour >= 12 && t_hour < 18) {

        cout <<"Good Afternoon!Half day is there " << endl;

    } else if (t_hour >= 18 && t_hour <= 23) {

        cout << "Good Evening. The day is yet to complete!" << endl;

    } else {

        cout << "Invalid hour entered!" << endl;

    }

    return 0;

}

Output:

Enter the hour from the value 0-23: 9

Good Morning! Have a nice day

Next method is given below.

2.Based on the system time.

It reads the system time. Based on the time, the message is displayed.

#include <iostream>

#include <ctime>

using namespace std;

int main() {

    time_t t_now = time(0);

    tm *localTime = localtime(&t_now);

    int t_hour = localTime->tm_hour;

    if (t_hour >= 0 && t_hour < 12) {

        cout << "Good Morning!Have a nice day" << endl;

    } else if (t_hour >= 12 && t_hour < 18) {

        cout << "Good Afternoon. The half day is there!" << endl;

    } else {

        cout << "Good Evening! The day is yet to complete" << endl;

    }

    return 0;

}

Output: Good Afternoon. The half day is there!

Last one is given below…

3.By using functions.

This program creates a function display_Greetings(). This function processes the time

and displays the message.

#include <iostream>

#include <ctime>

using namespace std;

void display_Greetings(int t_hour) {

    if (t_hour >= 0 && t_hour < 12) {

        cout << "Good Morning! Have a nice day" << endl;

    } else if (t_hour >= 12 && t_hour < 18) {

        cout << "Good Afternoon! Half day is more" << endl;

    } else if (t_hour >= 18 && t_hour <= 23) {

        cout << "Good Evening! The day is yet to complete" << endl;

    } else {

        cout << "Please enter valid hour!" << endl;

    }

}

int main() {

    time_t t_now = time(0);

    tm *localTime = localtime(&t_now);

    int t_hour = localTime->tm_hour;

    display_Greetings(t_hour);

    return 0;

}

Output:

Good Morning! Have a nice day

These are the three methods to print a message based on the time of the day in C++. Hope, this code is useful to you. Keep coding!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.