Thread: turning my logic into code

  1. #1

    turning my logic into code

    I am a newbie and was given an assignment by my instructor and a very brief lecture. I developed a good algorithm. Please review and see of you like what I have done so far.
    Problem: Given the current time (hrs & minutes) on a 24-hr clock, add a whole # of hours and determine what the new clock reading is and how many days later it is. (Hint: Use /24 and %24, designate midnight as 0:00 hours instead of 24:00 hours, Ex - 17:30 + 37 hours is 6:30, two days later)
    Here is my algorithm:
    1 Prompt user for hours
    2 store hours
    3 prompt user for minutes
    4 store minutes
    5 prompt user for additional hours
    6 store additional hours
    7 calculate elapsed days
    8 calculate new hours
    9 display elapsed days
    10 display new time

    Oaky, I am identifying Hours, Minutes, Additional hours, and elapsed days as my variables!?

    I can see that hours = 17; Minutes = 30; and Additional Hours = 37, do you agree?

    I can disect a problem but I am having trouble with the code part of it.

    int main()

    //I figured out from reading my book how the output would be

    cout << "Please enter hours:";
    cin << Hours
    return 0;
    I would appreciate any comments and direction. I will keep reading.

  2. #2
    Registered User
    Join Date
    Sep 2002
    cin << Hours

    needs to be

    cin >> Hours

    Thats not going to help a lot, but its a start :-P

  3. #3
    Registered User
    Join Date
    Sep 2002

    Re: turning my logic into code

    I'll try to be a bit more helpful

    #include <iostream>
    #include <time.h>
    using namespace std;
    int main()
          // you probably want the current time...
          time_t ltime;
          struct tm *today = localtime( & ltime );
          time( & ltime );
          //x.tm_hour, tm_isdst, tm_mday, tm_min, tm_mon, tm_sec, tm_wday, tm_yday, tm_year
          //specifically: x.tm_hour is hours since midnight
         // x.tm_min is how many minutes since start of hour
          // x.tm_sec is seconds
          int hours, minutes, morehours, days;
          cout << "How many hours? ";
          cin >> hours;
          // you should get the idea for input
          days = (hours+(minutes/60))/24;
          return 0;
    Thats part of it. I'll let you figure the rest out

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    I had built a alarm program before.. It runs under DOS.. just check it.. it did this work.. it had a present time counter.. and a time where the user could set (alarm time) and a third counter showed the remaining time along with sec and milliseconds etc...

    in this file there is another file by name "" which is mine.. contains the above...

    this might give you some idea

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM