CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2012
    Posts
    3

    Question 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.

  2. #2
    Join Date
    Dec 2009
    Posts
    145

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2012
    Posts
    3

    Re: will you help me plz ...i didn't get the right o/p

    iam sorry i cant understand iam a student
    will you please............

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: will you help me plz ...i didn't get the right o/p

    What exactly do you not understand?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jul 2012
    Posts
    3

    Re: will you help me plz ...i didn't get the right o/p

    yyes......... i got it brother..............
    thanks alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured