craps game

i have to create a craps game and i have looked at other code for it but mine doesn't need a betting system in it.

this is what i have so far and it is saying i haven't declared repeat but i did.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void main ()

{
int die1;
int die2;
int roll1;
int roll2;
int value;
char repeat;
srand((unsigned)time(0));
repeat = y;
cout << "Welcome\n\n" << "The game is about to begin\n\n";
while (repeat == y)
{
system ("pause");
die1 = (rand()% 6+1);
die2 = (rand()% 6+1);
roll1 = die1 + die2;
cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 << endl;
if (roll1 == 7 || roll1 == 11)
{
cout << "You win!\n\nWould you like to play again? <Y/N>: ";
cin >> repeat;
}
else if (roll1 == 2 || roll1 == 3 || roll1 == 12)
{
cout << "Sorry you lose! Would you like to try again? <Y/N>: ";
cin >> repeat;
}
else
value == roll1;
}
return(0);
}

i'm sure there are many other mistakes because i'm not done writing it but this is the one that is holding me up right now.

thanks
if you use code tags it is easier to read (click the # sign on the right under format and insert your code between the tags)
Also, if you mark your post as a question, you usually get more replies.

I am just a begginer, but on line 15 you have repeat = y; but y is not declared.

I also notice you have main as void, usually it should be int, but void functions do not return value(s) and you have it return 0.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void main ()

{
int die1;
int die2;
int roll1;
int roll2;
int value;
char repeat;
srand((unsigned)time(0));
repeat = y;
cout << "Welcome\n\n" << "The game is about to begin\n\n";
while (repeat == y)
{
system ("pause");
die1 = (rand()% 6+1);
die2 = (rand()% 6+1);
roll1 = die1 + die2;
cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 << endl;
if (roll1 == 7 || roll1 == 11)
{
cout << "You win!\n\nWould you like to play again? <Y/N>: ";
cin >> repeat;
}
else if (roll1 == 2 || roll1 == 3 || roll1 == 12)
{
cout << "Sorry you lose! Would you like to try again? <Y/N>: ";
cin >> repeat;
}
else
value == roll1;
}
return(0);
}
Last edited on
Hi, this should work.

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
#include <iostream>
#include <ctime>
#include <limits>

int main()
{
    srand(time(0)); //seeds random generator

    int die1, die2 = 0; //you can put them together to make it smaller + neater
    int roll1, roll2 = 0;
    char repeat = 'y'; //when declaring a char with a letter you have to put ' ' around it

    std::cout << "Welcome. The game is about to begin." << std::endl;

    while (repeat == 'y' || repeat == 'Y') //still needs ' '
    {
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //recommend this instead of system("PAUSE") you will need '#include <limits>"
                                                                            //you can find why this is better in a long thread somewhere in these forums
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1; //dont need brackets around rand()
        roll1 = die1 + die2;

        std::cout << "Your roll was: " << die1 << " + " << die2 << " = " << roll1 << std::endl;
        if (roll1 == 7 || roll1 == 11)
        {
            std::cout << "You win! Would you like to play again? [Y/N]:" << std::endl;
            std::cin >> repeat;
        }
        else if (roll1 == 2 || roll1 == 3 || roll1 == 12)
        {
            std::cout << "Sorry, you lose! Would you like to play again? {Y/N]:" << std::endl;
            std::cin >> repeat;
        } //what happens if it's not 7, 11, 2, 3 or 12?
    }

    return(0);
}


You can add some more stuff to it but i'm pretty sure this fixes your problem.
Last edited on
Topic archived. No new replies allowed.