-3

I have a hangman program, but I'm having issues with randomly choosing a word out of the list...

I get the errors:

-error C2661: 'rand' : no overloaded function takes 1 arguments

-IntelliSense: too many arguments in function call

They are both referring to the rand function noted in the code where I'm trying to randomly choose a word out of the array so the user can guess.

// 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
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
user1368970
  • 555
  • 4
  • 10
  • 19
  • What are you trying to do? Pick a random number between 0 and 4? – godel9 Dec 08 '13 at 19:40
  • 1
    And why don't you believe the compiler when he says that "no overloaded function takes 1 arguments"? What do you expect `rand(5)` to do; and on what grounds? – Oswald Dec 08 '13 at 19:40
  • I'm trying to choose a random word out of the array of words. And I have tried different values and I get the error regardless. – user1368970 Dec 08 '13 at 19:42
  • Well, in that case, here's the documentation: [`std::rand()`](http://en.cppreference.com/w/cpp/numeric/random/rand) for C++, [`rand()`](http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html) for C. – Oswald Dec 08 '13 at 19:44
  • I include std even though I have using namespace std at the beginning? I tried that, but it didn't fix the "overloaded function takes 1 argument" error. – user1368970 Dec 08 '13 at 19:46
  • 1
    That's because no version of `rand()` takes one argument. – Oswald Dec 08 '13 at 19:47
  • There's no argument to be passed to `rand()`! There's no argument to be passed to `rand()`! There's no argument to be passed to `rand()`! There's no argument to be passed to `rand()`! There's no argument to be passed to `rand()`! There's no argument to be passed to `rand()`! **GOT IT?** – πάντα ῥεῖ Dec 08 '13 at 19:48
  • Sorry.. Do I just remove the rand() then? That's the only error I'm getting with my code I just don't know how to pick a random word out of the array and use it. :/ – user1368970 Dec 08 '13 at 19:51
  • @user1368970 No! Just don't pass an argument (parameter) to `rand()`! _'That's the only error I'm getting with my code'_ There are other errors, see the answers how to use `rand()` properly ... – πάντα ῥεῖ Dec 08 '13 at 19:53
  • @g-makulik LOL, I don't know which part of either your comments or answers below, OP is having trouble with – P0W Dec 08 '13 at 19:56
  • I changed to: int n=0 + ( std::rand() % ( 4 - 0 + 1 ) ); strcpy_s(word,words[n]); and it worked!! – user1368970 Dec 08 '13 at 19:57
  • @user1368970 _'`( 4 - 0 + 1 )`'_ Did you mean to say `5`?? – πάντα ῥεῖ Dec 08 '13 at 19:58
  • @user1368970 Good. FYI.. you used `using namespace std;` so your `int n=0 + ( std::rand() % ( 4 - 0 + 1 ) );` is exactly `int n=rand() % 5;` :S – P0W Dec 08 '13 at 19:59
  • I found you could do it like that online. Sorry, I'm new to this..?.. – user1368970 Dec 08 '13 at 20:00
  • @user1368970 _'I found you could do it like that online.'_ Can you show the link? Either you didn't get it right or the article/post should be removed ... – πάντα ῥεῖ Dec 08 '13 at 20:03
  • http://stackoverflow.com/questions/7560114/random-number-c-in-some-range I replaced those numbers in the first answer with mine and it worked. Idk why. I also included an srand function and my program works perfectly! – user1368970 Dec 08 '13 at 20:13
  • I edited the posted code to reflect what I have that works. – user1368970 Dec 08 '13 at 20:15
  • @user1368970 `25 + ( std::rand() % ( 63 - 25 + 1 ) )` is pretty much the same as `25 + ( std::rand() % 39)`, so what's your point? – πάντα ῥεῖ Dec 08 '13 at 20:18
  • _'I edited the posted code to reflect what I have that works.'_ You shoudn't do it this way, but update(edit) your question giving this information additionally- Otherwise it might render answers given prior your edits useless (which isn't good for readers to follow). – πάντα ῥεῖ Dec 08 '13 at 20:22
  • I didn't have a point, I just said it worked for me...? – user1368970 Dec 08 '13 at 20:23

2 Answers2

4

rand takes no argument

You probably want :

int n=rand() % 5;

P0W
  • 46,614
  • 9
  • 72
  • 119
  • That took away argument error, now the only error I get is: error C2661: 'rand' : no overloaded function takes 1 arguments – user1368970 Dec 08 '13 at 19:43
  • @user1368970 Look carefully what you wrote, you shouldn't provide any argument – P0W Dec 08 '13 at 19:45
1

std::rand() generates a pseudo random number, if you want a number 0, 1, 2 3 or 4, which i guess you are trying to do use the following:

int n = rand()%5;

This generates a random number and then the % gives the rest value if you would divide by 5. If the random number is 12, you can 'put two fives in it', and the rest value will be 2.

One thing to note is that in C++ using rand() is generally a bad idea, because it will not give you a truly random number, and using % makes it worse, but I think you will be fine since this is just a small program.

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

Kevin
  • 2,739
  • 33
  • 57