### Craps Game Help

I have to create a craps game. I hope everybody already knows how craps work. But I need this to include a function, something about a pause. It will not work.

//Christy Windham
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
int dieOne = 0;
int dieTwo = 0;
int roll = 0;
int point = 0;
int win = 0;
int loss = 0;
double odds = 0;
srand((unsigned int)time(NULL));

dieOne = (rand()%6)+1; //Creates dice
dieTwo = (rand()%6)+1;
roll = dieOne + dieTwo;

cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl; //Prints random dice numbers to screen

if(roll==7 || roll==11) //Win on first roll
win++;
else if (roll==2 || roll==3 || roll==12)// Lose on first roll
loss++;
else if (roll==4 || roll==5 || roll==6 || roll==8 || roll==9 || roll==10)
{
point=roll;//point set
do
{

dieOne=(rand()%6)+1;//rolls dice again
dieTwo=(rand()%6)+1;
roll=dieOne+dieTwo;

cout<<dieOne<<" plus "<<dieTwo<<" = "<<roll<<endl;

if(roll==point)//wins if player rolls point
win++;
else if(roll==7)//lose if player rolls 7
loss++;
else if(roll!=7 && roll!=point) //nothing otherwise
odds=0;

}while(roll!=point && roll!=7);//keep going until point or 7 is rolled

}
ckw77 wrote:
Show that you can create a function that generates a random number from
1 to n and return the generated random number. Also show that you can display
the random number returned by this function to the console window.

I don't Understand!!

So, (s)he basically wants you to write a function take takes a number (n) and returns a random number between 1 and n. You're generating random numbers already, using rand. You just need to put that in a function and, instead of using 6 as the limit, use the number (n) passed in.

Have you done much on functions? Is there something in particular you're confused about?
Really I suck at programming... I do not understand functions at all
Have a read of this: http://www.cplusplus.com/doc/tutorial/functions/

Once you've done that, you can start building your function up bit by bit. You already know that you need it to take an integer as a parameter and return an integer, so you've essentially built the function prototype right there.

 ``1234`` ``````int GetRandomNumber( int upper ) { // Your code here... }``````

Edit: 1000th post, woohoo!
Last edited on
Okay I have read... I have created a function called GetRandom... So how do I implement that using the dice??

int GetRandom(int a, int b)
{
int r;
r=a+b;
return (r);
}

int main()
{
int dieOne = 0;
int dieTwo = 0;
int roll = 0;

So How do I call it and get a random result
This will probably be my last post then I'm going to head to sleep.

The only parameter your function needs is the upper limit of the range of random numbers, since we know the lower is one.

 ``1234567891011121314151617`` ``````int GetRandomNumber( int upper ) { // Code to generate random number here // Same as how you did it for your dice in the first post // only using upper, rather than 6 // Return the result } // To call it in main... int myNum; // Pass in whatever number you want for the upper limit myNum = GetRandomNumber(5); // Between 1 and 5 myNum = GetRandomNumber(200); // Between 1 and 200 // etc. ``````
 ``12`` `````` return 0; }``````

you can write a better code but it works fine.

I get outputs like this
 ``` 1 plus 3 = 4 4 plus 1 = 5 5 plus 5 = 10 6 plus 3 = 9 5 plus 6 = 11 1 plus 6 = 7 ```

but i think you want to get a different output.
if you want to learn win or lose use cout command end of program.
so i did not understand what you want exactly.
Topic archived. No new replies allowed.