Thread: help with cin using classes

  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.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Well, cin doesn't know anything about your Time class. And anyway, when you enter separate values for hours, minutes and seconds you're not entering a Time as you have defined the class. You are entering 3 ints, right?

    So, change the type of a, b, and c in your main function to int instead of Time. Then cin will have no difficulty extracting the user input. After that, use the values of a, b and c with your Time constructor to instantiate a Time object.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you want cin and other istreams to be able to handle new types you have to overload operator>>. If your book doesn't show you how to do this at all then that must not be what the assignment requires, so just read the time in another format, and set the member data with a public method.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    When I change a, b, and c to ints I get an error saying "Error 1 error C2228: left of '.tictime' must have class/struct/union i:\time class\time.cpp 75 Time Class"


    I'm trying to understand all of this but I'm having a difficult time understanding classes. Help is appreciated.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You can't write "a.tictime()", etc., since a, b and c are ints. You have to declare a Time object using your constructor. Then instead of "a.tictime()", you will call "[name of your Time object].tictime()".

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Here's the page I learned it from: [15] Input/output via <iostream> and <cstdio>, C++ FAQ

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You're missing the point. Here's an example:

    Code:
    #include <iostream>
    using namespace std;
    
    class Rectangle {
        private:
            double length, width;
            
        public:
            Rectangle( double, double );
            
            void print();
    };
    
    Rectangle::Rectangle( double l=0, double w=0 ) {
        length = l;
        width = w;
    }
    
    void Rectangle::print() {
        cout << "The rectangle's dimensions are: " << endl;
        cout << "length = " << length << "; width = " << width << endl;
    }
    
    
    int main() {
        double len=0, wid=0;
        cout << "I will construct a rectangle." << endl;
        cout << "Enter length: " << endl;
        cin >> len;
        cout << "Enter width:" << endl;
        cin >> wid;
        Rectangle rect( len, wid );
        rect.print();
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Thanks a lot guys for all of the help, especially for the example!! Does anyone know why it is so hard to come across class sample programs that use cin in main? My book doesn't even have one, and I don't think I could even find one online either, weird.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> Does anyone know why it is so hard to come across class sample programs that use cin in main? My book doesn't even have one, and I don't think I could even find one online either, weird.
    I have to admit I'm skeptical of the problem. There are some bad books out there, but you are dependent on getters and setters to change objects until you learn operator overloading, and even then it is not like getters and setters disappear from your toolbox. It's not something that textbooks will forget to cover at some point. You really do have to pay attention when learning this stuff. Language features are very nearly never used in a vacuum. They are also presented commonly in a way that supplements what you've learned so far. Read closer.

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