简体   繁体   English

C ++文字游戏自定义时钟系统

[英]C++ Text Game Custom Clock System

Clock doesn't work. 时钟不起作用。 Once it reaches 60 mins, it continues onward without changing hour. 一旦达到60分钟,它将继续前进,而不会更改小时数。 Example: 12:75 am, 12:90 am, etc... I wanted to build a clock that adds time based on actions. 例如:上午12:75,上午12:90,等等...我想建立一个时钟,根据动作增加时间。 Does not work on real time. 不能实时工作。

Sorry for the mess, new to Stack Overflow, and sharing code in general. 很抱歉,这是Stack Overflow的新知识,并且通常会共享代码。 Keep in mind, I'm still new to coding. 请记住,我还是编码的新手。

(All integers used)
-
int timeHour = 0;
string timePMAM = "";
int timeHourDisplay = 0;
int timeHourMax = 12;

int timeMin = 0;
int timeMinDisplay = 00;

int day = 0;
string dayDisplay = "";
if (timeMin == 60)
    {
        timeMinDisplay = 0;
        timeMin = 0;
        timeHour += 1;
        timeHourDisplay += 1;
    }
    if (timeMin > 60)
    {
        timeMinDisplay -= 60;
        timeMin -= 60;
        timeHour += 1;
        timeHourDisplay += 1;
    }
    //Used for changing 60minutes into an hour

    if (timeHour < timeHourMax)
    {
        timePMAM = "am";
    }
    if (timeHour >= timeHourMax)
    {
        timePMAM = "pm";
    }
    //pm and am

    if (timeHour == 13)
    {
        timeHourDisplay = 1;
    }
    // 13 o'clock is now 1 o'clock
    if (timeHour == 24)
    {
        timeHour = 0;
        timeHourDisplay = 12;
        day += 1;
    }
    if (timeHour == 1)
    {
        timeHourDisplay = 1;
    }
    if (timeHour == 0)
    {
        timeHourDisplay = 12;
    }
}

I recommend using Howard Hinnant's free, open source date/time library . 我建议使用Howard Hinnant的免费开放源日期/时间库 It takes care of all of the time/calendrical arithmetic for you and has very flexible formatting options. 它可以为您处理所有时间/日历算法,并且格式设置非常灵活。 For example: 例如:

#include "date.h"
#include <iostream>
#include <string>

std::string
display_time(std::chrono::system_clock::time_point time)
{
    auto s = date::format("%I:%M %p", time);
    if (s.front() == '0')
        s.erase(0, 1);
    for (auto& c : s)
        c = tolower(c);
    return s;
}

std::string
display_date(std::chrono::system_clock::time_point time)
{
    return date::format("%b %e, %Y", time);
}

int
main()
{
    using namespace std;
    using namespace date::literals;
    auto time = date::sys_days{2017_y/sep/10} + 75min;

    time += 15min;
    cout << "The walk took 15 minutes: " << display_time(time) << '\n';

    cout << "You lay down on the cold ground and cry yourself to sleep, longing for burrito" << endl;
    time += 8h;
    cout << display_time(time) << endl;

    cout << "The current time is " << display_time(time) << endl;
            cout << "The date is " << display_date(time) << endl;
            cout << "It seems your watch is broken again.." << endl;
}

Output: 输出:

The walk took 15 minutes: 1:30 am
You lay down on the cold ground and cry yourself to sleep, longing for burrito
9:30 am
The current time is 9:30 am
The date is Sep 10, 2017
It seems your watch is broken again..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM