Thread: What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    What am I doing wrong?

    Hi, I am trying to write this program and obviously I have problems with it. I am not sure where and what I am doing wrong, anyone's help is much appreciated, thanks for the time.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <assert.h>
    
    class Time
    {
    	public:
    		Time(int h=0, int m=0, int s=0);
    		void printStandard();
    		void add(Time t);
    		void subtract(Time t);
    		void getFromKbd();
    	private:
    		int hour;
    		int min;
    		int sec;
    };
    Time::Time(int h, int m, int s)
    {
    	hour = (h>=0 && h<24) ? h:0;
    	min = (m>=0 && m<60) ? m:0;
    	sec = (s>=0 && m<60) ? s:0;
    }
    void Time::printStandard()
    {
       cout << ((hour % 12 == 0) ? 12 : hour % 12) << ':'
            << (min < 10 ? "0" : "") << min  << ':'
            << (sec < 10 ? "0" : "") << sec
    	    << (hour < 12 ? " AM" : " PM");
    }
    void Time::add()         //  Adds t to the object  through which the function is called
    {
    	Time t;
    	
    	cout << "Sum of the time is " << t+t.getFromKbd();
    	
    
    }
    void Time::subtract()     // Subtracts t from the object through which the function is called
    {
    	cout << "Difference of the time is " << t-t.getFromKbd();
    }
    
    void Time::getFromKbd()    // t.getFromKbd() will get values for t.hour, t.min, and t.sec from the keyboard, allows user to input as military time
    {
    	
    	cout << "Enter the hour(s): ";
    	cin >> t.hour;
    	cout << "Enter the minute(s): ";
    	cin >> t.min;
    	cout << "Enter the second(s): ";
    	cin >> t.sec;
    
    }
    
    int main()
    {
    	Time t(12, 45, 30);      
    	t.printStandard();			// should display 12:45:30 AM or PM
     	
     	t.getFromKbd();				// user inputs his/her time
    	t.add();					// adds user time to 12:45:30
    	t.printStandard();			// displays sum of times
    	
    	t.subtract();				// subtracts both times
    	t.printStandard();			// prints times
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Post a specific problem.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Response to Time

    I think possibly you need to work with a reference to the time object. Declaring the time obj within a function and returning void will alter nothing during runtime. I think that's what you mean.
    Also, the getkboard() function doesn't get anything from the keyboard to work with. Don't forget, you need an instance of any object to work with. Classes cannot be manipulated.

  4. #4
    Edited to use code tags.

    And in future, try being specific as kuphryn suggests. It would help to know what the actual problem you're experiencing might be. Error messages, incorrect output, crashing, etc... Whats happening? It helps people help you faster and better.

    Peace
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM