如何获得Chrono C++的价值?

发布于 2025-01-23 16:38:58 字数 1689 浏览 0 评论 0原文

我正在尝试制作一个有计时器的文本游戏,一旦游戏在60秒之前或60秒内完成,就有一个奖励积分。但是,我不知道如何在不使用cout-ing的情况下获得使用计时的价值或时间。我想使用该值来计算奖励点。我可以通过.count()来表达该值,但是我无法将其用于条件部分使用。 这是我的评分部分的代码:

void Game::score(auto start, auto end) {
    int bonus = 0;
    int total = 0;
    string name;
    
    box();
    gotoxy(10,8); cout << "C  O  N  G  R  A  T  U  L  A  T  I  O  N  S";
    gotoxy(15,10); cout << "You have successfully accomplished all the levels!";
    gotoxy(15,11); cout << "You are now a certified C-O-N-N-E-C-T-o-r-I-s-T" << char(002) << char(001);
    gotoxy(20,13); cout << "= = = = = = = = = = GAME STATS = = = = = = = = = =";
    gotoxy(25,15); cout << "Time Taken: " << chrono::duration_cast<chrono::seconds>(end - start).count() << " seconds";
    gotoxy(25,16); cout << "Points: " << pts << " points";
        if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 60) {
            bonus == 5000;
        } else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 90) {
            bonus == 3000;
        } else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 120) {
            bonus == 1000;
        } 
    gotoxy(30,17); cout << "Bonus Points (Time Elapsed): " << bonus;
    total = pts + bonus;
    gotoxy(25,18); cout << "Total Points: " << total << " points";
    gotoxy(20,20); cout << "Enter your name: ";
    cin >> name;
    
    scoreB.open("scoreboard.txt",ios::app);
    scoreB << name << "\t" << total << "\n";
    scoreB.close();
    
}

I am trying to make a text game where there is a timer and once the game was finished before or in 60 seconds, there is a bonus points. However, I have no idea how can I get the value or the time from using the chrono without cout-ing it. I want to use the value for calculating the bonus point. i can cout the value through the .count() but I cannot get that value to use for the condition part.
here's my code for the scoring part:

void Game::score(auto start, auto end) {
    int bonus = 0;
    int total = 0;
    string name;
    
    box();
    gotoxy(10,8); cout << "C  O  N  G  R  A  T  U  L  A  T  I  O  N  S";
    gotoxy(15,10); cout << "You have successfully accomplished all the levels!";
    gotoxy(15,11); cout << "You are now a certified C-O-N-N-E-C-T-o-r-I-s-T" << char(002) << char(001);
    gotoxy(20,13); cout << "= = = = = = = = = = GAME STATS = = = = = = = = = =";
    gotoxy(25,15); cout << "Time Taken: " << chrono::duration_cast<chrono::seconds>(end - start).count() << " seconds";
    gotoxy(25,16); cout << "Points: " << pts << " points";
        if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 60) {
            bonus == 5000;
        } else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 90) {
            bonus == 3000;
        } else if (chrono::duration_cast<chrono::seconds>(end - start).count() <= 120) {
            bonus == 1000;
        } 
    gotoxy(30,17); cout << "Bonus Points (Time Elapsed): " << bonus;
    total = pts + bonus;
    gotoxy(25,18); cout << "Total Points: " << total << " points";
    gotoxy(20,20); cout << "Enter your name: ";
    cin >> name;
    
    scoreB.open("scoreboard.txt",ios::app);
    scoreB << name << "\t" << total << "\n";
    scoreB.close();
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2025-01-30 16:38:58

您应该真正使用计时文字进行比较。请参阅此处的示例:

#include <chrono>
#include <iostream>
#include <thread>

using Clock = std::chrono::system_clock;

void compareTimes(std::chrono::time_point<Clock> startTime,
                  std::chrono::time_point<Clock> finishTime) {

    using namespace std::chrono_literals;

    std::chrono::duration<float> elapsed = finishTime - startTime;

    std::cout << "elapsed = " << elapsed.count() << "\n";

    if (elapsed > 10ms) {
        std::cout << "over 10ms\n";
    }

    if (elapsed < 60s) {
        std::cout << "under 60s\n";
    }
}

int main() {
    using namespace std::chrono_literals;

    auto startTime = Clock::now();
    std::this_thread::sleep_for(20ms);
    auto finishTime = Clock::now();
    compareTimes(startTime, finishTime);
    return 0;
}

demo: https://godbolt.org/z/hqv58Acoy

You should really use the chrono literals for comparing durations. See example here:

#include <chrono>
#include <iostream>
#include <thread>

using Clock = std::chrono::system_clock;

void compareTimes(std::chrono::time_point<Clock> startTime,
                  std::chrono::time_point<Clock> finishTime) {

    using namespace std::chrono_literals;

    std::chrono::duration<float> elapsed = finishTime - startTime;

    std::cout << "elapsed = " << elapsed.count() << "\n";

    if (elapsed > 10ms) {
        std::cout << "over 10ms\n";
    }

    if (elapsed < 60s) {
        std::cout << "under 60s\n";
    }
}

int main() {
    using namespace std::chrono_literals;

    auto startTime = Clock::now();
    std::this_thread::sleep_for(20ms);
    auto finishTime = Clock::now();
    compareTimes(startTime, finishTime);
    return 0;
}

Demo: https://godbolt.org/z/hqv58acoY

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文