Hey,
I was wondering if any one could help me with several problems I am having.

1st problem:

Can anyone tell me how to combine two int's and put a ':' in between. Bascially I want the user to enter the hour and minutes separately, then combine them both together to look like a digital clock.
Example:

int hour = 10;
int minute = 30;

//then something like:

 hour + ':' + minute = thetime;

I'd appreciate it if anyone could help me on this matter. This is for an appointment program i am making.

2nd Problem:

I enter several variables for my program. Begintime and duration which are the main ones. Any ideas how I could make an overlap function to find out if the next begintime (booking) would clash with any other appointments made stored in the array. Such as I had a booking at 10.30 which lasted for an hour already. But then I wanted to add a booking for 11.00. There is a clash. I have no idea where to start on this function.

Thanks

Recommended Answers

All 7 Replies

You have to make them a string, such as

char buf[40];
int hour = 1;
int minute = 25;
sprintf(buf,"%02d:%02d", hour, minute);

That's the easiest way to do it. It can also be done using stringstream.

Or if all you want to do is display them cout << hour << ':' << minute << '\n';

Problem 2: put the times and their duration in an array, then search the array. You might need to use the complete day and time in case duration is beyond 24 hours.

struct booktime
{
   time_t starttime;
   time_t endtime; // start time plus duration
};

I think i need to look more into this time_t business. Thank's AncientDragon. So when the user enters the starttime it should be in this time_t datatype format?

enter the time as normal and the program converts it to time_t

int hour = 10;
int minute = 30;
int duration = 4; // 4 hours
struct tm* tm;
time_t theTime;

theTime = time(0); // get current date/time
tm = localtime(&theTime); // convert to structure
tm->tm_hour = hour;   
tm->tm_minute = minute;
theTime = mktime(tm);  // convert struct to time_t

// now calculate the end time.  Just add the hours and/or minutes
// and the function mktime() will adjust all other values as
// necessary to normalize the structure.
tm->tm_hour += duration; // add the hours
theTime = mktime(tm);  // convert struct to time_t

I tried out your example code just but there is one error. It says that tm_minute is not a member of tm.

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <time.h>

using namespace std;

void main()
{
int hour = 10;
int minute = 30;
int duration = 4; // 4 hours
struct tm* tm;
time_t theTime;

theTime = time(0); // get current date/time
tm = localtime(&theTime); // convert to structure
tm->tm_hour = hour;   
tm->tm_minute = minute;
theTime = mktime(tm);  // convert struct to time_t

// now calculate the end time.  Just add the hours and/or minutes
// and the function mktime() will adjust all other values as
// necessary to normalize the structure.
tm->tm_hour += duration; // add the hours
theTime = mktime(tm);  // convert struct to time_t

cout << theTime << endl;
}

Look at the structure and figure it out for yourself what the problem is. Learn to do a little research on your own and it will save you lots of time and effort.

If you display the value of time_t all you will see is a very large number that means nothing to you. It is the number of seconds since 1970.

Found the problem:
It was meant to be tm_min.
Thanks a lot Ancient Dragon!
Thread solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.