will you help me plz ...i didn't get the right o/p
This is the program for add two times
#include<iostream.h>
#include<conio.h>
class time
{
long int hr,min,sec;
public:
void get();
void put();
void add(time t1,time t2);
time();
};
time::time()
{
hr=0;
min=0;
sec=0;
}
void time::get()
{
cout<<"enter the time in hours,minuts and seconds:";
cin>>hr>>min>>sec;
}
void time:ut()
{
cout<<"\nthe time:"<<hr<<":"<<min<<":"<<sec;
}
void time::add(time t1,time t2)
{
time s;
s.sec=(t1.sec+t2.sec);
if(s.sec>=60)
{
s.sec=s.sec%60;
s.min++;
}
s.min=(s.min+t1.min+t2.min);
if(s.min>=60)
{
s.min=s.min%60;
s.hr++;
}
s.hr=(s.hr+t1.hr+t2.hr);
}
void main()
{
clrscr();
time t1,t2,t3;
t1.get();
t2.get();
t3.add(t1,t2);
t1.put();
t2.put();
cout<<"\nsum:";
t3.put();
getch();
}
the compiler shows no error, but
the output that i got is always:0 0 0
Last edited by fasluca; July 31st, 2012 at 11:25 PM.
Re: will you help me plz ...i didn't get the right o/p
You need to redefine your object scope to resolve your problem. It is simply an out of scope issue {I just skip and do not go into much detail as I am heading for a ~meeting~ right now}. Local object is valid only within its locality. People say no one can know when the object's allocated memory is reclaimed (by GC or OS - Uhmmm I doubt this a little). I guess object lifetime is within its scope and the object's memory is reclaimed right after the program terminates.
Re: will you help me plz ...i didn't get the right o/p
Your add function should either be a member function that takes one argument and changes the current object, or be a non-member function that takes two arguments and returns an object that is the result of the sum. Problem is, it is currently a member function that takes two arguments, stores the result of the sum in another object, then does not return that object.
By the way:
Please indent your code and post it within [code][/code] bbcode tags.
<iostream.h> should be <iostream>
You don't need the non-standard <conio.h>, clrscr and getch functions here.
cout and cin are from the std namespace.
void main should be int main
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Bookmarks