使用结构体来精确表示年、月、日、小时、分、秒

声明一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒;
提示用户输入年、月、日、小时、分、秒的值,
然后完整的显示出来

#include<iostream>
using namespace std;

//定义结构体
struct MyTimeStruct
{
    unsigned int year;
    unsigned int month;
    unsigned int day;

    unsigned int hour;
    unsigned int min;
    unsigned int sec;
};

//主函数
int main()
{
    MyTimeStruct mytime = {2015,3,16,12,0,0};
    cout << "now my time is: " << mytime.year;

    cout << "please input year:" << endl;
    cin >> mytime.year;

    cout << "please input month:" << endl;
    cin >> mytime.month;

    cout << "please input day:" << endl;
    cin >> mytime.day;

    cout << "please input hour:" << endl;
    cin >> mytime.hour;
    
    cout << "please input min:" << endl;
    cin >> mytime.min;

    cout << "please input sec:" << endl;
    cin >> mytime.sec;

    cout << "the time is set to :" << mytime.year << "/"
                                << mytime.month << "/"
                                << mytime.day << " "
                                << mytime.hour << ":"
                                << mytime.min << ":"
                                << mytime.sec << endl;
    return 0;
}

程序中并没有实现对输入数值的控制,有可能输入的数值根本不符合要求,如在输入月份时输入13,这个数值就是没有价值的,所以我们应该想办法对输入的数值进行检查和控制。有如下两种办法可以实现

  1. 配合使用枚举类型,约束各个时间元素的数值
  2. 使用if语句对输入的数值进行判断检查
  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曾爱吃榴莲

非常感谢你的认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值