Entry Level help.

This is a discussion on Entry Level help. within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that tells what time it is (am or pm) and what day of ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    32

    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("&#37;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);
    Last edited by alex1067; 03-10-2008 at 08:55 PM.

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    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

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    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;

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Ok sorry I thought I was in the C programming section. I must have had it bookmarked wrong. Thank you

  8. #8
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    just repost in the C section then.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Since this has been posted in the C programming forum...

    thread closed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  2. Listing binary tree numbers by level
    By Nazgulled in forum C Programming
    Replies: 5
    Last Post: 06-16-2008, 10:36 AM
  3. Entry Level Help.
    By alex1067 in forum C Programming
    Replies: 21
    Last Post: 03-11-2008, 05:13 PM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. Salary for entry level C position- How much can you make?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-10-2002, 11:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21