在两个输入c++之间花费时间
take time between two inputs c++
我有一个小项目的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/
我看不出有任何问题。顺便说一下,您缺少main
和after
的返回值。你也不能做int main (timetaker)
,那应该是什么?
您的srand函数应该使用main
函数中的数据吗?您需要在函数int after
中传递一些参数。
我也不建议使用using namespace std;
,因为如果您要实现自己的cout函数,这可能会导致未定义的行为。使用std::
是更好的方法。不过,这是你的选择,在这个代码中它是好的。
如果你想利用时间,你可以查看这些链接:
时间
时钟
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 构造函数和转换运算符之间的重载解析
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数