将 sleep() 与 rand() 一起使用,在 C++ 中出现错误
我正在尝试创建一个涉及“计算机”向您打字的游戏。我无法获得打字效果,而不是自动显示输出。如果有人有任何建议来实现这一点,我愿意接受想法,因为我在 C++ 方面还是新手。
所以我试图在 sleep 方法中生成一个随机数,如下所示:
Sleep(rand()%1500);
非常简单。可能太简单了哈哈。但我收到错误:“表达式必须具有算术或枚举类型。”
我实现打字效果的想法是循环输出的每个字符加上这个 sleep 方法,直到没有更多的输出为止。显示为:
for(int i = 0; i <= output.length(); i++){
cout << output[i] + Sleep(random);
}
感谢您的帮助!!!
I'm trying to create a game that involves the "computer" typing to you. I'm having trouble getting the typing effect instead of having the output automatically appear. If anyone has any suggestions to achieve this I'm open to ideas since I'm pretty novice at C++.
So I'm trying to get a random number to generate within the sleep method which is shown as:
Sleep(rand()%1500);
Pretty simple. Maybe too simple haha. But I'm getting the error: "expression must have arithmetic or enum type."
My idea to achieve the typing effect is to loop each character of the output plus this sleep method until there is no more output left. This is shown as:
for(int i = 0; i <= output.length(); i++){
cout << output[i] + Sleep(random);
}
Thanks for any help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是没有意义的 -
Sleep()
没有(有意义?)返回值,并且将结果添加到输出中没有任何意义。把它分成两个独立的陈述:
你应该没问题。
This doesn't make sense --
Sleep()
has no (meaningful?) return value, and it doesn't make any sense to add the result to your output.Break it up into two independent statements:
and you should be fine.
您无法打印 Sleep 来进行 cout。你想要的是这样的:
You can't print Sleep to cout. What you want is this: