0

I'm learning C++ by the program http://stanford.edu/class/archive/cs/cs106b/cs106b.1142/lectures.shtml and was doing example from the text book about random numbers inplementation. There was header file:

#ifndef _random_h
#define _random_h
/*
 * Function randomInteger
 * Usage: int n = randomInteger(low, high);
 * ---------------------------------------
 * returns a random integer in the range low to high, inclusive.
 */
int randomInteger(int low, int high);
/*
 * Function: randomReal
 * Usage: double d = randomReal(low, high)
 * ---------------------------------------
 * Returns a random real number in the half-open interval [low, high). A
 * half-open interval includes the first endpoint but not the second, which
 * means that the result is always greater than or equal to low but
 * strictly less than high.
 */
double randomReal(double low, double high);
/*
 * Function: randomChance
 * Usage: if(randomChance(p)) ...
 * -------------------------------
 * Returns true with the probability indicated by p. The argument p must
 * be a floating-point number between 0 (never) and 1 (always). For
 * example, calling randomChance(.30) returns true 30 percent of time.
 */
bool randomChance(double p);
/*
 * Function: setRandomSeed
 * Usage: setRandomSeed(seed);
 * ---------------------------
 * Sets the internal random number seed to the specified value. You can
 * use this function to set a specific starting point for the pseudorandom
 * sequence or to ensure that program behavior is repeatable during the 
 * debugging phase.
 */
void setRandomSeed(int seed);
#endif

And the source file itself:

#include <iostream>
#include "random.h"
using namespace std;

/*Function prototypes*/
bool tryToMakePoint(int point);
int rollTwoDice();

/*Main program*/
int main(){
    cout << "This program plays a game of craps." << endl;
    int point = rollTwoDice();
    switch (point){
    case 7: case 11:
        cout << "That's a natural. You win." <<endl;
        break;
    case 2: case 3: case 12:
        cout << "That's craps. You lose." << endl;
        break;
    default:
        cout << "Your point is " << point << "." << endl;
        if (tryToMakePoint(point)){
            cout << "You made your point. You win." <<endl;
        } else {
            cout << "You rolled a seven. You lose." << endl;
        }
    }
    return 0;
}

bool tryToMakePoint(int point){
    while(true){
        int total = rollTwoDice();
        if (total == point) return true;
        if (total == 7) return false;
    }
}

int rollTwoDice(){
    cout << "Rolling the dice ..." <<endl;
    int d1 = randomInteger(1, 6);
    int d2 = randomInteger(1, 6);
    int total = d1 + d2;
    cout << "You rolled " << d1 << " and " << d2 << " - that's " << total << endl;
    return total;
}

They both are in the same directory. While compiling I get:

highlander@linux-f62d:~/Documents/CS106b> g++ Craps.cpp /tmp/ccWyN2aR.o: In function rollTwoDice()': Craps.cpp:(.text+0x165): undefined reference torandomInteger(int, int)' Craps.cpp:(.text+0x177): undefined reference to `randomInteger(int, int)' collect2: error: ld returned 1 exit status

What's wrong?

Tsiskreli
  • 103
  • 1
  • 11

1 Answers1

3

You have a declaration of the function

int randomInteger(int, int);

However, it has no definition, another way of putting it is it doesn't know what to do functionally when you call it. You need something like:

int randomInteger(int low, int high) {
    return 4; // a dice was rolled to determine a random value
}
Scott Greenup
  • 183
  • 1
  • 9