Help with slot machine program in C?
Question by J: Help with slot machine program in C?
I got most everything working but I am trying to prompt the user to enter “1″ to activate the slot machine, any other value should return the printf message “ERROR” however, no matter what the user inputs it still runs. I’m not sure if my if statement is off but here is my code:
#include
#include
#include
int main(void) //main
{
int the_bank = 0; //Declare variables
int start = 0;
int wager = 0;
int wheel_1 = 0;
int wheel_2 = 0;
int wheel_3 = 0;
printf(“n Welcome to Joe’s Slot Machine”); //Greet User State purpose Step 1
printf(“n Enter a whole dollar amount for which you wish to use as your purse for wagering: “);
scanf(“%d”, &the_bank); //Prompt and receive bank amount //step 2
printf(“nn Bank is %5d n”, the_bank); //Let user know bank Step 3
do
{
printf(“n Enter wager: “); //Ask for and receive wager Step 4
scanf(“%d”, &wager);
the_bank = (the_bank – wager); //Subtract wager from bank Step 5
printf(“nn Bank is now %d n “,the_bank);
printf(“Wager is %d n”, wager); //State bank and wager Step 6
printf(“n Enter 1 to activate the slot machine: n”);
scanf(“%d”, &start);
if( (start = 1) )
{
{
{
int wheel_1 = 0 , wheel_2 = 0 , wheel_3 = 0 , total = 0, winnings = 0 ; //Spin wheel Step 7
srandom( (unsigned) time(NULL) ) ;
wheel_1 = random( ) % 6 + 1 ; // Assogn wheel random values Step 8
wheel_2 = random( ) % 6 + 1 ;
wheel_3 = random( ) % 6 +1 ;
printf(“nn Wheel 1 : %d n”, wheel_1 ); // Display wheel amounts Step 9
printf(” Wheel 2 : %d n”, wheel_2 );
printf(” Wheel 3 : %d n”, wheel_3 );
if( (wheel_1 == wheel_2) && (wheel_2 == wheel_3) )
{printf(“WINNER”);
switch( wheel_1 )
{
case 1:
winnings = 5 * wager;
break;
case 2:
winnings = 10 * wager;
break;
case 3:
winnings = 20 * wager;
break;
case 4:
winnings = 40 * wager;
break;
case 5:
winnings = 80 * wager;
break;
case 6:
winnings = 160 * wager;
break;
}
printf(“n You won: %d”,winnings);
the_bank = (the_bank + winnings);
}
else
printf(“Loser”);
}
printf(“n Bank is: %d n “,the_bank);
}
}
else if(start != 1)
printf(“ERROR”);
}
while(the_bank > 0); //Close loop Step 11
printf(“n You have run out of money n”); //Make the user cry Step 11
}
Best answer:
Answer by Madu.s.a.
Restart the computer
Know better? Leave your own answer in the comments!
Tags: Help, Machine, Program, Slot

July 24th, 2010 at 9:39 pm
to check if a fariable is equal to another the operator is == not =
with if( (start = 1) ) you assign to start the value 1, the correct statement is if( (start == 1) )
July 24th, 2010 at 10:23 pm
Change “if (start = 1)” to “If (start == 1)”.
Lots of people have used the practice of putting the number first when comparing for equals:
if (1 == start)
…says the same thing, but will generate a message if you mistype == as =.
By the way, how does this even compile? The function names are srand() and rand(), not srandom and random()!