Hi all,
I am making a text game in c++ and i am trying to get the player's attack to ba a random number between 1 and 7, but when i try to compile it, it gives me this error:
49 C:\Documents and Settings\Games\Desktop\C++ Tutorial\Combat\main.cpp void value not ignored as it ought to be

heres the code:

#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

class Badguy {
    int health, defense, damage; 
    char name;

    friend class Player;
    friend void damageCalc(Badguy Brutus, Player Popeye);

class Player {
    string name;
    int attack, attack1, attack2, attack3;
    Player ();  
    friend void damageCalc(Badguy Brutus, Player Popeye);

void damageCalc(Badguy Brutus, Player Popeye){
  Brutus.damage = Popeye.attack - Brutus.defense;
  if (Brutus.damage > Brutus.defense) {
    cout << "You damaged the orc!" << endl;
    Brutus.health -= Brutus.damage;
    cout << "The orc's health is: "  << Brutus.health << endl;
  else if (Brutus.damage < Brutus.defense) {
    cout << "You did'nt do any damage..." << endl;

    health = 100;
    defense = 3;

    name = "Tom";
    attack1 = 1;                                //
    attack2 = 7;                                //
    attack3 = random_shuffle(attack1, attack2); //Here is the part i am talking about
    attack = attack3;                           //

int main() {
    int loop = 1;
    string choice;
    Player p;
    Badguy b;  
    cout << "Do you want to attack the orc:";
    getline (cin, choice); 
    while (loop == 1){
      if (choice == "yes"){
        cout << "Do you want to attack again:";
        getline(cin, choice);
      else if (choice == "no"){
        loop = 0;

Any ideas?

Replace the function call in the line
attack3 = random_shuffle(attack1, attack2);
attack3 = rand() / (int) (((unsigned)RAND_MAX + 1) / 8);

include stdlib.h at the top.
rand() returns an integer between 0 and RAND_MAX which is defined in stdlib.h
int in the denominator for avoiding floating point result.
8 for the reason, you want the number between 1 to 7.

Please find some more references to rand().

A much clearer method would be rand()%x , where x is the number you want. For example, rand()%7 will return a random number between 0 and 6, and the code rand()%7+1 will return a value between 1 and 7.
If you want the numbers to be as random as possible, add the line srand(time(NULL)); before calling rand() (it's only necessary to use it once).
For more information about srand() and rand() you can read:

>A much clearer method would be rand()%x , where x is the number you want.
Clearer, but not necessarily better. rand has subtleties that need to be considered. In particular, there are two distinct problems with rand: one historical and one that is still common.

The historical problem is that older implementations of rand (and extremely poor modern implementations) show a pattern where the low order bits of the random number are decidedly nonrandom. Because the remainder trick ( rand() % N ) uses the low order bits, it fails miserably for smaller values of N.

The solution for the historical problem is to use the high order bits instead with some variation of division by RAND_MAX ( rand() / ( RAND_MAX / N + 1 ) ). If you have a small value of N and a weak implementation of rand, this method is vastly more effective.

The second problem is one of distribution. Both of the above methods (remainder and division by RAND_MAX) have the same problem with distribution, regardless of the quality of rand. Because implementations of rand have pretty much been fixed, the historical problem isn't much of a problem anymore, but the problem of distribution is still present. Except in very specific exceptions, some numbers will be produced with a higher frequency than others because rand is designed to return values in a certain range, and if you close that range then the pigeonhole principle is in effect and the hole with more pigeons will show up more often.

What the two methods above do is merge ranges into a single value. For example if the full range of rand is [0, 10) and you want a value in the range of [0,1], you need to pretend that any value in the range of [0, 5) represents 0 and any value in the range of [5, 10) represents 1:

0          1
---------  ---------
0 1 2 3 4  5 6 7 8 9

That's all well and good because 10 is evenly divisible by 2, but what if you want numbers in a range of [0, 2]? 10 isn't evenly divisible by 2, so you have to make one group larger than the other two:

0      1       2
-----  -----  -------
0 1 2  3 4 5  6 7 8 9

Now 2 will be more common than 0 and 1 because the range that's assumed to be 2 is larger than the range that's assumed to be 0 or 1. This is the distribution problem. To fix the distribution, I prefer to use a uniform deviate:

#include <cstdlib>
#include <iostream>

int main()
  for ( int i = 0; i < 20; i++ )
    std::cout<< ( std::rand() * ( 1.0 / ( RAND_MAX + 1.0 ) ) ) * 7 + 1 <<'\n';

The difference is that instead of trying to shrink the range of the random number (which breaks the distribution except when the RAND_MAX is evenly divisible by N, a uniform deviate produces a random value in the range of [0, 1) in floating-point, which can then be multiplied to get a suitable range without affecting the distribution. It's tedious to type, so a function would be better suited:

#include <cstdlib>
#include <iostream>

double udrand ( int lo, int hi )
  static const double base = 1.0 / ( RAND_MAX + 1.0 );

  double deviate = std::rand() * base;

  return lo + deviate * ( hi - lo );

int main()
  for ( int i = 0; i < 20; i++ )
    std::cout<< udrand ( 1, 7 ) <<'\n';

I'd also rant about using time to seed the random number generator, but people have a tendency to ignore my advice on that matter. ;)

For not shamelessly plugging your rather cute website!
This article has been dead for over six months. Start a new discussion instead.