|
From: Per W. <pw...@ia...> - 2002-10-22 00:32:32
|
Never include standard files as "iostream.h".
For old-time compatibility, you could use <iostream.h>, but preferably you
should go for <iostream> and use namespace std in the program.
The dice.h does contain a number of method implementations. Unless there
is a need to specifically use inline code for the methods, their
implementation should normally be placed in a separated dice.cpp file.
Frog.cpp also have to #include "frog.h". It isn't enough that main.cpp
contains an #include.
I'm assuming that your reference to error while debugging is in fact error
while compiling. No. All versions of Dev-Cpp - and of course all versions
of gcc (MinGW) - does support include files in a meaningful way. Did you
place your include files in a directory where the compiler searches for
them? In this case your own include files - dice.h and frog.h should
normally be placed in the same directory as the source main.cpp.
If MinGW can find the h file, it will point any compile errors in the
correct incldue. If not, your compile error should specify that it can't
locate include file xx.
One little side issue.
You have encapsulated the random number generator within the dice class,
and you specifically calls srand(time % INT_MAX).
An end user of the dice class would probably assume that he may declare
any number of dice objects, and they would each perform in a well-behaved
manner. Not so in this case.
Example. Create one dice, roll and print. Directly create another new
dice, roll and print. There is a very large proability that clock() will
return the same value both times, resulting in the roll of the second dice
returning the same value as the roll of the first dice.
In a well-behaved world, you should make sure that you only initialize the
random number generator once, independent of number of created dices.
One other thing about random numbers.
(rand() % sides) will have a higher probability of returning low numbers
for most combinations of sides. Let's assume that INT_MAX is only 8.
sides=2 divides 8, so no problem.
sides=3 doesn't, so answer may be 1,2,3,1,2,3,1,2 - extra chance of 1,2.
sides=4 divides 8, so no problem.
sides=5 doesn't, so answer may be 1,2,3,4,5,1,2,3 - extra chance of 1,2,3.
sides=6 doesn't, so answer may be 1,2,3,4,5,6,1,2 - extra chance of 1,2.
sides=7 doesn't, so answer may be 1,2,3,4,5,6,7,1 - extra chance of 1.
sides=8 divides 8, so no problem.
Depending on the number span compared to the span supported by rand(),
this skew in output might lead to significant problems. When sides gets
within a factor two of the range of rand(), the lower rolls will get TWICE
the probability of the high rolls...
/Per W
On Mon, 21 Oct 2002, Gregory Hart wrote:
> I'm having a problem with includes. When I include in main.cpp a header file
> that I created, the debugger gets passed main.cpp without any of the header
> files included and this causes errors while debugging, such as undelcared
> functions, so that I cannot compile the program. It also makes it difficult
> to find other errors in my program. I have tried just copying and pasting
> the header file and all other linked files into my code and it compiled
> successfully, but I'm programming for a class and the teacher will not let
> me copy and paste the header file into main.cpp for my final code. I'm using
> version 4.9.6.0. Does anybody know of any way to include these files so that
> I don't have to copy and paste, or does anybody know which version this
> should work in? I've included my four codes below:
>
> /* MAIN.CPP */
> // Make a frog move so many steps.
> // Created By: Gregory Hart
>
> // Includes
> #include "iostream.h"
> #include "frog.h"
>
> // Main function
> int main()
> {
> // Declare variables
> int position = 0, steps = 0, blanks;
> frog myFrog;
>
> // Get number of steps
> cout << "How many steps would you like the frog to travel? ";
> cin >> steps;
>
> // Print out frog
> while (position < steps) {
> blanks = 40 + myFrog.move();
> for (int a=0; a < blanks; ++a)
> cout << " ";
> cout << "*" << endl;
> ++position;
> }
> cout << "The final position is " << (blanks + 1);
>
> // End program
> return 0;
> }
>
> /* FROG.H */
> // Frog class
> // Created By: Gregory Hart
>
> #ifndef _FROG_H
> #define _FROG_H
>
> // Includes
> #include "dice.h"
>
> class frog
> {
> public:
> frog();
> int move();
> int getSteps() const;
> private:
> int mySteps;
> int myPosition;
> dice myDice;
> };
>
> #include "frog.cpp"
>
> #endif
>
> /* DICE.H */
> // Metrowerks and Visual version
>
> #ifndef DICE_H
> #define DICE_H
>
> #include <stdlib.h> // srand() and rand() are inside of stdlib.h
> #include <time.h>
> #include <limits.h>
>
> class dice{
> public:
> dice(int sides); // constructor
> int roll(); // return the random roll
> int numSides(); // returns how many sides on die
> long numRolls(); // returns # times this die has been rolled
> private:
> int mySides;
> long myRollCount;
> };
>
> dice::dice(int sides)
> // postcondition: all private fields initialized
> // randomize() called to set random number generator
> {
> long time = clock();
>
> srand(time % INT_MAX);
> myRollCount = 0;
> mySides = sides;
> }
>
> int dice::roll()
> // postcondition: # of rolls updated, returns random die roll
> {
> myRollCount++;
> return (rand() % mySides) + 1;
> }
>
> int dice::numSides()
> // postcondition: return # of sides of die
> {
> return mySides;
> }
>
> long dice::numRolls()
> // postcondition: return # times die has been rolled
> {
> return myRollCount;
> }
>
> #endif
>
> /* FROG.CPP */
> frog::frog()
> : mySteps(0), myPosition(0), myDice(2)
> {
> // used precondition
> }
>
> int frog::move()
> {
> ++mySteps;
> int n = myDice.roll();
> if (myDice.roll() == 1)
> --myPosition;
> else
> ++myPosition;
> return myPosition;
> }
>
> int frog::getSteps() const
> {
> return mySteps;
> }
>
>
>
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ad.doubleclick.net/clk;4699841;7576298;k?http://www.sun.com/javavote
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|