Hello, I have been trying to figure out how to do the homework assignment but cannot come to a conclusion. Looked all over for reference but didn't find anything. I did write a piece of code that works but is not what the assignment asks. Basically I am stuck and do not know how to proceed. Maybe someone can explain to me how to do this exactly or give me an idea?
Assignment: Write a program in ~/lab1/lab1b.cpp that outputs "Good morning <instructor>", "Good afternoon <instructor>", or "Good evening <instructor>" depending on whether the current time is midnight-noon, noon-sunset, or sunset-midnight respectively.
Your program should first prompt the user for the sunset time with the following prompts, inputting data using cin after each prompt:
Enter the hours part of today's sunset time (1-12):
Enter the minutes part of today's sunset time (0-59):
Make sure that your program does error checking and re-prompts the user if needed.
You will also need to retrieve the current time (24 hour format). A standard platform-independent way to do this is through the ctime library. Since you have not covered enough C/C++ to use this yet, you can just cut/paste the following sequence to get the current hour:
time_t t;
struct tm *now;
t = time(0); // get current time
now = localtime(&t); // adjust for local timezone
int hour = now->tm_hour; // retrieve current hour
int min = now->tm_min; // retrieve current min
You will also need to include the ctime library (#include <ctime>).
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
|
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
int hour;
time_t t;
int sunset;
cout << "Enter the hours part of today's sunset time (1-12):";
cin >> sunset;
{
time_t t;
struct tm *now;
t = time(0); // get current time
now = localtime(&t); // adjust for local timezone
int hour = now->tm_hour; // retrieve current hour
cout << hour;
cout << t;
}
if (hour < 12)
{
cout << "Good Morning Instructor";
}
else if ((hour > 12) && (hour < sunset + 12))
{
cout << "Good Afternoon Instructor";
}
else if ((hour > sunset + 12) && (hour<= 24))
{
cout << "Good Evening Instructor";
}
// else
{
// cout << "Unknown Time, please try again";
}
}
|