【发布时间】:2016-08-15 03:57:09
【问题描述】:
我正在尝试制作的这个初学者程序基本上是一个生成真正随机数并且“玩家”必须猜测数字的程序。计算机会响应“太高”或“太低”,直到玩家猜对了数字。
我从某个网站获得了这个程序的想法,并认为我应该继续使用它。这是我的第二个程序——我的第一个程序是一个简单的 I/O 程序。
这是我的代码:
// BracketingSearch.cpp
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <istream>
#include <fstream>
using namespace std;
int main()
{
int x = srand(time(0));
cout << x << endl;
for (int x = 1; x <= 10; x++) {
cout << 1 + (rand() % 100) << endl;
}
string stall;
getline(cin, stall);
}
旁注:
我认为我不需要那么多标题;我仍在使用 c++ 库。
请尽可能多地批评我的代码。我渴望学习编程!
string stall 只是为了让我的控制台应用程序暂停输入,以便我可以看到结果。
感谢所有可以提供帮助的人! -迈克
【问题讨论】:
-
您似乎有 2 个不同的问题:“为什么 srand 没有存储在我的变量中”和“请尽可能多地批评我的代码”。您在寻找code review 吗?请澄清。
-
srand的返回类型为void。我很惊讶你的编译器没有抱怨int x = srand(time(0));。 -
函数
srand()没有返回值。见这里:cplusplus.com/reference/cstdlib/srand 编辑:看起来@RSahu 快了几秒钟。 -
而不是
srand和rand看看使用std::uniform_int_distribution。 -
这是否通过了编译器?
srand确实返回 void,编译器不应允许将此值分配给 x。
标签: c++ variables random srand