C++ 时钟不工作

C++ Clock Not Working

我想制作一个简单的程序,用户必须输入提示的数字 (1-4)。继续的条件是根据用户选择的级别定义的时间量输入数字,当然,输入正确的数字。问题是程序会识别数字是否正确,但是您可以想玩多久就玩多久,它不会停止游戏。这个“15 分钟”的程序本来应该是对 C++ 的深入研究,但它已经变成了一个周末的项目,混淆了时钟算法和混乱。下面是完整的源代码。任何帮助都会很棒...

编辑:Here 是一个 link 在线 C++ 仿真器,因此您可以在当前状态下测试程序...

#include <iostream>
#include <time.h>
#include <ctime>
#include <unistd.h>


using namespace std;

int main() {

    //input
    string numberGuessed;
    int intNumberGuessed;
    int score = 0;
    int actualNumber;
    int gameOver = 0;

    //Level Select Variables
    char level = {0};
    string input = " ";
    double timeForInput;


    //initialize random seed
    srand (time(NULL));


    //Generates random number
    actualNumber = rand() % 4 + 1;

    //LEVEL SELECTOUR
    cout << "Select A Level: 'e' 'm', or 'h':  ";
        getline(cin, input);

        if (input.length() == 1) {
            level = input[0];
        }

        if (level == 'e') {
            cout << "You Have Selected Easy..." << endl;
            cout<< "You Have .5 Second to Enter" << endl;
            timeForInput = 5;

        } else if(level == 'm'){
            cout << "You Have Selected Medium..." << endl;
            cout<< "You Have .2 Seconds to Enter" << endl;
            timeForInput = 2;

        } else if(level == 'h'){
            cout << "You Have Selected HARD!!!" << endl;
            cout<< "You ONLY Have .1 Seconds to Enter" << endl;
            timeForInput = 1;

        } else {
            cout << "You LOSE! GOOD DAY SIR!" << endl;
        }

        //Instructions and Countdown
        cout<<"Press The Number and Hit Enter in The Amount of Time Provided"<<endl;
        sleep(1);
        cout<<"3"<<endl;
        sleep(1);
        cout<<"2"<<endl;
        sleep(1);
        cout<<"1"<<endl;
        sleep(1);
        cout<<"GO!"<<endl;
        cout<<"--------------------------------------------------------------------------------"<<endl;
        cout<< "Enter the Numbers As They Appear:"<<endl;
        cout<<endl;
        cout<<endl;
        cout<<endl;
        sleep(1);
        double duration = 0.0;




        do {
        //Procedere For Each Round

        clock_t start;
        clock_t finish;


        start = clock();
        finish = clock();

        double delay = (double)(finish-start);


        //Clock
            start = clock();

            cout<<"The number is:  "<< actualNumber<<endl;
            getline(cin, numberGuessed);
            intNumberGuessed = stoi(numberGuessed);

            finish = clock();

            double elapsed = (double)(finish-start);
            elapsed-=delay;

            duration = elapsed/CLOCKS_PER_SEC;
            cout<<duration<<endl;

            //Test User's input
            if((intNumberGuessed == actualNumber) && (duration <= (timeForInput/10))){
                score += 1;
                gameOver = 0;

            } else if ((intNumberGuessed != actualNumber) || (duration >= (timeForInput/10))) {
                gameOver = 1;
            }

            //Reset Number
           actualNumber = rand() % 4 + 1;

        } while (gameOver != 1);


        cout<<"You Failed!"<<endl;
        sleep(1);
        cout<<"Your Score Was:  "<<score<<endl;

        return 0;

}

在标准中,clock() 被指定为 return 进程使用的大概处理器时间。特别是,这意味着表达式 (finish-start) 产生的持续时间不一定等于已经过去的挂钟时间量。例如,如果您测量四个线程消耗 CPU 1 秒,那么您应该得到大约 4 秒的结果。

这与您的程序相关的方式是,只是等待输入或休眠的程序未使用任何处理器时间,因此 (finish-start) 的结果将为零。

#include <iostream>

#include <chrono>   // std::chrono::seconds, milliseconds
#include <thread>   // std::this_thread::sleep_for

#include <ctime>    // std::clock()

int main() {
    auto start_processor_usage = std::clock();
    auto start_wall_clock = std::chrono::steady_clock::now();

    std::this_thread::sleep_for(std::chrono::seconds(1));

    auto finish_processor_usage = std::clock();
    auto finish_wall_clock = std::chrono::steady_clock::now();

    std::cout << "CPU usage: " << (finish_processor_usage - start_processor_usage) / CLOCKS_PER_SEC << '\n';
    std::cout << "Wall clock: " << (finish_wall_clock - start_wall_clock) / std::chrono::milliseconds(1) << '\n';
}

上面的程序应该输出如下内容:

CPU usage: 0
Wall clock: 1000

请注意,虽然 *nix 平台通常正确实现 clock() 到 return 处理器使用,但 Windows 不会。在 Windows clock() return 秒挂钟时间。在 Windows 和其他平台之间切换时需要牢记这一点。


Ok Sure. What is the syntax for a random int using <random>? – Noguiguy

#include <random>

int main() {
  // initialize with random seed
  std::random_device r;
  std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
  std::mt19937 engine(seed);

  // define distribution: improved version of "% 4 + 1"
  auto one_to_four = std::uniform_int_distribution<>(1, 4);

  // generate number
  auto actualNumber = one_to_four(engine);
}

我也发现 clock() 没有给出我想要的结果。 所以我只是写了一个简单的计时器 class 来使用 Windows 上的 QueryPerformanceCounter 和 Linux

上的 clock_gettime 来计算持续时间

试试这个:https://github.com/AndsonYe/Timer

:)