14 Replies - 26843 Views - Last Post: 01 February 2010 - 02:03 PM Rate Topic: -----

#1 MingusDew   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-September 06

Dice roller

Posted 28 September 2006 - 06:04 PM

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

#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

Is This A Good Question/Topic? 0
  • +

Replies To: Dice roller

#2 MingusDew   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-September 06

Re: Dice roller

Posted 28 September 2006 - 09:55 PM

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?
Was This Post Helpful? 0
  • +
  • -

#3 DeeViLiSh   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: Dice roller

Posted 28 September 2006 - 11:17 PM

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 :
#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.
Was This Post Helpful? 0
  • +
  • -

#4 MingusDew   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-September 06

Re: Dice roller

Posted 29 September 2006 - 12:12 AM

ok this is the reformed code that i got when applied your strategy
#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
Was This Post Helpful? 0
  • +
  • -

#5 demonten   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-April 09

Re: Dice roller

Posted 26 May 2009 - 06:18 PM

I have got this program running properly except for one thing. I'm missing "time.h" Do any of you guys know where I could get the header file? thanks!
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6968
  • View blog
  • Posts: 31,958
  • Joined: 10-May 07

Re: Dice roller

Posted 26 May 2009 - 06:52 PM

View Postdemonten, on 26 May, 2009 - 07:18 PM, said:

I'm missing "time.h"

What compiler are you using?
Was This Post Helpful? 0
  • +
  • -

#7 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dice roller

Posted 27 May 2009 - 03:44 AM

View Postdemonten, on 26 May, 2009 - 05:18 PM, said:

I have got this program running properly except for one thing. I'm missing "time.h" Do any of you guys know where I could get the header file? thanks!


It shouldn't matter but the header is <ctime> in C++ (rather than <time.h>).

I can't see that should prevent a compile but worth a try (and it's more correct in C++ anyway).
Was This Post Helpful? 0
  • +
  • -

#8 questionwhy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-June 09

Re: Dice roller

Posted 03 June 2009 - 11:50 PM

Okay, I'm using this for a character generator, but I've got a serious problem. I've got it coming up with a random number and then spitting out a certain value based on that number. But any time I have it set with an else if statement, it only ends in a one. It's probably something really stupid since I'm new to C++. Thanks.
#include <time.h>
#include <iostream.h>

#define MAXRace 10
#define MAXClass 16
#define MAXGender 2
#define MIN 1
using namespace std;

