Help with home work

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";
    }


}
Last edited on
A good practice for debugging is an organized check of certain common issues, particularly when using c&p code from someone else.

For example, put the code into wordpad or similar, then highlight all declarations, check for duplicates, check to make they are declared before use, etc. Then move on to function calls, then check scope brackets, etc.

Then you might find common errors, such as the one involving a particular declaration.
Thing is I don't know how to structure this properly. How should I make it so it gives me the hour based on the sunset time? The Sunset hours can only be from 1 pm through 12 am so based on the hour that is inputted it should give the message corresponding to the time of day. This is how I understand the assignment.
You are not basing anything on the sunset time.

Really, what you should do, is build the program in pieces.

First, make an outline. This is best done in excel or another spreadsheet program, so you can move outline pieces around as desired. Start off with each thing you want the program to do that will be seen when the program us run.
Example,
Display a welcome message depending on time

Then break down that goal into what you need the program to do to achieve it,
example,
Get the time,
Use the time to pick a message
Display the chosen message

Then you can add details,
Example,
Get time
Get cuurent time
Get sunset time
Pick one of three messages
msg one between 00 and 12
msg two between 12 and sunset
msg three between sunset and 24
display chosen message

Then you go through and identify what variables you need, where you need them, what parts will repeat, etc.

Basically, break things down till you have everything clarified, then arranged by what is needed to accomplish each step, and when. Then, you will see a basic working structure. May not always be the best structure when you do this for more complicated programs, but it will be a working structure that you can build from, then refine when have the expertise to make such refinements.
So if I am not basing anything on sunset time, why are they asking for user to input "Enter the hours part of today's sunset time (1-12):" ?
There is a difference between using a value and building stuff around that value. Perhaps just an odd way of speaking where I'm from, but generally if you are basing stuff on something, it means you are doing more than simply using that thing, it implies that thing is the focus of multiple constructions.

That is all that is.
Topic archived. No new replies allowed.