Help with Craps game please

Hi there, I have written this craps game in C for my assignment and am just trying to put the finishing touches on it, but the problem is I can't get it to play more than 1 or 2 games...I know it's probably a simple little fix and I have been trying a number of things but it either plays up to 2 games or a concession of them, one after the other without user intervention....anyway I have posted the code for you to see;
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void play_craps();
int die_1,die_2,die_3,die_4,roll,roll_2;
int bet=0;
int chipBalance=100;
int play=0;
int main(void)
{
    char answer,junk;
    do
    {
        printf("\n\n\n");
        printf("Would you like to play craps!\n\n");
			answer=getchar();
			junk=getchar();
		 if(answer=='y' || answer=='Y')
            play_craps();
			play++;
			
			if (play >= 1)
			
			printf("Would you like to play again?\n\n"); 
			answer=getchar();
			junk=getchar();
			
			if(answer=='y' || answer=='Y')
			play_craps();
			play++;
			return;

		}while(answer=='y' || answer=='Y');
		play_craps();
		return;
}

void play_craps()
{
    int rand_int(int a, int b);
    srand((unsigned)time(NULL));
	printf("Number of chips: %d\n", chipBalance);
	printf("\nHow much would you like to bet?\n");
	scanf("%d", &bet);

    die_1= 1+ rand() %6;
    die_2= 1+ rand() %6;
    roll=die_1 + die_2;
    printf("\nYou rolled %d + %d = %d\n",die_1,die_2,roll);
	if( roll==4 || roll==5 || roll==6 || roll ==8 || roll==9 || roll==10)
	printf("\nPoint to make is: %d\n", roll);
	
    if(roll==7 || roll==11)
        {	
			chipBalance+=bet;
            printf("* * * You Win! * * *\n");
			printf("\n You now have %d chips!\n\n", chipBalance);
			printf("\n Thanks for playing!\n");
            
        }
    else if(roll==2 || roll==3 || roll==12)
        {
			chipBalance-=bet;
            printf("* * * You Lose * * *\n");
			printf("\n You now have %d chips!\n\n", chipBalance);
			printf("\n Thanks for playing!\n");
            
        }
    else
			
        {
            do
            {

                die_3= 1+ rand() % 6;
                die_4= 1+ rand() % 6;
                roll_2=die_3 + die_4;
                printf("\n|--> rolled: %d + %d = %d\n\n",die_3, die_4, roll_2);
                if(roll_2==roll)
					{
					 chipBalance+=bet;
                     printf("\n* * * You Win * * *\n\n");
					 printf("\n You now have %d chips!\n\n", chipBalance);
					 printf("\n Thanks for playing!\n");
					}
					
			}while(roll_2 != 7);
					
					chipBalance-=bet;
					printf("You Lose");
					printf("\n You now have %d chips!\n\n", chipBalance);
					printf("\n Thanks for playing!\n");
					
			if (chipBalance <= 0)
					{
					printf( "\n Sorry, you're all out of chips\n\n");
					printf("\n *** GAME OVER ***\n\n");
					printf("\n Thanks for playing!\n");
					}
	}
return;
}
I have not looked through all the code, but I think the problem is because you have a return statement in the loop of your main function.

Your main function looks very messy. I would change it to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(void)
{
	char answer,junk;
	printf("Would you like to play craps?\n\n");
	answer=getchar();
	junk=getchar();
	while(answer=='y' || answer=='Y')
	{
		play_craps();
		play++;
		printf("\n\n\n");
		printf("Would you like to play again?\n\n");
		answer=getchar();
		junk=getchar();
	}
	return;
}
hi, this is my updated code....and the output I'm getting...now it only plays about three games before it ends with the normal press any-key message...
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void play_craps();
int die_1,die_2,die_3,die_4,roll,roll_2;
int bet=0;
int chipBalance=100;
int play;
int gamesWon=0;
int gamesLost=0;
char answer,junk;
int main(void)
{
    char answer,junk;
	
    do
    {

        printf("\n\n\n");
        printf("Would you like to play craps?\n\n");
			answer=getchar();
			junk=getchar();
		 if(answer=='y' || answer=='Y')
		 {
            play_craps();
			play++;
		 }
		 	
			
	}while(answer=='y' || answer=='Y');
		play_craps();
		
}

