If Else Statment with ctime condition

I am try to make a menu for deliver order program.

The menu divide into 3 group which are breakfast, lunch and dinner.
However breaker only available at 6.30am-10pm, lunch available at 11am-1.30pm
and dinner only available at 6.00pm-8.30pm

Let say my current time on the system is 10.30am, and the user select breakfast order. It should not available at this time, and prompt a message to use that the order not available at this period.

How to write the program?
Try using the <ctime> library, particularly the localtime function:
http://www.cplusplus.com/reference/ctime/localtime/

And the struct it stores it in:
http://www.cplusplus.com/reference/ctime/tm/
I know how to make current time...

Let say my current time is 10.30am, i need to prompt message to user that order is unavailable if the user order any menu.

How to use if...else statement or while statement or switch-case statement for this condition?


An example program using ctime and the current system time, based on what you described.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <ctime>

int main(int argc, char** argv)

{

	time_t rawtime;
	struct tm timeinfo;
	char buffer[80];

	while (true) {

		time(&rawtime);
		localtime_s(&timeinfo, &rawtime);

		strftime(buffer, 80, "The time is currently %I:%M%p.", &timeinfo);
		puts(buffer);

		std::cout << "\n\nWelcome to the C++ Forum's Delivery Order System! Please order from our menu below by typing in the number which corresponds with what you want:";

		std::cout << "\n\n1. Pancakes";
		std::cout << "\n2. Bacon and Eggs";
		std::cout << "\n3. French Toast";
		std::cout << "\n4. Chicken Sandwich";
		std::cout << "\n5. BLT Sandwich";
		std::cout << "\n6. Hamburger";
		std::cout << "\n7. CheeseBurger";
		std::cout << "\n8. Steak and Mashed Potatoes";
		std::cout << "\n9. Meatloaf";
		std::cout << "\n10. Spaghetti and Meatballs";
		std::cout << "\n11. Roasted Chicken with Rice and Beans";

		std::cout << "\n\nexit. Exit Program";

		std::string Input;

		std::cout << "\n\n What would you like? ";

		std::cin >> Input;

		std::stringstream StringConverter(Input);

		if (Input == "exit" || Input == "quit") {

			std::cout << "\n\nThank you for stopping by!\n\n";

			break;

		}

		int UserChoice;

		StringConverter >> UserChoice;

		if (UserChoice < 1 || UserChoice > 11) {

			std::cout << "\n\nThank you for stopping by!\n\n";

			break;

		}
		if (UserChoice > 0 && UserChoice < 4) {

			/*
			
			Breakfast item.
			
			*/

			if (timeinfo.tm_hour > 4 && timeinfo.tm_hour < 12) {

				std::cout << "\n\nYour breakfast item is coming right up!\n\n";

			}
			else {

				std::cout << "\n\nSorry, we are not serving breakfast right now!\n\n";

			}

		}
		else if (UserChoice > 3 && UserChoice < 8) {

			/*
			
			Lunch Item.
			
			*/

			if (timeinfo.tm_hour > 11 && timeinfo.tm_hour < 17) {

				std::cout << "\n\nYour lunch item is coming right up!\n\n";

			}
			else {

				std::cout << "\n\nSorry, we are not serving lunch right now!\n\n";

			}

		}
		else if (UserChoice > 8 && UserChoice < 12) {

			/*
			
			Dinner item.
			
			*/

			if (timeinfo.tm_hour > 16 && timeinfo.tm_hour < 24) {

				std::cout << "\n\nYour dinner item is coming right up!\n\n";

			}
			else {

				std::cout << "\n\nSorry, we are not serving dinner right now!\n\n";

			}

		}

	}

	return EXIT_SUCCESS;

}
#include<ctime>

//
Get current time
//
if, switch...
Topic archived. No new replies allowed.