GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2008, 01:20
slash24 slash24 is offline
New Member
 
Join Date: Apr 2008
Posts: 9
slash24 is on a distinguished road

roll dice animation


Hello!
i have to write code for a dice roll by displaying the numbers 1 to 6 on the screen..
This is what i have done:
CPP / C++ / C Code:
int AnimOfDice() 
{
     int delay=1,later,die;   
     later=time(0)+delay;
     srand(time(0));
     cout<<"Number on dice is....  ";
     while(later!=time(0))
     {
     die = rand()%6+1;
     cout<<die;                    
     }
     return die;
}
is this the right way to do it?It doesnt work...
Last edited by LuciWiz : 06-Apr-2008 at 12:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 03-Apr-2008, 05:40
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

Re: roll dice animation


I see 2 problems here.

1.
CPP / C++ / C Code:
while(later!=time(0))

Should be

CPP / C++ / C Code:
while(later < time(0))

Why? Because it's possible (although unlikely in this case since the body of the loop is so small) that we could skip right past the point where time(0) == later. Again, it's very unlikely that this will cause problems with your program, but it's important to understand the principle.

2.

I'm assuming that you don't want to display the results until after the time has passed. That means that you should do it after the while loop terminates:

CPP / C++ / C Code:
     while(later<time(0)) {}
     die = rand()%6+1;
     cout<<die;                    
__________________
www.blake-foster.com
  #3  
Old 03-Apr-2008, 10:16
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: roll dice animation


Quote:
Originally Posted by Blake
Should be

CPP / C++ / C Code:
while(later < time(0))
Shouldn't it be
CPP / C++ / C Code:
    while(later > time(0))
Quote:
Originally Posted by Blake
I'm assuming that you don't want to display the results until after the time has passed.

Since the function name is "Anim...," I would assume that some kind of animation is involved. See Footnote.

Maybe:

CPP / C++ / C Code:
int main()
{
    int value;
    value = AnimOfDice();
    cout << "value = " << value << endl;
    return 0;
}

int AnimOfDice()
{
    int delay = 1, later, die;
    later = time(0) + delay;
    srand(time(0));
    cout << "Number on die is.... ";
    while (later > time(0)) {
        die = (rand() % 6) + 1;
        cout << die << '\b'; // backspace over the previous number
    }
    cout << endl;
    return die;
}

If the terminal implementation supports a "backspace" character, this might give what was wanted. I mean, the '\b' special character is part of the C language standard, but not all terminals support it, so the actual action is implementation (and hardware) dependent. In other words: IWFMYMMV (It Works For Me---Your Mileage May Vary.)

Regards,

Dave

Footnote: Since the Original Poster didn't actually mention what was meant by "doesn't work," we are both guessing. Maybe I'm wrong.

To Original Poster: It might save some time and bandwidth if you would actually tell us
1. What did you expect to happen (or what did you want to happen).
2. What happened?
3. What do you not understand about the difference between 1 and 2.

I mean, why should we have to guess?

Final comment: The singular of "dice" is "die." If you have one, it's a "die." If you have two (or more), they are "dice."
  #4  
Old 03-Apr-2008, 10:25
slash24 slash24 is offline
New Member
 
Join Date: Apr 2008
Posts: 9
slash24 is on a distinguished road

Re: roll dice animation


well when i use the function as

x=AnimOfDice();

it works..

but when i use it in a loop

for(i=0;i<4;i++)

i get lots of numbers...

i need to display the dice roll for 4 players
  #5  
Old 03-Apr-2008, 11:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: roll dice animation


Quote:
Originally Posted by slash24
well ...

Well, "getting lots of numbers" doesn't really tell us much. Or maybe it's just me.


I'm not going to guess again. I respectfully suggest that you show us the code.

Then (and I hate to repeat myself) but:

1. Tell us what you expected to happen.
2. Tell us what happened.
3. Tell us what you don't understand about the difference between 1 and 2



Regards,

Dave
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Dice program killzone C++ Forum 5 22-Nov-2006 11:15
Decks and Dice cable_guy_67 C++ Forum 12 19-May-2006 09:43
Dice rolling program (random number generator) crystalattice Python Forum 1 17-Apr-2006 17:34
Possible Use of an Array???? Nexa C++ Forum 6 29-Nov-2004 19:49
problems with animation blelisa Graphics Forum 4 05-Mar-2004 07:08

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.