结构体变量问题

LLIZH061 2008-10-12 10:21:26
#include "stdafx.h"
#include <iostream>

using namespace std;

typedef struct
{
short year;
short month;
short day;
short hour;
short min;
short sec;
short m;
} time ;
int main(void)
{
time t;
cout<<"Year:"<<endl;
cin>>t.year;
cout<<"Month:"<<endl;
cin>>t.month ;
cout<<"Day:"<<endl;
cin>>t.day;
cout<<"Hour:"<<endl;
cin>>t.hour ;
cout<<"Min:"<<endl;
cin>>t.min;
cout<<"Sec:"<<endl;
cin>>t.sec;
cout<<t.year<<'-'<<t.month<<'-'<<t.day<<' '
<<t.hour<<':'<<t.min<<':'<<t.sec<<endl;
time m;
cin>>t.m;
return 0;
}
怎样禁止运行时输入不合理的数据,例如month的值大于12?
...全文
102 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jia_xiaoxin 2008-10-13
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>

using namespace std;

typedef struct
{
short year;
short month;
short day;
short hour;
short min;
short sec;
short m;
} time ;
int main(void)
{
time t;
cout < <"Year:" < <endl;
cin>>t.year;
cout < <"Month:" < <endl;
cin>>t.month ;
while(true)
{
cin >> t.month;
if(t.month <= 12 && t.month >= 1)
break;
else
{
cout << "Please input num between 1..12" << endl;
cin.clear();
cin.get();
}
}
cout < <"Day:" < <endl;
cin>>t.day;
cout < <"Hour:" < <endl;
cin>>t.hour ;
cout < <"Min:" < <endl;
cin>>t.min;
cout < <"Sec:" < <endl;
cin>>t.sec;
cout < <t.year < <'-' < <t.month < <'-' < <t.day < <' '
< <t.hour < <':' < <t.min < <':' < <t.sec < <endl;
time m;
cin>>t.m;
return 0;
}
zgjxwl 2008-10-12
  • 打赏
  • 举报
回复
偶改名的时候...
chlaws 2008-10-12
  • 打赏
  • 举报
回复
看到过那帖子
zgjxwl 2008-10-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chlaws 的回复:]
呵呵 没看明白就算了
[/Quote]
你和小崔一样,叫偶小雨,哈哈,第一次是晨星这么叫的
chlaws 2008-10-12
  • 打赏
  • 举报
回复
呵呵 没看明白就算了
lunarfan 2008-10-12
  • 打赏
  • 举报
回复
输出之前先判断
zgjxwl 2008-10-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chlaws 的回复:]
小雨 你属小强?
[/Quote]
石家说啥哈?
chlaws 2008-10-12
  • 打赏
  • 举报
回复
小雨 你属小强?
zgjxwl 2008-10-12
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;

typedef struct
{
short month;

} time ;
int main(void)
{
time t;
cout <<"Month:" <<endl;
cin>>t.month ;
if(t.month==1||t.month==2||t.month==3||t.month==4||t.month==5||t.month==6||t.month==7||t.month==8||t.month==9||t.month==10||t.month==11||t.month==12)
cout<<"The data you just input is right!"<<endl;
else
cout<<"The data you just input is wrong!";
return 0;
}
chlaws 2008-10-12
  • 打赏
  • 举报
回复
加个断言 assert(month>=0 && month<12); 注意别忘了头文件#include <assert.h>
wuyu637 2008-10-12
  • 打赏
  • 举报
回复

int main(void)
{
int months = 0;

int temp=0;
cout << "input month :" << endl;
cin >> temp;
while(temp > 12 || temp < 0)
{
cout << "input data error : month should between 1-12." << endl;
cout << "input month: " <<endl;
cin >> temp;
}
months = temp;

cout << " the months is :" << months << endl;
}
fox000002 2008-10-12
  • 打赏
  • 举报
回复
读取后判断一下,如果不符合就重新输入

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