void play_craps()
{
    play=gamesWon+gamesLost;
	srand((unsigned)time(NULL));
	printf("Number of chips: %d\n", chipBalance);
	printf("\nHow much would you like to bet?\n");
	scanf("%d", &bet);
	die_1= 1+ rand() %6;
    die_2= 1+ rand() %6;
    roll=die_1 + die_2;
	
    printf("\nYou rolled %d + %d = %d\n",die_1,die_2,roll);
	if( roll==4 || roll==5 || roll==6 || roll ==8 || roll==9 || roll==10)
	printf("\nPoint to make is: %d\n", roll);
	
    if(roll==7 || roll==11)
        {	
			chipBalance+=bet;
            printf("* * * You Win! * * *\n");
			printf("\n You now have %d chips!\n", chipBalance);
			printf("\n Thanks for playing!\n");
            gamesWon++;
			
        }
    else if(roll==2 || roll==3 || roll==12)
        {
			chipBalance-=bet;
            printf("* * * You Lose * * *\n");
			printf("\n You now have %d chips!\n", chipBalance);
			printf("\n Thanks for playing!\n");
			gamesLost++;
			        
        }
    else
			
        {
            do
            {

                die_3= 1+ rand() % 6;
                die_4= 1+ rand() % 6;
                roll_2=die_3 + die_4;
                printf("\n|--> rolled: %d + %d = %d\n",die_3, die_4, roll_2);
                if(roll_2==roll)
					{
					 chipBalance+=bet;
                     printf("\n* * * You Win * * *\n\n");
					 printf("\n You now have %d chips!\n", chipBalance);
					 printf("\n Thanks for playing!\n");
					 gamesWon++;
					 
					}
					
			}while(roll_2 != 7);
					
					chipBalance-=bet;
					printf("You Lose");
					printf("\n You now have %d chips!\n", chipBalance);
					printf("\n Thanks for playing!\n");
					gamesLost++;
					
			if (chipBalance <= 0)
					{
					printf( "\n Sorry, you're all out of chips\n\n");
					printf("\n *** GAME OVER ***\n\n");
					printf("\n Thanks for playing!\n\n\n");
					
					}

					if(answer != 'y');
					{
					printf("\nGames played:  %d\n\n",play);
					printf("|--> Games won:  %d\n", gamesWon);
					printf("|--> Games lost: %d\n", gamesLost);
					printf("|--> Chips left: %d\n\n", chipBalance); 
					printf("\n Thanks for playing!\n");
					return;
					}		
	}
 main();
} 







Would you like to play craps?

y
Number of chips: 100

How much would you like to bet?
10

You rolled 1 + 3 = 4

Point to make is: 4

|--> rolled: 1 + 4 = 5

|--> rolled: 2 + 3 = 5

|--> rolled: 6 + 2 = 8

|--> rolled: 3 + 6 = 9

|--> rolled: 1 + 6 = 7
You Lose
 You now have 90 chips!

 Thanks for playing!

Games played:  1

|--> Games won:  0
|--> Games lost: 1
|--> Chips left: 90


 Thanks for playing!



Would you like to play craps?

y
Number of chips: 90

How much would you like to bet?
10

You rolled 6 + 6 = 12
* * * You Lose * * *

 You now have 80 chips!

 Thanks for playing!



Would you like to play craps?

y
Number of chips: 80

How much would you like to bet?
10

You rolled 3 + 1 = 4

Point to make is: 4

|--> rolled: 2 + 6 = 8

|--> rolled: 1 + 1 = 2

|--> rolled: 3 + 4 = 7
You Lose
 You now have 70 chips!

 Thanks for playing!

Games played:  3

|--> Games won:  0
|--> Games lost: 3
|--> Chips left: 70


 Thanks for playing!
Press any key to continue . . .
Last edited on
The problem is that you have the "junk" variable to get rid of the "\n" after inputting the answer in the main function, but you forgot to do it after inputting the bet amount in your play_craps function.

This should mean that you only get to play one game before the program stops, but you also made the mistake of calling the main function at the end of your play_craps function. That caused the main function to run a few times due to recursion before crapping out.

Also contributing to the multiple games is the fact that you call the play_craps function again in your main function even after the answer is determined to be something other than 'y' or 'Y'.
Topic archived. No new replies allowed.