Please help with compile for gcc

This is a discussion on Please help with compile for gcc within the C++ Programming forums, part of the General Programming Boards category; Hi, the following program runs fine with Borland. It is a twelve hour clock. What it does is requests hours, ...

  1. #1
    Registered User nevermind's Avatar
    Join Date
    Oct 2002
    Posts
    23

    Please help with compile for gcc

    Hi, the following program runs fine with Borland. It is a twelve hour clock. What it does is requests hours, mins, secs and then waits for user input. When user hits a key the program stops running and adds the elapsed time to the user input time and outputs it.

    There are two issues when i compile using gcc.
    (1) getch() is not avilable in GCC, but i need a simple substituition that will permit a pause for user input(press any key ....)

    (2) The time.h file, i am not sure if its is right. I cannot even get to the output it just skips past when user input is required.

    Please can you possibly help me, I need this to work for me to go ahead with the world clock!

    PS: Excuse the code; I am just learning, any help would be appreciated.




    Code:
    //program: 12 hour time clock for c++
    
    
    #include<iostream>
    #include<time.h>
    #include<conio.h>
    
    class myClock{
       private:
          int hh, mm, ss;
          int start, timerSecs;    //for the timer
    
    
       public:
          myClock();        //constructor
          void get_time(int h, int m, int s);
          void startTimer();
          void endTimer();
          void add_time();
          void show_time();
    };
    
    
    myClock :: myClock()
    {
       hh = mm = ss = 0;
    }
    
    void myClock :: get_time(int h, int m, int s)
    {
       hh = h;
       mm = m;
       ss = s;
    }
    
    void myClock :: startTimer()
    {
       start = clock();             //get the initial clock position
    }
    
    void myClock :: endTimer()
    {
       clock_t end;                  //to get the final clock position
       end = clock();
       timerSecs = (end - start)/CLK_TCK;    // to get value in secs
    }
    
    void myClock :: add_time()
    {
       ss += timerSecs;
       if ( ss > 59 ) {
          mm ++;
          ss %= 60;
       }
    
       if ( mm > 59 ){
          hh ++;
          mm %= 60;
       }
    
       if ( hh > 12 ){
          hh %= 12;
       }
    }
    
    void myClock :: show_time()
    {
       cout << "The time is: " << hh << ":" << mm << ":" << ss;
    }
    
    
    
    
    int main()
    {
    
       int hours, mins, secs;
       myClock c;
    
       cout << "Enter the hours: ";
       cin >> hours;
    
       cout << "Enter the minutes: ";
       cin >> mins;
    
       cout << "Enter the seconds: ";
       cin >> secs;
    
       c.get_time(hours, mins, secs);
    
       cout << "c is now ticking ..." << "\n";
       c.startTimer();
    
       cout << "Press any key to stop c ";
       getch();
       c.endTimer();
       c.add_time();
       c.show_time();
    
       return 0;
    }

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you can always use cin.get() or cin >> using a dummy variable. It may require pressing two keys, one of which is the enter key, but it should always be available.

    try #include <ctime> instead of #include <time.h>

  3. #3
    Registered User nevermind's Avatar
    Join Date
    Oct 2002
    Posts
    23

    Thumbs up Thanks

    Thanks a lot, it clears it up.

    APPRECIATE YOUR HELP!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21