为什么 randomize() 函数不起作用
Why the randomize() function not working?
我和我的朋友正在尝试为我们的学校项目制作一个使用C++制作"刽子手"游戏。但是在编译时,消息显示标准函数 randomize() 和 random 没有在此范围内声明。代码中有什么问题?
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXLENGTH=80;
const int MAX_TRIES=5;
const int MAXROW=7;
int letterFill (char guess, char secretword[], char guessword[]) {
int matches = 0;
for (int i = 0; secretword[i]!=' '; i++) {
if (guess == guessword[i])
return 0;
if (guess == secretword[i]) {
guessword[i] = guess;
matches++;
}
}
return matches;
}
void initUnknown (char word[], char unknown[]) {
int i, length = strlen(word);
for (i = 0; i < length; i++)
unknown[i]='*';
unknown[i]=' ';
}
int main () {
char unknown [MAXLENGTH];
char letter;
int wrong_guesses=0;
char word[MAXLENGTH];
char words[][MAXLENGTH] = { "batman begins", "superman returns", "2012",
"tarzan", "goal", "300", "space jam", "transformers", "megamind",
"spiderman" };
randomize();
int n=random(10);
strcpy(word,words[n]);
initUnknown(word, unknown);
cout << "nnWelcome to hangman...Guess a Movie Name";
cout << "nnEach letter is represented by a star.";
cout << "nnYou have to type only one letter in one try";
cout << "nnYou have " << MAX_TRIES << " tries to try and guess the word.";
cout << "n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
while (wrong_guesses < MAX_TRIES) {
cout << "nn" << unknown;
cout << "nnGuess a letter: ";
cin >> letter;
if (!letterFill(letter, word, unknown)) {
cout << endl << "Whoops! That letter isn't in there!" << endl;
wrong_guesses++;
} else
cout << endl << "You found a letter! Isn't that exciting!" << endl;
cout << "You have " << MAX_TRIES - wrong_guesses;
cout << " guesses left." << endl;
if (!strcmp(word, unknown)) {
cout << word << endl;
cout << "Yeah! You got it!";
break;
}
}
if(wrong_guesses == MAX_TRIES) {
cout << "nSorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cin.get();
}
在 C++ 或 C 中,您可以使用 rand 生成随机数。 您可以在 http://www.cplusplus.com/reference/cstdlib/rand/
定期!
使用 srand() 播种 rand()。
#include <cstdlib>
#include <cstddef>
#include <ctime>
...
int main(...){
...
srand(time(NULL));
int randomByte = rand()%256;
...
}
相关文章:
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么我的数组双精度函数不起作用?
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ Arduino - 随机函数不起作用
- 为什么我的数组或函数不起作用?
- 从类中的另一个文件请求函数不起作用
- 双链表堆栈删除函数不起作用
- 将 cmake 代码段转换为函数 - 不起作用
- 为什么我的从base64解码的函数不起作用?
- 为什么当我们使用等于'='符号比较器函数时,c ++的内置排序函数不起作用?
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 子类化 STL 容器:范围构造函数不起作用
- 带有此指针的模板类多重继承构造函数不起作用?
- 以下打印树函数不起作用为什么?
- 为什么内存集函数不起作用?
- 调用填充我的主体数组的函数不起作用
- 带有自定义数字的阶乘函数不起作用
- 如何QDialog raise()函数不起作用
- Lambda 表达式闭包函数不起作用
- C++:在没有 lvalue 的情况下调用时复制构造函数不起作用