Thread: have a logic problem that i can't even see

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    have a logic problem that i can't even see

    i write a program that use random numbers to simulate a dice. The problem is that when i roll the rice both random generates are give me the same number even thought i use the time function as the seed.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    this the implementation of the header that generates the numbers

    Code:
    #include <iostream>
    #include "Dice.h"
    #include <ctime>
    using namespace std;
    
    /* Constructor that initializes data member */
    Dice :: Dice ()
    {
         dice1 = 0;
         dice2 = 0;
    }//end constructor
    
    
    
    /* Set content of dice1 data member */
    void Dice :: setDice1 ()
    {
         srand ( time (0));
         dice1 = 1 + rand ()% ( 6 - 1 + 1 );
    }//end setDice
    
    
    
    /* Set content of dice2 data member */
    void Dice :: setDice2 ()
    {
         srand ( time (0));
         dice2 = 1 + rand () % ( 6 - 1 + 1);
    }//end setDice2
    
    
    /* Gets content of dice1 data member */
    int Dice :: getDice1 ()
    {
        setDice1 ();
        return dice1;
    }//end getDice1
    
    
    /* Gets the content of dice2 data member */
    int Dice :: getDice2 ()
    {
        setDice2 ();
        return dice2;
    }//end getDice2

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Don't call srand at this point. Call it once, at the start of your global main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    what do you think about that code in regards to indentation and logic

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your indentation is fine. As for logic: why do you call setDice1 in getDice1? If you are going to do that, then you only need getDice: you don't need any member variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    the reason i did that is because i want each of my functions to solve one problem. if i did not call setDice in getDice when i call the getDice function in main it would only return 0 ( note: i would have to call setDice first and i think call it in getdice would fix that problem).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mdennis10
    if i did not call setDice in getDice when i call the getDice function in main it would only return 0 ( note: i would have to call setDice first and i think call it in getdice would fix that problem).
    Looking at the interface that your class provides, I would expect that I should call setDiceX, then I can access getDiceX as many times as I want to get the same value. Changing the value on me would come as an unpleasant surprise. On the other hand, if there was only getDice (which could be a non-member function), then I expect it to return a random die roll each time it is called.

    By the way, I believe dice is plural. The singular form should be die.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    i wanted to do this in OOD and that's why i used data members

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I'm telling you my reaction to the interface and its behaviour. You are of course free to ignore my feedback in the name of OOD, but OOD is a means to an end: just because you have member variables doesn't mean that you have a good OOD.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    what exactly is soooo bad about it

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The setDiceN member functions don't make sense when you are calling them from getDiceN. Are they private member functions? If so, then that's fine, but then you also don't need the member variables since they are always getting overwritten only.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    i understand what you are saying but one of the things i took in consideration was that if the user call the getDice function without first calling the setDice function it would creat a problem, so to fix that i taught by eliminating the need for them to call it by calling it implicitly for them in getDice would be a good idea

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mdennis10
    one of the things i took in consideration was that if the user call the getDice function without first calling the setDice function it would creat a problem
    So, instead of initialising the member variables to 0, "roll the dice" in the constructor. Renaming setDiceN to rollDiceN might also be a good idea.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    ok thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic Problem
    By Phyxashun in forum C# Programming
    Replies: 16
    Last Post: 11-01-2010, 05:59 AM
  2. Logic problem
    By MarlonDean in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 05:11 AM
  3. Logic problem
    By earth_angel in forum C Programming
    Replies: 6
    Last Post: 07-26-2005, 03:01 AM
  4. Logic problem?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-10-2002, 04:10 PM
  5. Re : Logic problem
    By Wah in forum C Programming
    Replies: 5
    Last Post: 01-31-2002, 02:19 PM