craps game in c++

i was asked to make a craps game using empty console in c++

roll two dice. Each die has six faces representing values 1,2...., and 6, respectively. Check the sum of the two dice. If the sum is 2,3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (ie., 4,5,6,7,8,9,10), a point is established. Continue until you roll either a 7 (you lose) or the same point value (you win).

Your program acts as a single player.

i need to add 3 void functions:
void GetRoll(int &);
void CalcSum(int, int, int &);
void PrintRoll(int, int, int);

this is wat i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


void GetRoll(int &);
void CalcSum(int, int, int &);
void PrintRoll(int, int, int);


int main()
{
	const int dice = 6;
	srand((unsigned)time(0));		//sets a new seed for random function
	int RollDie,rolldice2, TotalRoll;
	GetRoll(RollDie);
	CalcSum(RollDie, rolldice2, TotalRoll);
	PrintRoll(one, two, three);
	return 0;
}

int GetRoll(int & RollDie)
{
	RollDie = rand()%7; 
}
void CalcSum(int RollDie,int rolldice2, int & TotalRoll)
{
	TotalRoll = RollDie + RollDie2;
}

void PrintRoll(int, int, int);
{
	cout << "You rolled: " << RollDie << " + " << RollDie2 << " = " << TotalRoll << endl;
}


what i need help with, is how to get two random values, using one parameter in the void GetRoll, then somehow having it in two parameters for the Void CalcSum and Void PrintRoll.


any help will be appreciated....
asap ty.

You initially prototype GetRoll() as type void (meaning no return value), but then define it to return type int (without actually returning a value).

You can do it either way, but my recommendation would be to use
1
2
3
4
int GetRoll();

dice1 = GetRoll();
dice2 = GetRoll();


GetRoll() has to return a value of type int (the value you randomly calculate).

rand()%7 provides a value between 0 and 6, not what you want.
And to get a random value between 1 and 6: rand() % 6 + 1

Last edited on
what jraskell said ^^, as an inclusion be careful of your naming conventions. methods and variables are written in camel case, constants are written in camel as well as methods eg:
int myNumberToPlayWith = 1000; // first letter is lowercase
const int anotherNumber = 1000;

to solve it for you, because I learn in the process :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

int getRoll()
{
    return 1+rand()%DIE;
}
void rollDice()
{
    dice1=getRoll();
    dice2=getRoll();
}
int calcSum()
{
    return dice1 + dice2;
}

int main()
{
    //vars
    const int DIE = 6;
    int dice1, dice2, totalRoll;
    srand((unsigned)time(NULL));

    // start app
    rollDice();
    totalRoll = calcSum();

    // print
    cout << "You rolled: " << dice1 << " + " << dice2 << " = " << totalRoll << endl;

    // check win / loose
    int num=totalRoll;
    if (num==2||num==3||num==12)
    {
        cout << "you loose";
    }
    else if (num==7||num==11)
    {
        cout  << "you win";
    }
    else
        cout << "draw";

    return 0;
}
Last edited on
gcampton, your code has a variable scope issue with DIE.
i tried gcamptons way and it doesnt compile. it says dice1 and dice2 not declared.

can you plz help me...i still dont get it...
And in any case, don't just post the solution gcampton. Even if you learn, the OP doesn't if you just do it for them.
anyone? this the code i have now.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int getRoll();
void calcSum(int, int, int &);
void printRoll(int, int, int);


int main()
{
    const int DIE = 6;
    int dice1, dice2,totalRoll;
    srand((unsigned)time(NULL));
	getRoll();
	calcSum(dice1, dice2, totalRoll);
	printRoll(dice1, dice2, totalRoll);
	
    

    return 0;
}


int getRoll()
{
	int DIE = rand()%6 + 1; 
    return DIE;
}

void calcSum(int dice1, int dice2, int & totalRoll)
{
	dice2=getRoll();
	dice1=getRoll();
    totalRoll = dice1 + dice2;
}
void printRoll(int dice1, int dice2, int totalRoll)
{
	cout << "You rolled: " << dice1 << " + " << dice2 << " = " << totalRoll << endl;

    int num=totalRoll;
    if (num==2||num==3||num==12)
    {
        cout << "you loose";
    }
    else if (num==7||num==11)
    {
        cout  << "you win";
    }
    else
        cout << "draw";
}
nekomata, you seem to be struggling with how to use functions, more specifically how to pass parameters into functions and get return values from them, and the scope of variables across separate blocks of code.

Check out this link for a tutorial on using functions (there are two pages total, a link to the second page is at the bottom of the first).

http://cplusplus.com/doc/tutorial/functions/

Topic archived. No new replies allowed.