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

C++提示用户输入年、月、日、小时、分、秒并显示出来,怎么添加输入条件?

c250484657 发布于 2011-09-21 20:54, 2014 次点击
C++提示用户输入年、月、日、小时、分、秒。并显示出来。程序已经写好,但是想给他们加个条件,比如年的数字要大于0,月不能大于12,日不能大于31,小时不能超过12,分秒不能超过60.这个附加条件的程序要怎么加奥,我试了好久没添加成功。
#include <iostream>
using namespace std;

struct _time
{
    short year;
    short month;
    short day;
    short hour;
    short minute;
    short second;

};

int main( )
{
    struct _time t;
    cout<<"请输入年,月,日,时,分,秒:"<<endl;
    cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
    cout<<t.year<<"年"<<t.month<<"月"<<t.day<<"日"<<t.hour<<"时"<<t.minute<<"分"<<t.second<<"秒"<<endl;
    return 0;
}
9 回复
#2
c2504846572011-09-21 20:55
#include <iostream>
using namespace std;

struct _time
{
    short year;
    short month;
    short day;
    short hour;
    short minute;
    short second;

};

int main( )
{
    struct _time t;
    cout<<"请输入年,月,日,时,分,秒:"<<endl;
    cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
    if
      0 < t.year &&t.month <= 12 && t.day <=31 &&t.hour<=24&& t.minute<=60 &&t.second<=60;
      cout<<t.year<<"年"<<t.month<<"月"<<t.day<<"日"<<t.hour<<"时"<<t.minute<<"分"<<t.second<<"秒"<<endl;
     else
      cout<<"you have enter a wrong time"<<endl;
      
    return 0;
}
这儿不会了。。。。求教
#3
Toomj2011-09-23 10:17
if
      0 < t.year &&t.month <= 12 && t.day <=31 &&t.hour<=24&& t.minute<=60 &&t.second<=60;

if(0 < t.year &&t.month <= 12 && t.day <=31 &&t.hour<=24&& t.minute<=60 &&t.second<=60)
#4
statics2011-09-23 11:11
if(t.year > 0 &&t.month <= 12 && t.day <=31 &&t.hour< 24&& t.minute< 60 &&t.second< 60)
#5
yixiu8682011-09-23 13:35
程序代码:
struct _time t;
    cout<<"请输入年,月,日,时,分,秒:"<<endl;
    if(t.year > 0 && t.month >= 1 && t.month <= 12 && t.day >= 1 && t.day <= 31 && t.hour >= 0 && t.hour <=      12 && t.minute >= 0 && t.minute <= 59 && t.second >= 0 && t.second <= 59){
        cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
        cout<<t.year<<""<<t.month<<""<<t.day<<""<<t.hour<<""<<t.minute<<""<<t.second<<""<<endl;
    }
    else
        cout << "输入有误";
    return 0;


[ 本帖最后由 yixiu868 于 2011-9-23 13:43 编辑 ]
#6
yixiu8682011-09-23 13:38
同时还有闰年平年二月多少天不同,每个月(除二月)有的有31天,有的有30天,这些还要符合实际
#7
yixiu8682011-09-23 13:42
程序代码:
#include <iostream>
using namespace std;

struct _time
{
    short year;
    short month;
    short day;
    short hour;
    short minute;
    short second;

};

int main( )
{
    struct _time t;
    cout<<"请输入年,月,日,时,分,秒:"<<endl;
    if(t.year > 0 && t.month >= 1 && t.month <= 12 && t.day >= 1 && t.day <= 31 && t.hour >= 0 && t.hour <= 12 && t.minute >= 0 && t.minute <= 59 && t.second >= 0 && t.second <= 59){
        cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
        cout<<t.year<<""<<t.month<<""<<t.day<<""<<t.hour<<""<<t.minute<<""<<t.second<<""<<endl;
    }
    else
        cout << "输入数据有误!" << endl;
    return 0;
}
#8
lqsh2011-09-24 10:17
程序代码:
#include <map>
#include <iostream>
using namespace std;
struct _time

 {
     short year;
     short month;
     short day;
     short hour;
     short minute;
     short second;

};

int main( )

 {
     struct _time t;
     map<float,float>m;
m[1]=31;
m[2]=28;
m[3]=31;
m[4]=30;
m[5]=31;
m[6]=30;
m[7]=31;
m[8]=31;
m[9]=30;
m[10]=31;
m[11]=30;
m[12]=31;

     cout<<"请输入年,月,日,时,分,秒:"<<endl;
     cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
     if (t.year%4==0&&t.year%100!=0||t.year%400==0)
     {
         m[2]=29;
     }
     if( 0 < t.year &&t.month <= 12 && t.day <=m[t.month] &&t.hour<=24&& t.minute<=60 &&t.second<=60)
       cout<<t.year<<""<<t.month<<""<<t.day<<""<<t.hour<<""<<t.minute<<""<<t.second<<""<<endl;
      else
       cout<<"you have enter a wrong time"<<endl;
   
     return 0;

 }

 
#9
博士无双2011-09-25 18:52
建议使用try-catch结构
#10
niitzz2011-09-25 21:04
struct _time
{
    short year;
    short month;
    short day;
    short hour;
    short minute;
    short second;
};

int main( )
{
    struct _time t;
    cout<<"请输入年,月,日,时,分,秒:"<<endl;
    cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
    if(t.year>0&&t.month<=12&&t.month>=1&&t.day<=31&&t.day>=1&&t.hour<=12&&t.hour>=1&&t.minute<60&&t.minute>=0&&t.second>=0&&t.second<60)      
       cout<<t.year<<"年"<<t.month<<"月"<<t.day<<"日"<<t.hour<<"时"<<t.minute<<"分"<<t.second<<"秒"<<endl;
       else
    cout<<"you input messege is wrogng!"<<endl;
    return 0;
}
1