Thread: Need help with time

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    Need help with time

    Write a menu-driven program that repeatedly allows the user to select one of the following tasks:

    (Task 1) User enters the time of city A, time difference between cities A and B, and the program will output the equivalent time of city B.



    Define and implement a class Time. Time difference is a positive value if time of city B is ahead of city A. It is a negative value if time of city B is behind city A.

    Typical Sample Runs for Task 1 (user inputs in bold):
    Enter hrs, mins and am/pm of time of city A: 2 10 pm
    Enter time difference (in hrs) between cities A & B: 6
    The equivalent time of city B is 8:10 pm.

    Enter hrs, mins and am/pm of time of city A: 9 45 am
    Enter time difference (in hrs) between cities A & B: -3
    The equivalent time of city B is 6:45 am.

    Im now doing on this and got stuck.
    This is what i do. The code is below

    Code:
    #include <iostream>
    using namespace std;
    
    class Time
    {
    	private:
    		int hrs, mins;
    
    	public:
    		int h;
    		int m;
    		int h1;
    		void output();
    };
    
    
    void main()
    {
    	Time equivalent;
        int h;
    	int h1;
    	int total= h + h1;
    
    
    	cout << "Enter hrs, mins and am/pm of time of city A: ";
    	cin >> equivalent.h >> equivalent.h1 ;
    	
    	cout << "Enter time difference (in hrs) between cities A & B: ";
    	cin >> equivalent.h1;
    
    	cout << "The equivalent time of city B is: " << total << endl;
    	
    }
    can someone help me. Im very poor on my C++
    thanks.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    class Time
    {
        public:
            void set_time(int, int, std::string);
            Time add_hours(int);
            void print();
        private:
            int hours, minutes;
            std::string am_pm;
    };
    How about something like that for the class prototype. Now try to figure out what the parameters for the methods mean, and try to implement them. Using this class in a program should be a piece of cake, if you got that done.

    (Also note, that your code contains public data members. Don't do that! Data has to be private, especially since you may need to validate the parameters first. With public data, you're class would accept times such as 100.-23 FM.)

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    8
    I have redo the program and come out of this.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <iomanip>
    
    using std::setfill;
    using std::setw;
    
    class Time {
    public:
       Time();
       void setTime( int, int );
       void printsetUp();
       void print_A();
       void print_B();
    
    private:
       int h;
       int m;
    };
    
    Time::Time()
    {
       h = m = 0;
    }
    
    void Time::setTime( int p, int l )
    {
       h = ( p >= 0 && p < 24 ) ? p : 0;
       m = ( l >= 0 && l < 60 ) ? l : 0;
    }
    
    void Time::printsetUp()
    {
       cout << setfill( '0' ) << setw( 2 ) << h << ":"
            << setw( 2 ) << m;
    }
    
    void Time::print_A()
    {
       cout << ( ( h == 0 || h == 12 ) ? 12 : h % 12 )
            << ":" << setfill( '0' ) << setw( 2 ) << m
            << ( h < 12 ? " AM" : " PM" ) << endl;
    }
    
    void Time::print_B()
    {
       cout << ( ( h == 0 || h == 12 ) ? 12 : h % 12 )
            << ":" << setfill( '0' ) << setw( 2 ) << m
            << ( h < 12 ? " AM" : " PM" ) << endl;
    }
    
    int main()
    {
       Time t;
       int h;
       int m;
    
       cout << "Current values are:\n";
       cout << "City A: ";
       t.print_A();
       cout << "City B: ";
       t.print_B();
       cout << "Enter values for City A:\nHour: ";
       cin >> h;
       cout << "Minute: ";
       cin >> m;
    
       t.setTime( h, m );
       cout << "City A: ";
       t.print_A();
    
       if ( h <= 2 )
           h -= 0;
       else if ( h > 2 && h < 24 )
           h -= 2;
    
       if ( m <= 5 )
           m -= 0;
       else if ( m > 2 && m < 60 )
           m -= 10;
    
       t.setTime( h, m );
       cout << "City B: ";
       t.print_B();
    
       return 0;
    }
    can someone help me.
    I want the user to key in the time difference (in hrs) between cities A & B like and show the equivalent time of city B.

    The user can key in any number in the time difference (in hrs) between cities A & B. ex negative or positive number.
    thanks

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    What do A and B refer to in your code?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So who wrote that newer piece of code?
    It was clearly not written by the same person.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, this code looks very elegant, except that Time:rint_A and Time:rint_B are identical, Time:rintSetup is unused (and not needed - and should be a private method anyway).

    It looks as if you got some code from a solution to an earlier assignment (which you probably failed / stole from somebody), copied the print() function (because you have no idea what variables are and how classes work - that you make different instances of a class and call the same methods on them, not the other way round), and wrote a stupid driver program that doesn't take you one step closer to the solution.

    Adding hours to a given time should be done by a class method, not in main.

    Try harder.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Gong is learning from the popular Detiel C++ programming books. I have done this assignment, and like anon said it's quite easy with the correct class. The book does tell you before this task how to set a class out correctly. After this he will have to do an assignment about a post office. Good luck gong.
    Double Helix STL

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If he's learning from a book, then I'm sorry for my harsh words.

    But really, instead of struggling on without understanding the basics, find the place where things got confusing, reread, do the exercises and try out the new concepts with similar programs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM