Using current time as the condition in selection structure

Hi, here is my condition...I was given a menu which is restricted to time, I.E: breakfast only available from 8 to 10...and so I want to allow the user make order according to the time. I am planning to use if...else with the condition current time...Is it possible??Thank you so much for your attention.
so you want to use time, as in like system time in your program? For example, ask the computer for the time, and use that in your program? If so, I think that would be dependent on your operating system, and you would probably have to use a library supplied by it. Edit1: here is a relevant Stack Exchange answer, I think:
http://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
Last edited on
Actually I am a newbie in programming. I know nothing much about function in c++... Actually i had tried to google it, but the answers are all complicated and I don't understand... I had read the page u shared before, but i stll can't implement it in my program...I mean in the if...else condition...or apart from if...else do u have any other suggestion on how i can restrict the user input according to time???thanks
I found some code that explains the thingie I linked above a bit more, here it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ctime>

int main()
{
  time_t currentTime;
  struct tm *localTime;

  time( &currentTime );                   // Get the current time
  localTime = localtime( &currentTime );  // Convert the current time to the local time

  int Day    = localTime->tm_mday;
  int Month  = localTime->tm_mon + 1;
  int Year   = localTime->tm_year + 1900;
  int Hour   = localTime->tm_hour;     
  int Min    = localTime->tm_min;
  int Sec    = localTime->tm_sec;

  std::cout << "This program was exectued at: " << Hour << ":" << Min << ":" << Sec << std::endl;
  std::cout << "And the current date is: " << Day << "/" << Month << "/" << Year << std::endl;
  return 0;
}


source: http://runnable.com/U2DKRsrlaUEEYt4f/get-the-current-local-time-with-ctime-for-c%2B%2B

For your problem, I recommend doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <ctime>

int main() {

time_t currentTime;
struct tm *local time;

time( &currentTime );
 localTime = localtime( &currentTime );

int hour = localTime->tm_hour; 

//This is the part that checks the time
//Your part of the program

if(hour = 8) {cout << "Blah blah\n";}
else {cout << "Blah blah blah\n";}

return 0;
}


The above code is what I think you're looking for. Don't worry about time_t and that stuff for now. You'll get it as you learn more c++, and start using the standard library. For now, just worry about the hour variable, and the if else statement. Hour is always initialized to the hour the program is run, in a 24 hour format, and you can use if else statements then to direct your program.
It works!!! Oh, thanks a lot... It is so kind of you to give me so much help...
no problem
Topic archived. No new replies allowed.