Thread: help with cin using classes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    help with cin using classes

    I am writing a program using classes, the program should allow a user to enter a time in , hours, minutes, and seconds. The program should then generate the next 20 times (in military time). The problem is I cannot find one example online or in my book that uses classes and also uses cin in main(). Can someone please help with this and let me know how I can fix this? Thanks!

    EDIT- I get an error at the cin statements saying it does not know how to handle >> of type Time.

    Code:
    #include <iostream>
    using namespace std;
    
    //Class Declaration
    class Time
    {
          private:
                  int secs;
                  int mins;
                  int hours;
                  
           public:
                   Time(int = 0, int = 0, int = 0);
                   void settime(int, int , int);
                   void showtime();
                   void tictime();
                         
    };                  
                    // Constructor 
                   Time::Time(int h, int m, int s)
                   {
                         secs = s;
                         mins = m;
                         hours = h;
                                  
                   }      
    
    			   void Time::settime(int h, int m, int s)
    			   {
                    
    				   secs = s;
    				   mins = m;
    				   hours = h;
    
    				   return;
    			   }
    
    			   void Time::showtime()
    			   {
    				   cout << "The time is " << hours << ":" << mins << "." << secs;
    				   cout << endl;
    				   return;
    			   }
    
    			   void Time::tictime()
    			   {
    				   
    				   secs = secs + 1;
    				
    				   if(secs > 59)
    					   mins = mins + 1;	
    
    				   if(mins > 59)
    					  hours = hours +1;
    				   
    					return;
    			   }
    
    
    	int main()
    	 {
    		Time a, b, c;
    
    		 cout << "Enter hours\n";
    		 cin >> a;
    
    		 cout << "Enter minutes\n";
    		 cin >> b;
    
    		 cout << "Enter seconds\n";
    		 cin >> c;
    
    		 for(int i = 1; i < 21; i++)
    		 {
    			 a.tictime();
    			 a.showtime();
    			 b.tictime();
    			 b.showtime();
    			 c.tictime();
    			 c.showtime();
    		 }
    
    
    
    		 system("pause");
    		 return 0;
    	}
    Last edited by nick753; 11-11-2010 at 08:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple classes question
    By TriKri in forum C++ Programming
    Replies: 20
    Last Post: 06-11-2010, 04:03 PM
  2. CIN inputing the wrong type
    By Bill Agnor in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2010, 06:24 PM
  3. Convert functions into classes and header file
    By gonzalo12345 in forum C Programming
    Replies: 2
    Last Post: 12-02-2009, 10:16 PM
  4. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM