Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,392 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,145 people online right now. Registration is fast and FREE... Join Now!




Dice roller

 
Reply to this topicStart new topic

Dice roller

MingusDew
28 Sep, 2006 - 05:04 PM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2006
Posts: 13


My Contributions
ok so here it is, Im trying to make a dice roller/random number generator this is what i got so far

CODE

#include <iostream>
#include <stdlib>

using namespace std;

int main()
{
  int a = random ( 100 - 1 ) + 1,b = random ( 20 - 1 ) + 1,c = random ( 12 - 1 ) + 1,Q=0,d;
  while ( Q < 20 ) {
  Q++;
  cout<<"What type of dice do you want to roll d%, d20, or d12?\n";
  cin>> d;
  cin.ignore();
  if ( d == 12 ) {
  cout<< c;
    }
  else if ( d == 20 ) {
  cout<< b;
    }
  else ( d == 100 ) {
  cout<< a;
    }
  }
  cin.get();
}
  


i have been working on this for close to a week on and off, and i get a "statement missing ; in function main()" at line 20, and a warning that 'a' is assigned avalue that is never used in function main()

any help any body
User is offlineProfile CardPM
+Quote Post

MingusDew
RE: Dice Roller
28 Sep, 2006 - 08:55 PM
Post #2

New D.I.C Head
*

Joined: 28 Sep, 2006
Posts: 13


My Contributions
ok so i got the else statement workingo on line 20 and the program works now. But it only gives out one value for each die type (i.e. 20 gives a value of 14). Do I need some type of algorithm or am I thinking too hard?
User is offlineProfile CardPM
+Quote Post

DeeViLiSh
RE: Dice Roller
28 Sep, 2006 - 10:17 PM
Post #3

D.I.C Head
Group Icon

Joined: 25 Jul, 2006
Posts: 175



Thanked: 1 times
Dream Kudos: 575
My Contributions
Your random function = too hard thinking =P
Space your code a little, that might help you understand more what's going on.
For the random function :
CODE

#include <time.h>
#include <iostream.h>

#define MAX 100
#define MIN 1
int main()
{
   int a;

   srand(time(NULL));
   a = (rand() % (MAX - MIN + 1)) + MIN;
  
    cout<<a;
    return 0;
}


Compile and run and you'll understand it.
This function is from the time.h header.
User is offlineProfile CardPM
+Quote Post

MingusDew
RE: Dice Roller
28 Sep, 2006 - 11:12 PM
Post #4

New D.I.C Head
*

Joined: 28 Sep, 2006
Posts: 13


My Contributions
ok this is the reformed code that i got when applied your strategy
CODE

#include <time>
#include <iostream>

using namespace std;

int main()
{
  int a,b,c,d;

  srand(time(NULL));
  a = (rand() % ( 100 - 1 )) + 1;
  b = (rand() % ( 20 - 1 )) + 1;
  c = (rand() % ( 12 - 1 )) + 1;

  cout<<"What type of dice do you want to roll d%, d20, or d12?\n";
  cin>> d;
  cin.ignore();
  if ( d == 12 ) {
  cout<< c<<"\n";
    }
  else if ( d == 20 ) {
  cout<< b<<"\n";
    }
  else {
  cout<< a<<"\n";
    }
}
  

but i guess the problem in having it in a while loop, I have to run the program again once i need a new roll, I'm thinking its because once it runs it once the same data set is used when finding another roll in the same instance of the program. Is there possibly a way to refresh the proposed data set?

By the way thank you
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:38AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month