C ++时钟不工作 [英] C++ Clock Not Working

查看:145
本文介绍了C ++时钟不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的程序,用户必须输入提示号码(1-4)。其中继续的条件基于在用户选择的级别定义的时间量中输入数字,并且当然输入正确的数字。问题是,程序识别数字是否正确,但你可以采取只要你喜欢,它不会停止游戏。这个15分钟程序应该是一个回到C ++,但它已经变成一个周末长项目混乱的时钟算法和混乱。下面是整个源代码。任何帮助将是巨大的...

I wanted to make a simple program where the user has to enter a prompted number(1-4). Where the conditions of continuing is based on entering the number in the amount of time as defined by which level the user has selected and, of course, entering the correct number. The problem is that the program recognizes if the number is correct, however you can take as long as you like and it won't stop the game. This "15 minute" program was supposed to be a delve back into C++, however it has turned into a weekend long project of confusing clock algorithms and confusion. Below is the entire source code. Any help would be great...

编辑:这里是一个到在线C ++仿真器的链接,所以你可以测试程序在它的当前状态...

Here is a link to an online C++ emulator so you can test out the program in it's current state...

#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()被指定为返回进程使用的近似处理器时间。特别地,这意味着从表达式(完成开始)得到的持续时间不一定等于经过的挂钟时间的量。例如,如果你测量四个线程咀嚼CPU 1秒钟,那么你应该得到大约4秒的结果。

In the standard, clock() is specified to return the approximate processor time used by the process. In particular that means that the duration resulting from an expression (finish-start) doesn't necessarily equal the amount of wall-clock time that has passed. For example if you measure four threads chewing up CPU for 1 second then you should get a result of about 4 seconds.

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

The way this is relevant to your program is that a program that is just waiting for input or sleeping is not using any processor time, so the result of (finish-start) will be zero.

#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';
}

上述程序应该输出如下:

The above program should output something like:

CPU usage: 0
Wall clock: 1000


请注意,虽然* nix平台通常正确地实现了 clock()以返回处理器使用率,但是Windows才不是。在Windows clock()返回挂钟时间。在Windows和其他平台之间切换时,您需要谨记这一点。


Note that while *nix platforms in general correctly implement clock() to return processor usage, Windows does not. On Windows clock() returns wall-clock time. You need to keep this in mind when switching between Windows and other platforms.


。使用< random> 的随机int的语法是什么? - Noguiguy

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);
}

这篇关于C ++时钟不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