【问题标题】:Error with rand function C++ [closed]rand函数C ++出错[关闭]
【发布时间】:2013-12-08 19:37:59
【问题描述】:

我有一个刽子手程序,但我在从列表中随机选择一个单词时遇到问题...

我得到错误:

-error C2661: 'rand' : 没有重载函数需要 1 个参数

-IntelliSense:函数调用中的参数过多

它们都指的是代码中提到的 rand 函数,我试图从数组中随机选择一个单词,以便用户猜测。

// Hang.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

using namespace std;

const int MAXLENGTH=80;
const int MAX_TRIES=5;
const int MAXROW=7;

int letterFill (char, char[], char[]);
void initUnknown (char[], char[]);

int main ()
{
char unknown [MAXLENGTH];
char letter;
int num_of_wrong_guesses=0;
char word[MAXLENGTH];
char words[][MAXLENGTH] =
{
    "india",
    "america",
    "germany",
    "china",
    "canada"
};

      //THIS IS WHERE THE ISSUE IS OCCURRING vvvvvvv
//choose and copy a word from array of words randomly
rand();
int n=rand(5);
strcpy(word,words[n]);

    // Initialize the secret word with the * character.
initUnknown(word, unknown);

// welcome the user
cout << "\n\nWelcome to Hangman!";
cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the word.";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
    cout << "\n\n" << unknown;
    cout << "\n\nGuess a letter: ";
    cin >> letter;
    // Fill secret word with letter if the guess is correct,
    // otherwise increment the number of wrong guesses.
    if (letterFill(letter, word, unknown)==0)
    {
        cout << endl << "Sorry, that letter was wrong." << endl;
        num_of_wrong_guesses++;
    }
    else
    {
        cout << endl << "You found a letter!" << endl;
    }
    // Tell user how many guesses has left.
    cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
    cout << " guesses left." << endl;
    // Check if they guessed the word.
    if (strcmp(word, unknown) == 0)
    {
        cout << word << endl;
        cout << "You guessed it!";
        break;
    }
}
if(num_of_wrong_guesses == MAX_TRIES)
{
    cout << "\nSorry, you lose...you've been hanged." << endl;
    cout << "The word was : " << word << endl;
}
getch();
return 0;
}

/* Take a one character guess and the secret word, and fill in the
 unfinished guessword. Returns number of characters matched.
 Also, returns zero if the character is already guessed. */

int letterFill (char guess, char secretword[], char guessword[])
{
int i;
int matches=0;
for (i = 0; secretword[i]!='\0'; i++)
{
    // Did we already match this letter in a previous guess?
    if (guess == guessword[i])
        return 0;
    // Is the guess in the secret word?
    if (guess == secretword[i])
    {
        guessword[i] = guess;
        matches++;
    }
}
return matches;
}


// Initialize the unknown word

void initUnknown (char word[], char unknown[])
{
int i;
int length = strlen(word);
for (i = 0; i < length; i++)
    unknown[i]='*';
unknown[i]='\0';
} 


// end

【问题讨论】:

  • 你想做什么?选择 0 到 4 之间的随机数?
  • 当编译器说“没有重载函数需要 1 个参数”时,你为什么不相信呢?你希望rand(5) 做什么;有什么理由?
  • 我正在尝试从单词数组中选择一个随机单词。而且我尝试了不同的值,但无论如何我都会收到错误。
  • 好吧,在这种情况下,这里是文档:std::rand() 用于 C++,rand() 用于 C。
  • 这是因为rand() 的任何版本都没有一个参数。

标签: c++ arrays random overloading


【解决方案1】:

rand 不带参数

你可能想要:

int n=rand() % 5;

【讨论】:

  • 带走了参数错误,现在我得到的唯一错误是:错误 C2661:'rand':没有重载函数需要 1 个参数
  • @user1368970 仔细看你写的,你不应该提供任何论据
【解决方案2】:

std::rand() 生成一个伪随机数,如果你想要一个数字 0、1、2 3 或 4,我猜你正在尝试使用以下内容:

int n = rand()%5;

这会生成一个随机数,然后如果您除以 5,则 % 会给出余数。如果随机数是 12,您可以“在其中放入两个五”,余数将为 2。

需要注意的一点是,在 C++ 中使用 rand() 通常是一个坏主意,因为它不会给你一个真正的随机数,而使用 % 会使情况变得更糟,但我认为你会没事的,因为这只是一个小程序。

Watch this talk if you want to know how to properly generate a random number in c++

【讨论】:

  • 此代码生成一个介于 0 和 4 之间的值,而不是介于 0 和 5 之间的值。
  • 不,rand()%5 永远不会生成 -1。
  • “Between”不明确;它可以是排他性的或包容性的。我不应该使用它,你也不应该坚持它只有一个含义。
猜你喜欢
  • 2016-07-07
  • 1970-01-01
  • 2019-02-25
  • 2017-07-06
  • 2012-11-08
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多