have a logic problem that i can't even see

This is a discussion on have a logic problem that i can't even see within the C++ Programming forums, part of the General Programming Boards category; i write a program that use random numbers to simulate a dice. The problem is that when i roll the ...

  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
    20,975
    Post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    Ah. Don't call srand at this point. Call it once, at the start of your global main function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21