我有一个刽子手程序,但是我在从列表中随机选择一个单词时遇到了问题......
我收到了错误:
-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)
rand
不参与
你可能想要:
int n=rand() % 5;
答案 1 :(得分:1)
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++