help with assignment

hi, I have this assignment in C programming that I need to submit very soon and my problem is that this craps game doesn't work as intended, the while loop in the game should allow the user to play the game when 'y' is selected but should exit and show the gameStats when 'n' is chosen in the craps_game() function but this does not happen. ANy help would be greatly appreciated

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
int main(void)
{
int play=gamesWon+gamesLost;
	srand((unsigned)time(NULL));
	display_details();
		
	
    do
    {
char answer,junk;	

		

	    printf("\n\n\n");
		printf("Would you like to play craps [y|n]?  ");
		answer=getchar();
		junk=getchar();
		printf("\n\n\n");

	
	 
		 if(answer=='y' || answer=='Y')
			 play_craps();
				
			
		}while(answer=='y' || answer=='Y'); 
	
	
return;
}
void play_craps()
{
int play=gamesWon+gamesLost;

do
	{
	
		srand((unsigned)time(NULL));
		
		printf("+-------------------------------------+\n\n"); 
		printf("Number of chips: %d\n\n", chipBalance);
		printf("\nPlace your bet: ");
		scanf("%d", &bet);
		printf("\n\n");
		
		while(bet < 0 || bet > chipBalance)
					{
					printf("+---------------------------------------+");
					printf("\nSorry, you can only bet what you have (0-%d)!\n", chipBalance);
					printf("\nPlace your bet: ");
					scanf("%d", &bet);
					printf("\n\n");
					}
		
							

	die_1 = 1+ rand() %6;	
	die_2 = 1+ rand() %6;	
	roll = die_1 + die_2;	
				
				printf("\nYou rolled %d + %d = %d\n\n",die_1,die_2, roll);
			 	if( roll==4 || roll==5 || roll==6 || roll ==8 || roll==9 || roll==10)
				printf("Point to make is: %d\n\n", roll);
				if(play>1)
				printf("Point to make is: %d\n\n", roll);	
							
		
			 if(roll==7 || roll==11)
					{
				chipBalance+=bet;
				gamesWon++;
				printf("\n* * * You Win! * * *\n\n");
				printf("\n You now have %d chips!\n\n", chipBalance);
				printf("+-------------------------------------+\n\n\n");
				printf("\n Play Again [y|n]? ");
				answer=getchar();
				junk=getchar();
				
				}
			   
			else if(roll==2 || roll==3 || roll==12)
				{
				chipBalance-=bet;
				gamesLost++;
				printf("\n* * * You Lose! * * *\n\n");
				printf("\n You now have %d chips!\n\n", chipBalance);
				printf("+-------------------------------------+\n\n\n");
				if (chipBalance<=0) 
					{gameOver();return;}
				else if (chipBalance > 0 )
				printf("\n Play Again? ");
				answer=getchar();
				junk=getchar();
				
				 }
	   
				
		 else 
			 
			{
					
				do
				{
play=gamesWon+gamesLost;			
                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;
					 gamesWon++;
                     printf("\n* * * You Win! * * *\n\n");
					 printf("\n You now have %d chips!\n\n", chipBalance);
					 printf("\n+-------------------------------------+\n\n");
					 printf("\n Play Again? ");
					 answer=getchar();
					 junk=getchar();
					 
					 									
				}
					
				
			}while(roll_2 != 7);
				
					chipBalance-=bet;
					gamesLost++;
					printf("\n* * * You Lose! * * *\n\n");
					printf("\nYou now have %d chips!\n\n", chipBalance);
					printf("+-------------------------------------+\n\n");
					if (chipBalance<=0)
					{gameOver();}	
					if (chipBalance > 0 )
					printf("\nPlay Again? ");
					answer=getchar();
					junk=getchar();
																	
				}
		 
	
	
	
	
		
			
	 }while (answer=='y' || answer=='Y');
		

 gameStats();return;
}

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
void gameOver(void)
{
					
		printf( "\n Sorry, you're all out of chips\n\n");
		printf("\n *** GAME OVER ***\n\n");
		{gameStats();return;}
					
return;
}

void gameStats()
{
			
play=gamesWon+gamesLost;


					printf("\n\n\n");
					printf("\n Games 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;
}


Last edited on
http://www.cplusplus.com/forum/beginner/83826/

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.
I did not have to use the 'junk' variable because 'scanf' ignores all empty spaces '\n' and new line statements....& besides I am after an integer value not a string. However this still does not solve the problem.
I think you're not posting all your code and you're having trouble with local vs global scope.

You have "answer" declared in your main function yet you also use it inside your play_craps function, without it being declared there. If it compiled successfully, then it must mean you also had answer declared globally (outside of all functions).

The global "answer" variable is overridden by the local "answer" variable in the main function. The check inside the main function is carried out on the local variable. Inside the play_craps function, you are changing the value of the global variable.
hi, well this is the rest of the code showing the declarations;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void gameOver();
void play_craps();
void display_details();
void gameStats();
int roll_die();
int die_1,die_2,die_3,die_4,roll,roll_2;
int bet=0;
int chipBalance=100;
int gamesWon=0;
int gamesLost=0;
int play;
char answer,junk;

Topic archived. No new replies allowed.