Thread: help with classes

  1. #1
    helpme
    Guest

    Question help with classes

    Im student and i having problem with this code, my output is not the way i wanted, plus it do not show the time on the three functions that should displayed.

    Can anyone help me please????



    [tag]


    // this program was modified to accept user input, and to loop with a more atractive display

    #include <iostream.h>

    class Time
    {
    public:
    Time(); // constructor
    void setTime( ); // set hour, minute, second
    void printMilitary(); // print military time format
    void printStandard(); // print standard time format
    private:
    int hour, h; // 0 - 23
    int minute, m; // 0 - 59
    int second, s; // 0 - 59
    };

    // Time constructor initializes each data member to zero.
    // Ensures all Time objects start in a consistent state.
    Time::Time()
    {
    {
    cout<<"\n"<<endl; int asterisk=1;
    while (asterisk<70)
    {
    cout<<"-"; //gives a separator with dashes
    ++asterisk;
    }
    cout<<"\n\n"<<endl;
    }
    cout<<"Please enter the time you want to be displayed"<<endl<<endl;


    cout<<"hour? ";
    cin>>hour;
    if (hour>24)
    hour=0;
    h=hour;
    cout<<hour<<" : -- : --"<<endl;

    cout<<"minute(s)? ";
    cin>>minute;
    if (minute>59)
    minute=0;
    m=minute;
    cout<<hour<<" : "<<minute<<" : --"<<endl;

    cout<<"seconds? ";
    cin>>second;
    if (second>59)
    second=0;
    s=second;
    cout<<hour<<" : "<<minute<<" : "<<second<<endl;
    }

    // Set a new Time value using military time. Perform validity
    // checks on the data values. Set invalid values to zero.
    void Time::setTime( )
    {
    h = ( h >= 0 && h < 24 ) ? h : 0;
    m = ( m >= 0 && m < 60 ) ? m : 0;
    s = ( s >= 0 && s < 60 ) ? s : 0;
    }

    // Print Time in military format
    void Time:rintMilitary()
    {
    cout << ( hour < 10 ? "0" : "" ) << hour << ":"
    << ( minute < 10 ? "0" : "" ) << minute;
    if (hour>=24)
    hour=0;
    if (minute>59)
    minute=0;
    if (second>59)
    second=0;
    }

    // Print Time in standard format
    void Time:rintStandard()
    {
    if (hour>13)
    hour=0;
    if (minute>59)
    minute=0;
    if (second>59)
    second=0;

    cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
    << ":" << ( minute < 10 ? "0" : "" ) << minute
    << ":" << ( second < 10 ? "0" : "" ) << second
    << ( hour < 12 ? " AM" : " PM" );

    }


    int main() // Driver to test simple class Time
    {
    char response;
    do
    {
    Time t; // instantiate object t of class Time


    cout << "The initial military time is ";
    t.printMilitary();
    cout << "\nThe initial standard time is ";
    t.printStandard();

    t.setTime( );
    cout << "\n\nMilitary time after setTime is ";
    t.printMilitary();
    cout << "\nStandard time after setTime is ";
    t.printStandard();

    t.setTime( ); // attempt invalid settings
    cout << "\n\nAfter attempting invalid settings:"
    << "\nMilitary time: ";
    t.printMilitary();
    cout << "\nStandard time: ";
    t.printStandard();
    cout << endl;

    cout<<"Do you want to use the program again? Y or N"<<endl;
    cin>>response;
    }
    while (response=='Y'||'y');
    return 0;
    }




    [/tag]

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    Time class

    There are a couple of things wrong with your class. First off you should get rid of the h, m, s member variables from your class.

    Second setTime should have some arguments setTime(int h, int m, int s)

    Third you should not include for a way to get input in your constructor. This class would not work in say a windows application, since you have bounded input output to the constructor. Instead create a member function (method) that allows you to get input from the console and name it GetConsoleInput or something.

    Fourth when you set the time you are checking to see if the values exceed the max but you are not checking to see if they exceed the min value of 0. You should also check for this.

    Lastly when printing Military or standard time you should not have to check to see if the time units are within range since this was checked at construction then when setting. There is no way to enter invalid data so why check for it.
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM