Problem with time program

This is a discussion on Problem with time program within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a program which is supposed to give the time elapsed from a given time to another given ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    42

    Problem with time program

    Hi, I have a program which is supposed to give the time elapsed from a given time to another given time. There were three functions which were incomplete setTime(), display1Time()and elapsed(). I have created a solution for two of the functions, but found that there are some erros which I do not understand and was wondering if I can get your help to correct these errors and make the program work completely.My code is displayed below:

    Code:
    // Incomplete version
    // Starting code for student tutorial
    
    #include <iostream>
    using namespace std;
    
    struct Mytime
    {
       int hours;
       int mins;
    };
    
    int timecmp( Mytime, Mytime );
    // compares two Mytime values
    // returns 0 if times are same
    // returns >0 if first is later than second
    // returns <0 if first is before second
    
    int setTime( Mytime& t, int h, int m );
    // if valid time sets hh and mm to h and m 
    // and returns 0
    // if invalid returns integer > 0 as error code
    // error code +1 = underflow hours
    // error code +2 = overflow hours
    // error code +4 = underflow mins
    // error code +8 = overflow mins
    
    void display1Time( Mytime t );
    // displays in form hh:mm
    
    void elapsed( Mytime t1, Mytime t2, Mytime& duration );
    // determines the time duration between t1 and t2
    // t1 assumed to be earlier than t2
    
    int main()
    {
        Mytime now;
        Mytime then;
        Mytime howlong;
        
        int h,m;
        
        do // validate input
        {
            cout << "Enter start hh mm : ";
            cin >> h >> m ;
        } while (setTime(now,h,m));
        
        do
        {
            cout << "Enter finish hh mm : ";
            cin >> h >> m ;
        } while (setTime(then,h,m));
        
        elapsed(now,then,howlong);
    
        cout << "Time elapsed from ";
        display1Time(now);
        cout << " until ";
        display1Time(then);
        
        cout << " is ";
        display1Time(howlong);
        cout << endl;
        return ( 0 );
    }
    
    
    int timecmp(Mytime t1,Mytime t2)
    {
       if (t1.hours == t2.hours)
       {
          if (t1.mins == t2.mins)
          {
             return 0;
          }
          else // mins not same
          {
             if (t1.mins > t2.mins)
             {
                return 1; // greater than zero
             }
             else
             {
                return -1;
             }
          }
       }
       else // hours not same
       {
          if (t1.hours > t2.hours)
          {
             return 1;
          }
          else
          {
             return -1;
          }
       }
    }
    
    
    int setTime(Mytime& t, int h, int m )
    {
       
      if (h < 0)
       {
          return 1; // error code +1 = underflow hours
       }
      else if (h > 23)
       {
          return 2; // error code +2 = overflow hours
       }
      if (m < 0)
       {
          return 4; // error code +4 = underflow mins
       }
      else if (m > 60)
       {
          return 8; // error code +8 = overflow mins
       }
     
        t.hours = h;
        t.mins = m;
        return 0;
    }
    
    void display1Time(Mytime t)
    {
      if (h > 10) 
       {
          cout << h << endl;
       }
      else
       {
          cout << "0" << h << endl;
       }
      if (m > 10)
       {
          cout << m << endl;
       }
      else
       {
          cout << "0" << m << endl;
       }
    
      cout << h << ":" << m << endl;
    }
    
    
    void elapsed(Mytime t1, Mytime t2, Mytime& duration)
    {
        setTime(duration,0,0);
    }
    I have completed the functions for setTime() and display1Time(), but these have caused the following errors:

    Code:
    time.cc: In function `void display1Time(Mytime)':
    time.cc:133: `h' undeclared (first use this function)
    time.cc:133: (Each undeclared identifier is reported only once
    time.cc:133: for each function it appears in.)
    time.cc:141: `m' undeclared (first use this function)
    I tried correcting this error by changing the function to void display1Time (int h, int m) but this did not work saying the following:

    Code:
    16 jaguar% g++ time.cc
    time.cc: In function `int main()':
    time.cc:58: `struct Mytime' used where a `int' was expected
    time.cc:60: `struct Mytime' used where a `int' was expected
    time.cc:63: `struct Mytime' used where a `int' was expected
    Hope you can help me. Also, I would like to know how to approach the function elapsed().

    Thank you if you can help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you don't want to use random data inside your displaytime function; you want to display the data inside the object. You use dots to access the fields of an object (see your set function for an example).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    There appeas to be no erros now when I compile it, but my output displays the following:

    Code:
    9 jaguar% g++ time.cc
    10 jaguar% ./a.out
    Enter start hh mm : 7 30
    Enter finish hh mm : 7 29
    Time elapsed from 07
    30
    7:30
     until 07
    29
    7:29
     is 00
    00
    0:0

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, that's how you told it to print: The hours, a new line, the minutes, a new line, and then the time with a colon.

    And currently your elapsed always returns 0:0 so that's expected too.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    So I just need to take out the new lines and it should display as normal? Also I have not completed the elapsed() function yet, so I will expect the output to be 0:0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  2. time synchronization problem
    By freeindy in forum C Programming
    Replies: 1
    Last Post: 04-19-2007, 07:25 AM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 11:42 AM
  4. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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