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
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
#include <iostream>
#include <ctime>
int main()
{
time_t currentTime;
struct tm *localTime;
time( ¤tTime ); // Get the current time
localTime = localtime( ¤tTime ); // 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;
}
#include <iostream>
#include <ctime>
int main() {
time_t currentTime;
struct tm *local time;
time( ¤tTime );
localTime = localtime( ¤tTime );
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.