为什么 randomize() 函数不起作用

Why the randomize() function not working?

本文关键字:函数 不起作用 randomize 为什么      更新时间:2023-10-16

我和我的朋友正在尝试为我们的学校项目制作一个使用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;
        ...
    }