| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2014 人关注过本帖
标题:C++提示用户输入年、月、日、小时、分、秒并显示出来,怎么添加输入条件?
只看楼主 加入收藏
c250484657
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-9-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
C++提示用户输入年、月、日、小时、分、秒并显示出来,怎么添加输入条件?
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;
}
搜索更多相关主题的帖子: 条件 include minute second 
2011-09-21 20:54
c250484657
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-9-19
收藏
得分:0 
#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;
}
这儿不会了。。。。求教
2011-09-21 20:55
Toomj
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:257
专家分:1826
注 册:2011-5-17
收藏
得分:3 
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)
2011-09-23 10:17
statics
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:163
专家分:625
注 册:2011-8-29
收藏
得分:3 
if(t.year > 0 &&t.month <= 12 && t.day <=31 &&t.hour< 24&& t.minute< 60 &&t.second< 60)

惟我独行...
2011-09-23 11:11
yixiu868
Rank: 2
等 级:论坛游民
帖 子:28
专家分:22
注 册:2010-10-25
收藏
得分:3 
程序代码:
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 编辑 ]
2011-09-23 13:35
yixiu868
Rank: 2
等 级:论坛游民
帖 子:28
专家分:22
注 册:2010-10-25
收藏
得分:0 
同时还有闰年平年二月多少天不同,每个月(除二月)有的有31天,有的有30天,这些还要符合实际
2011-09-23 13:38
yixiu868
Rank: 2
等 级:论坛游民
帖 子:28
专家分:22
注 册:2010-10-25
收藏
得分:0 
程序代码:
#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;
}
2011-09-23 13:42
lqsh
Rank: 2
来 自:山东济南
等 级:论坛游民
帖 子:26
专家分:58
注 册:2011-8-29
收藏
得分:3 
程序代码:
#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;

 }

 
2011-09-24 10:17
博士无双
Rank: 2
等 级:论坛游民
帖 子:32
专家分:65
注 册:2011-7-5
收藏
得分:3 
建议使用try-catch结构
2011-09-25 18:52
niitzz
Rank: 1
等 级:新手上路
帖 子:12
专家分:3
注 册:2011-7-20
收藏
得分:3 
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;
}
2011-09-25 21:04
快速回复:C++提示用户输入年、月、日、小时、分、秒并显示出来,怎么添加输入条 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020673 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved