# Thread: Craps the Dice Game

1. ## Craps the Dice Game

What is the way to have a random number from 1-6 for the game craps, and how do u output it. Hope someone can help...

2. to get a random number use the rand() function, this gives you a random integer. To specify the range you want you need to use the % operator.
Code:
```int num;

num = rand() % 6;```
The above will give you a number between 0 - 5, so you will also have to add 1 to num to get 1 - 6;
Code:
```int num;

num = rand() % 6 + 1;

printf("%d", num);```

3. you'll find the rand function in <stdlib.h>

4. ## another question

This is my code...

int func_dice()

{ int dice1, dice2, number;
cout<<"Let's play CRAPS!!";
cout<<endl;
cout<<"Let's roll the dice...";
dice1=(Rand()%6)+1;
dice2=(Rand()%6)+1;
cout<<"You rolled a "<<printf("%d", dice1)<<" and a "<<printf("%d", dice2);
cout<<endl;
number=dice1+dice2;
cout<<"The total was "<<number;

It says that it wont work because printf() and rand() aren't properly called functions...any help?

5. as your using c++ you don't need the printf()

just do cout << dice1 << dice2;

6. ## more problems

Here's my whole code so far...

//crapsgame.cpp
//This is a form of game called craps which uses functions, looping, arrays, switchs, and enums

#include <iostream.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int func_name();
int func_dice();

int main()

{
cout<<"Hello, and welcome to my program. I hope you enjoy it!";
func_name();
func_dice();

return 0;

}

//************************************************** ***************************
//This portion finds the name of the player

int func_name()

{
char f_name[20];
cin>>f_name;
char l_name[30];
cin>>l_name;
cout<<endl;
cout<<"Welcome "<<f_name<<" "<<l_name;
cout<<endl<<endl<<endl;

return 0;
}

//************************************************** ***************************
//This is the actual game portion of the program

int func_dice()

{ int dice1, dice2, number;
cout<<"Let's play CRAPS!!";
cout<<endl;
cout<<"Let's roll the dice...";
dice1=(Rand()%6)+1;
dice2=(Rand()%6)+1;
cout<<"You rolled a "<<dice1<<" and a "<<dice2;
cout<<endl;
number=dice1+dice2;
cout<<"The total was "<<number;

return 0;

}

It still says this as my error...
"Implicit declaration of function 'int Rand(...)'

Hope you can help, and thanx for the help so far..

7. dice1=(Rand()%6)+1;
dice2=(Rand()%6)+1;

change the capital 'R' to a lower case 'r'.
I did this then your prog compiles and runs fine

8. ## idea

Again, thanx for the help. One last thing. When I do the random numbers every time i open it up the do the same numbers...
It's always:
6,6
5,5
6,5
1,1
5,3
6,6
2,4
..........

9. The reason your getting the same numbers is because rand generates them at compile time not when the program is run.

http://www.cprogramming.com/boardfaq.html#random

read here for further info on random numbers, it gives an example of how to get different numbers each time.

10. Mmmm for some reason i got logged out.

11. ## switch statement and loop

I have 2 questions now. 1st:
In my switch statemnt it is only using the last number is each case. (12, and 11) it's ignoring the 7, 2, and 3 and just counting it as a default. How do i change it to more then one thing for each case?

switch(point1)
{
case (7,11):
{cout<<"You win!! Congratulations!"<<endl<<endl;
break;}
case (2,3,12):
{cout<<"You lose. Sorry better luck next time."<<endl<<endl;
break;}
default:
{cout<<"This is your current point. Let's keep on rolling.";
cout<<endl<<endl<<endl;
break;}

2nd Question:
After when they get they're point I need to be able to loop it. If they get a 7, 11 then they win and it's done, and if they get a 2, 3, or 12 then they're done. But if the number is a 4,5,6,8,9,10 it becomes there "point". Then i need it to remember that "point" and it needs to roll again. If it is a 7 then they automatically lose.. Also, if it's they're number then they win. But if it's not there then they have to roll again and again till they either get their number to win or a 7 to lose. Hopefully u can help me out with the 2 questions. Thanx, everyone!!!

12. First question : use seperate cases for each number like so
Code:
```switch(point1)
{
case 7  :
case 11 : cout << "You win!! Congratulations!";
break;
case 2  :
case 3  :
case 12 : cout << "You lose.";
break;
default : cout << "This is your current point.";
break;
}```
When you have cases without break statements the program carries on down through the other cases until it encounters one.

Second question : You obviously need to loop, so its just figuring out a control expression something like.
Code:
```while(point1 >= 4 && point1 <= 6 || point1 >= 8 && point1 <= 10)
{
// put code here
}```
Then what you need to do is add if statements to some of your cases in the switch
Code:
```switch(point1)
{
case 7  : if( This is not the first go)
{
cout << "You lose.";
break;
}
case 11 : cout << "You win!! Congratulations!";
break;
case 2  :
case 3  :
case 12 : cout << "You lose.";
break;
default : cout << "This is your current point.";
break;
}```