# Thread: turning my logic into code

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. cin << Hours

needs to be

cin >> Hours

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

3. ## Re: turning my logic into code

I'll try to be a bit more helpful

Code:
```#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. 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...

http://contests.cprogramming.com/04.zip

in this file there is another file by name "alarm+vasanth.zip" which is mine.. contains the above...

this might give you some idea

Popular pages Recent additions