在两个输入c++之间花费时间

take time between two inputs c++

本文关键字:c++ 之间 费时间 输入 两个      更新时间:2023-10-16

我有一个小项目的tropel,希望得到一些帮助。这是迄今为止的代码。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(timetaker) 
{
}
int after()
{
    srand(data from above);
    for (int x = 1; x<2;x++)
    {
      cout << 1+(rand()) << endl;
    }
}

我使用tropole的是一个需要时间并将其提供给int after()函数的函数。但如果能为int main(计时器)提供一些帮助,我将不胜感激

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
    {
        std::cout << "First Click: ";
        std::cin.ignore();
        unsigned int start = clock();
        std::cin.ignore();
        std::cout << "Next when you are ready ";
        std::cin.ignore();
        std::cout << "Time taken in millisecs: " << clock() << endl;
        std::cout << "Now for the random number. Are you ready" << endl;
        std::cin.ignore();
        srand(clock());
        for (int x = 1; x<2;x++)
        {
          cout << 1+(rand()) << endl;
        }
        std::cout << "That is the random number from the time taken.";
        return 0;
    }

如果将要调用的代码放在要调用的位置之前,会更容易。获取srand()的时间值,并将其从main()传递,可以如下所示进行。。。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void after(time_t seed)
{
    srand(seed);
    for (int x = 1; x<2;x++)
    {
      cout << 1+(rand()) << endl;
    }
}
int main() 
{
    do_stuff(time(NULL));
}

考虑到这个问题的模糊性,我认为这是您最好的资源:

http://www.cplusplus.com/reference/ctime/time/

我看不出有任何问题。顺便说一下,您缺少mainafter的返回值。你也不能做int main (timetaker),那应该是什么?

您的srand函数应该使用main函数中的数据吗?您需要在函数int after中传递一些参数。

我也不建议使用using namespace std;,因为如果您要实现自己的cout函数,这可能会导致未定义的行为。使用std::是更好的方法。不过,这是你的选择,在这个代码中它是好的。

如果你想利用时间,你可以查看这些链接:

时间

时钟