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]