Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Time addition program (http://www.go4expert.com/forums/time-addition-program-t15847/)

NoobProgrammer 17Jan2009 00:50

Time addition program
I am a noob programmer. I had a class in regular C programming that didn't prepare me for my C++ programming class. So my knowledge of programming is very limited. With that said, I've got an assginment here that I've started and have no idea how to complete. I was hoping that someone here could make this program. I know what you're thinking: How is that going to help you learn and why should I do it. As to the learning part, I really need to see what this program would look like to learn anything about it. Well, do with this what you'd like:


Write a program that uses a class.

Your project will contain files CTime.h, CTime.cpp and TimeApp.cpp.

1.2.1 (Class declaration)

* Place the class declaration and inline functions in a seperate header file, CTime.h.

* Class declaration:

declare CTime as a class

declare a defualt constructor
define an inline destructor with an empty code body
declare getHours as a member function
declare getMinutes as a member function
declare getSeconds as a member function
declare setHours, setMinutes, setSeconds as a member function
declare add as a member function with CTime return and a CTime parameter


declare hours, minutes and seconds as an integer

make the constructor, destructor, get- and set- functions inline.

1.2.2 (Implementation CTime.cpp)

* #include file CTime.h

* place the noninline member function add in a separate file, CTime.cpp

-convert the time values to total seconds before adding them
CTime tm;
long t1Seconds;
t1Seconds = (long)tm.hours*3600 + tm.minutes*60 + tm.seconds;
-add the total seconds together:
t3Seconds = t1Seconds + t2Seconds;
-divide them back out to make the new time hours, minutes and seconds.
- store them in a class CTime variable before returning it.

1.2.3 (member function add)

Module add

Return: CTime

Paramaters: define t2 as a CTime

define t1Seconds, t2Seconds and t3Seconds as long integers

convert hours, minutes and seconds to a long and assign to

convert t2.hours, t2.minutes and t2.seconds toa long and assign to t2Seconds

SET t3Seconds to t1Seconds + t2Seconds

derive values from t3Seconds to assign to temp.hours, temp.minutes and temp.seconds

return temp

END add

1.2.4 (Application file TimApp.cpp)

this is where the prgoram asks the user for 2 times entered in this format:


the program adds the 2 times and displays the sum

Here's the code I've done:

CTime.h file:


/******************************* FILE: CTime.h* PROGRAMMER: * DATE: 01/14/09** Declaration of class CTime.*******************************/#ifndef CTime_H#define CTime_H    #include <iostream.h>    class  CTime    {    public:          CTime();          ~CTime()  {}                int getHours();          int getMinutes();          int getSeconds();          int add();                void setHours();          void setMinutes();          void setSeconds();                          private:            int hours;            int minutes;            int seconds;    };        inline int CTime::getHours()    {      return hours;        }    inline int CTime::getMinutes()    {      return minutes;        }    inline int CTime::getSeconds()    {      return seconds;        }    inline int CTime::add(CTime)    {      return CTime    }    inline void CTime::setHours(int hr)    {      hours = hr;              }    inline void CTime::setMinutes(int min)    {      minutes = min;            }    inline void CTime::setSeconds(int sec)    {      seconds = sec;            }        #endif
and here's CTime.cpp

* FILE: CTime.cpp
* DATE: 01-14-09
* Implementation of class CTime.
#include "CTime.h"
#include <iostream>using namespace std;int main(int argc, char *argv[])
    int t1Seconds;
    int t2Seconds;
    int t3Seconds;
    int CTime::time()
        t1Seconds = (long)time.hours*3600 + time.minutes*60 + time.seconds;
    return EXIT_SUCCESS;

shabbir 17Jan2009 01:57

Re: Time addition program
Check this out - http://www.mbaguys.net/t448/

xpi0t0s 17Jan2009 15:00

Re: Time addition program
Doesn't work consistently: 1h 1min + 2h 2min = 3h 3min, but 101+202+40=343 = 3h 43 min.

1h50 + 1h50 + 1h50 = 3h 150 = 5h 30, but 150*3=450; add 40=490. You have to add 40 twice to get the "correct" result of 530.

So for this to work, you have to know how many overflows you've had, and add 40 for each one.

And if you're going to calculate overflows, you may as well convert to minutes, add them up normally, then convert back, e.g. 1h30*3 = 90 min * 3 = 270 min; integer divide by 60=4h; 270-4*60=30 for the minutes.

xpi0t0s 17Jan2009 15:12

Re: Time addition program
OP: no, you're wrong. You don't learn programming by looking at someone else's completed answer, you learn by doing. That's why the course presents a series of programming exercises for you to complete, rather than just going "Lesson 1, here look at this completed code. Lesson 2, here look at this completed code" and so on. So I'll help you fix the code, but I'm not going to write it for you.

Most of your CTime.cpp is wrong; this file is just for the definition of the CTime class so you don't need any of the io stuff or a main function. I don't know why you've started a CTime::time() function; this is not in the class definition.

What you need to do in CTime.cpp is pretty much already spelled out in detail:

1.2.2 (Implementation CTime.cpp)
* #include file CTime.h
* place the noninline member function add in a separate file, CTime.cpp

So do you know how to declare a non-inline member function? (Hint: it's exactly the same as an inline member function except (a) for the inline keyword and (b) it doesn't go in the header file.)

Your definition in the header of add is slightly wrong:

declare add as a member function with CTime return and a CTime parameter

but you wrote:

inline int CTime::add(CTime) { return CTime }

which has an int return instead of a CTime return, and while it has a CTime parameter that parameter has no name. Also this function (a) is inline, which is not required and (b) has a function body, so when you declare the function in CTime.cpp you will get a duplicate symbol error. The function add() should just be a prototype.

activ@ 17Jan2009 15:14

Re: Time addition program
Good good......................
You are clever

shabbir 17Jan2009 16:34

Re: Time addition program
Accepted its not consistent.

Now here is one more and probably better than that link. The idea is same but the algo is totally from my mind and may not work for all but brute force it as well and let me know the output

1 hr 35 Min
4 Hr 55 Min


+ .40 only if greater than equal to .60 till it gets less than .60

5.30 i.e 5 hours and 30 Mins

1hr 1min
2hr 2min


No need to add +.40

1hr 50mins
1hr 50mins
1hr 50mins

Now add 0.40


>= 0.60

Add 0.40


Brute force for other cases and see if it works for all. I have tested for quite a few

xpi0t0s 18Jan2009 17:53

Re: Time addition program
Now add 0.40

But how do you know that you have to add .40? Dividing by 100 doesn't simplify the problem, which is essentially all you've done.


So do we add .40 or not? Why?

shabbir 18Jan2009 18:22

Re: Time addition program
By this logic.

Add .40 only if greater than equal to .60 till it gets less than .60

xpi0t0s 18Jan2009 20:07

Re: Time addition program
Only if what is >=60?
155+155=310, so what do we compare with 60?

shabbir 19Jan2009 09:04

Re: Time addition program
Agreed, Let me think over it then.

All times are GMT +5.5. The time now is 01:20.