Thread: class TIME, help please

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    class TIME, help please

    Okay guys, here is the class i use:

    class TIME {
    public:
    TIME();
    void AddSecond();
    void SetTime(int, int, int);
    void GetTime();
    void PrintTime();
    private:
    int hour;
    int min;
    int sec;
    };

    So i have to write console application in MVS 6, which uses this class and shows time with autoincrement each second.

    So i need to write something like this Pascal statement:

    repeat
    ....
    PrintTime;
    AddSecond;
    delay(1000);
    until keypressed.

    Can someone help me ?

    source code on : http://free.hit.bg/thesaint/kkk.cpp
    any one who have interesting solution post here or mail me to [email protected]

    10z

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    class TIME, help please

    Okay guys, here is the class i use:

    #include <string.h>
    #include <iostream.h>
    #include <stdio.h>
    #include <dos.h>
    #include <conio.h>

    class TIME {
    public:
    TIME(); // constructor
    void AddSecond(); // sec++
    void SetTime(int, int, int);
    void GetTime();
    void PrintTime();
    private:
    int hour;
    int min;
    int sec;
    };

    TIME::TIME()
    {
    hour = 0;
    min = 0;
    sec = 0;
    }

    void TIME::SetTime(int h, int m, int s)
    {
    if (h>=0 && h < 24) { hour = h; }
    if (m>=0 && m < 60) { min = m; }
    if (s>=0 && s < 60) { sec = s;}
    }

    void TIME::GetTime()
    {
    cout << "Enter Time in 24-hr format: ";
    cout << "\n hour: ";
    cin >> hour;
    cout << "\n min: ";
    cin >> min;
    cout << "\n sec: ";
    cin >> sec;
    if ((hour<0) || (hour >23) || (min<0) ||
    (min>59) || (sec<0) || (sec>59))
    {
    cout << "\nError detected!\n" ;
    }
    }

    void TIME::PrintTime()
    {
    cout << (hour<10?"0":"") << hour << ":"
    << (min <10?"0":"") << min << ":"
    << (sec <10?"0":"") << sec << endl;
    }

    void TIME::AddSecond()
    {
    if (sec == 59)
    {
    sec = 0;
    if (min == 59)
    {
    min = 0;
    if (hour == 23)
    hour = 0;
    else
    hour++;
    }
    else
    min++;
    }
    else
    sec++;
    }

    int main()
    {
    char ch[2];
    // main program
    TIME t;
    t.GetTime();
    do
    {
    t.PrintTime();
    t.AddSecond();
    scanf("%c", ch);
    }
    while ( 1 != 0);
    return 0;
    }

    So i have to write console application in MVS 6, which uses this class and shows time with autoincrement each second.

    So i need to write something like this Pascal statement:

    repeat
    ....
    PrintTime;
    AddSecond;
    delay(1000);
    until keypressed.

    Can someone help me ?

    source code on : http://free.hit.bg/thesaint/kkk.cpp
    any one who have interesting solution post here or mail me to [email protected]

    10z

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    >Can someone help me ?

    With what ? What exactly is your question ? What is the part your are having problems with ? I could not access your code, got a 404. Does it compile ? Does it run ? Does it produce the desired result ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <string.h>
    #include <iostream.h>
    #include <stdio.h>
    #include <dos.h>
    #include <conio.h>
    #include <time.h>
    
    class TIME {
    public:
      TIME(); // constructor
      void AddSecond(); // sec++
      void SetTime(int, int, int);
      void GetTime();
      void PrintTime();
    private:
      int hour;
      int min;
      int sec;
    };
    
    TIME::TIME()
    {
      hour = 0;
      min = 0;
      sec = 0;
    }
    
    void TIME::SetTime(int h, int m, int s)
    {
      if (h>=0 && h < 24) { hour = h; }
      if (m>=0 && m < 60) { min = m; }
      if (s>=0 && s < 60) { sec = s;}
    }
    
    void TIME::GetTime()
    {
      cout << "Enter Time in 24-hr format: ";
      cout << "\n hour: ";
      cin >> hour;
      cout << "\n min: ";
      cin >> min;
      cout << "\n sec: ";
      cin >> sec;
      if ((hour<0) || (hour >23) || (min<0) ||
        (min>59) || (sec<0) || (sec>59))
      {
        cout << "\nError detected!\n" ;
      }
    }
    
    void TIME::PrintTime()
    {
      cout << (hour<10?"0":"") << hour << ":"
        << (min <10?"0":"") << min << ":"
        << (sec <10?"0":"") << sec << endl;
    }
    
    void TIME::AddSecond()
    {
      if (sec == 59)
      {
        sec = 0;
        if (min == 59)
        {
          min = 0;
          if (hour == 23)
            hour = 0;
          else
            hour++;
        }
        else
          min++;
      }
      else
        sec++;
    }
    
    void delay ( int milli )
    {
      clock_t end = clock() + milli;
    
      while ( clock() < end )
        ;
    }
    
    int main()
    {
      // main program
      TIME t;
      t.GetTime();
      
      while ( !kbhit() )
      {
        t.PrintTime();
        t.AddSecond();
        delay ( 1000 );
      }
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Please don't double post, it's very rude.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I think your class could do with some work.

    GetTime() gets from keyboard and enters whatever was got into class private members with scant regard for limits. ok if it detects an error you get a message but thats it. You should check the values before they are stored in the class private members. Only stroe if correct.
    SetTime() has a similar problem. If time out of bounds it is ignored rather than either an exception being thrown or a default value (say 0) being entered instead both of which are preferable to ignorance.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. help working with class files for first time
    By jrb47 in forum C++ Programming
    Replies: 12
    Last Post: 11-30-2006, 09:20 AM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM