728x90 AdSpace

Latest Article

Implement Time Class in C++ Programming Language Example Code





Implement Time Class in C++ Programming Language Example Code Hour Minutes and Seconds

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>

void mainmenu();
class time
{
 private:
int hr,min,sec;
 public:
void settime();
void displaytime();
 };
 void time :: settime()
 {
while(1)
{
cout<<"\nplz enter hours  ";
cin>>hr;
if(hr<0||hr>23)
{
cout<<"\nInvalid Hours  " ;
cout<<"\nPress any key to  Try Again ";
getch();
continue;
}//if ends
else
{
break;
}//else ends
    } //while ends
while(1)
{
cout<<"\nplz enter minutes  ";
cin>>min;

if(min<0||min>59)
{
cout<<"\nInvalid Minutes  " ;
cout<<"\nPress any key to  Try Again ";
getch();
continue;
}//if ends
else
{
break;
} //else ends
  }//while ends
while(1)
{
cout<<"\nplz enter seconds  ";
cin>>sec;
if(sec<0||sec>59)
{
cout<<"\nInvalid Seconds  " ;
cout<<"\nPress any key to  Try Again ";
getch();
continue;
} //if ends
else
{
break;
}
   }//while end



}//function end
 void time:: displaytime()
 {
 if(hr<10||min<10||sec<10)
 {
 cout<<setw(2)<<setfill('0');
 cout<<hr<<":"<<min<<":"<<sec;
 }
 }

 void main()
{

time t1,t2;
int choice;
clrscr();
while(1)
{

mainmenu();
cout<<"Please Enter Your Choice";
cin>>choice;
switch (choice)
{

    case 1:
cout<<"\nEnter Time One  ";   //setime
t1.settime();
cout<<"\n\nEnter Time Two  ";
t2.settime();
break;
    case 2:                                       //displaytime
clrscr();
gotoxy(17,25);
t1.displaytime();
gotoxy(18,26);
t2.displaytime();
break;
    case 3:
exit(0);
    default:
cout<<"Invalid Option";
getch();
continue;

}
   }

getch();
}
void mainmenu()
{
cout<<"    \n\t\t\tMAIN MENU   ";
cout<<"\n\n\t\t1.SET TIME      ";
cout<<"\n\n\t\t2.DISPLAY TIME      ";
cout<<"\n\n\t\t3.Exit            ";
}
no image
  • Title : Implement Time Class in C++ Programming Language Example Code
  • Posted by :
  • Date : 07:37
  • Labels :





  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Top