int main()
{
	int g;
	int m;
	cout << "Would you like to [m]ake you're own character or have me [g]enerate on for you?"
//Generated character
   ;if (cin >> g);
	   ;int a;
	   ;int b;
	   ;int c;
srand(time(NULL));
		c = (rand() % (MAXGender - MIN + 1)) + MIN;
			if (c = 1)
				cout<<"Male";
			else if (c = 2)
				cout<<"Female";
		cout<<" ";
		srand(time(NULL));
		a = (rand() % (MAXRace - MIN + 1)) + MIN;
			if (a = 1)
				cout<<"Dragonborn";
			else if (a = 2)
				cout<<"Dwarf";
			else if (a = 3)
				cout<<"Eladrin";
			else if (a = 4)
				cout<<"Elf";
			else if (a = 5)
				cout<<"Gnome";
			else if (a = 6)
				cout<<"Half-elf";
			else if (a = 7)
				cout<<"Half-orc";
			else if (a = 8)
				cout<<"Halfling";
			else if (a = 9)
				cout<<"Human";
			else if (a = 10)
				cout<<"Tiefling";
		cout<<" ";
		srand(time(NULL));
		b = (rand() % (MAXClass - MIN + 1)) + MIN;
			if (b=1)
				cout<<"Avenger";
			else if (b=2)
				cout<<"Barbarian";
			else if (b=3)
				cout<<"Bard";
			else if (b=4)
				cout<<"Cleric";
			else if (b=5)
				cout<<"Druid";
			else if (b=6)
				cout<<"Fighter";
			else if (b=7)
				cout<<"Invoker";
			else if (b=8)
				cout<<"Paladin";
			else if (b=9)
				cout<<"Ranger";
			else if (b=10)
				cout<<"Rogue";
			else if (b=11)
				cout<<"Shaman";
			else if (b=12)
				cout<<"Sorcerer";
			else if (b=13)
				cout<<"Warden";
			else if (b=14)
				cout<<"Warlock";
			else if (b=15)
				cout<<"Warlord";
			else if (b=16)
				cout<<"Wizard";

This post has been edited by questionwhy: 03 June 2009 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Dice roller

Posted 04 June 2009 - 12:16 AM

you want to make a Dice Roller ...

but the only thing i saw was a randomness,

#include <time.h>
#include <iostream>
#include <cstdlib>
using namespace std;

int DICE = 1;

void RollTheDice(); // function prototype

int main()
{
   srand(time(NULL));
   int user_choice = 2;

   do
   {
		RollTheDice(); // a function call do everything
		
		cout << "\n 1. Roll the Dice again\n 2. exit\n\n choose : ";
		cin >> user_choice;
   }while(user_choice==1);

   return 0;
}

void RollTheDice()
{
	DICE = 1+rand()%6; // 1,2,3,4,5,or 6
	
	cout << "\n\t"<<(char)218 <<(char)196<<(char)196<<(char)196 <<(char)191 <<"\n\t"<<(char)179;
	
	switch(DICE)
	{
		  case 1:
				cout << " . ";
				break;
		  case 2:
				cout << " : ";
				break;
		  case 3:
				cout << ". :";
				break;
		  case 4:
				cout << ": :";
				break;
		  case 5:
				cout << ":.:";
				break;
		  case 6:
				cout << ":::";
				break;
	}

	cout <<(char)179 << "\n\t"<<(char)192 <<(char)196<<(char)196<<(char)196 <<(char)217 <<"\n";
}



this code will just roll your dice and prints it on the screen as a DICE !

:: if you show your appreciation, I'll be happy to help

the secret behind success is: plan everything on a paper before coding !

and remember, you can make the dice make faces instead of dots or numbers or letters as short form of words

1,2 .. 6 __or__ :),:(, .. XD __or__ A,B .. F __or__ @,# .. $

different symbols, different meanings, different analogy of the game your making !

This post has been edited by Plus: 04 June 2009 - 12:47 AM

Was This Post Helpful? 0
  • +
  • -

#10 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dice roller

Posted 04 June 2009 - 03:09 AM

View Postquestionwhy, on 3 Jun, 2009 - 10:50 PM, said:

Okay, I'm using this for a character generator....


Welcome to DIC!

Best to start your own new topic thread to ask your question.

Hijacking other people's threads will generally just get your question ignored.
Was This Post Helpful? 0
  • +
  • -

#11 demonten   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-April 09

Re: Dice roller

Posted 04 June 2009 - 01:42 PM

I have Code::Blocks, Dev C++ and Windows Visual Express 2008. None of them have it. Also I changed it to "ctime" and now it says "srand" and "rand" were not declared in their respective scopes.

This post has been edited by demonten: 04 June 2009 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#12 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dice roller

Posted 05 June 2009 - 02:43 AM

View Postdemonten, on 4 Jun, 2009 - 12:42 PM, said:

I have Code::Blocks, Dev C++ and Windows Visual Express 2008. None of them have it. Also I changed it to "ctime" and now it says "srand" and "rand" were not declared in their respective scopes.


Show us your code.

But how about you start your own thread with your code and your latest question instead of continuing to confuse the issue by hijacking this ancient thread from 2006?

This post has been edited by janotte: 05 June 2009 - 02:49 AM

Was This Post Helpful? 0
  • +
  • -

#13 demonten   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-April 09

Re: Dice roller

Posted 05 June 2009 - 12:04 PM

 #include <ctime>
#include <iostream>

using namespace std;

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


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

	cout<< "What tipe of die would you like 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";
	}
} 

Was This Post Helpful? 0
  • +
  • -

#14 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dice roller

Posted 06 June 2009 - 05:59 AM

Did you notice people ignoring your posting?

I think you may have more success if you read what I wrote above and follow that advice.
Was This Post Helpful? 0
  • +
  • -

#15 collossus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-February 10

Re: Dice roller

Posted 01 February 2010 - 02:03 PM

Hey Demonten,
Are you cool with me snagging this starter? I want to tweak it a bit, but I can't find any other places with sample code and I just started A C class but I wouldn't know where to start otherwise.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1