注册 登录
编程论坛 C++教室

cin 函数在时间类中的 问题 。如何实现 时间设置后 的 显示连续性

haoyasen 发布于 2013-05-27 20:56, 642 次点击
/*
【基本题】定义一个时间类,提供设定时间、显示时间和秒数增加1的功能,其中设定时间的方法需要校验数据的正确性,
并在main函数中验证。
*/
#include<iostream>
#include<iomanip>
#include<windows.h>
using namespace std;
class  CClock
{
    int hour,minute,second,status;
public:
    CClock (int myhour=0,int myminute=0,int myscend=0)
    :hour(myhour ),minute(myminute ),second (myscend ){};
    //void Sethour( int sethour):hour(sethour){};
    //void Setminute(int setminute):minute(setminute){};
    //void  Setsecond(int setsecond):second(setsecond){};
    CClock(CClock & a)
    {
        hour=a.hour ;
        minute=a.minute ;
        second =a.second ;
    };
    void Display();
};
void  CClock::Display ()
{
    second ++;
    if(second ==60)
    {
        second =0;
        minute ++;        
    }
    if(minute ==60)
    {
    minute =0;
    hour ++;
    }
    if(hour==24)
    {
    hour=0;
    }
    //说明时间如何设置
    cout<<"            the tips  about  the set  of the time :"<<endl;
    cout<<"             press the key 'h' for  reset the time "<<endl;
    cout<<"             press the key 'g' for  let  the time go on"<<endl;
    //时间输出
    cout<<"Hour:"<<setw (2)<<hour<<"Minute:"<<setw(2)<<minute
        <<"Second:"<<setw(2)<<second<<endl;                                    
    for(long  i=0;i<290000000;i++)
    {}
    system("cls");
}
void main()
{
    int sethour,setminute,setsecond,flag=1;
    CClock myclock;
    while(1)
    {
     myclock .Display ();
     if(cin.get ()=='h'&&flag==1)//想显示提示信息和设置时间
        {                                         //cin这里出现错误我想让他连续的显示
            cin.clear ();
            cout <<"input the hour"<<endl;cin>>sethour ;
            cout<<"input the minute "<<endl;cin>>setminute;
            cout<<"input the second"<<endl;cin>>setsecond;
        //    myclock .Sethour (sethour );myclock .Setminute (setminute );myclock .Setsecond (setsecond );
            CClock myclock1( sethour,setminute,setsecond);
            myclock =myclock1 ;
            if(cin.get()=='g')
                flag=0;
            cin.clear ();
        }
        flag=1;
    }
}
1 回复
#2
haoyasen2013-05-28 08:06
大家看看吧
1