| ckw77 (9) | |
|
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 } | |
|
|
|
| iHutch105 (1070) | |||
From other thread...
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? | |||
|
|
|||
| ckw77 (9) | |
| Really I suck at programming... I do not understand functions at all | |
|
|
|
| iHutch105 (1070) | |||
|
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.
Edit: 1000th post, woohoo! | |||
|
Last edited on
|
|||
| ckw77 (9) | |
|
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 | |
|
|
|
| iHutch105 (1070) | |||
|
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.
| |||
|
|
|||
| karakale (14) | ||||
|
i tried your code. if you add these lines:
you can write a better code but it works fine. I get outputs like this
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. | ||||
|
|
||||