-
Entry Level help.
I am trying to write a program that tells what time it is (am or pm) and what day of week it is when the user enters an hour number between 0 and 167 (7 days Monday at midnight starts at 0)
Heres what I have so far. I am working on the days of the week:
Code:
7 #include <stdio.h>
8
9 int main(void)
10
11 {
12
13 int hour;
14 #define day (Monday || Tuesday || Wednesday || Thursday || Friday || Saturday || Sunday)
15
16 printf("Enter hours: ");
17 scanf("%d", &hour);
18
19 if (hour < 24)
20 day = Monday;
21 else if (hour > 23 || hour < 48)
22 day = Tuesday;
23 else if (hour > 47 || hour < 72)
24 day = Wednesday;
25 else if (hour > 71 || hour < 96)
26 day = Thursday;
27 else if (hour > 95 || hour < 120)
28 day = Friday;
29 else if (hour > 119 || hour < 144)
30 day = Saturday;
31 else ( hour > 143 || hour < 168)
32 day = Sunday;
33
34 printf("Result: %d\n", day);
-
It looks like you're trying to do this in C, not C++.
You'd probably rather use an enumerative type for the day.
Check this out on enumerative types.
Since enumerative types have an integer value associated with them, a switch statement might be cleaner.
Check this out on using a switch statement.
-
Yes it is in C. I am having trouble what to use, a while loop? Cases? else if? Here are the directions.
Code:
10 DIRECTIONS: Write a C program which will prompt the user for
11 an int representing hours. This value will be in the range 0 - 167
12 inclusive. Your program should then display
13 a day and time (hours only) followed by am or pm. Assume time begins
14 at midnight 12am Monday.
15
16 See the examples below:
17
18
19 EXAMPLES:
20
21 Enter hours: 0
22 Result: Monday, 12 midnight
23 PROGRAM ENDS
24
25 Enter hours: 12
26 Result: Monday, 12 noon
27 PROGRAM ENDS
-
Can I do this
Code:
#define Monday 0 < hour < 24
#define Tuesday 23 < hour < 48
#define Wednesday 47 < hour < 72
#define Thursday 71 < hour < 96
#define Friday 95 < hour < 120
#define Saturday 119 < hour < 144
#define Sunday 143 < hour < 168
int hour;
-
Perhaps this should be moved to the C - board.
Anyways, are you familiar with pseudo-code (the steps your program will take to solve this problem without actually coding it)? That might be a helpful step.
Also, this program will greatly benefit from the use of functions.
-
I personally wouldn't use all the #define statements.
A simple solution might be to use the if-else if-else statement like you have to get the day, then use a function on the remaining integer value to calculate the hour of the day.
-
Ok sorry I thought I was in the C programming section. I must have had it bookmarked wrong. Thank you
-
just repost in the C section then.
-
Since this has been posted in the C programming forum...
thread closed.