Help with slot machine program in C?

July 24, 2010 :: Posted by - admin :: Category - Slot Machine

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 statements
#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: , , ,

Comments Closed

2 Responses to “Help with slot machine program in C?”

  1. V Ronin Says:

    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) )

  2. husoski Says:

    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()!


Powered by Yahoo! Answers

Long Awaited Report On Lottery...

Lottery Secrets - The 10 Winning Strategies To Massive Lottery Success

Silver Lotto System
In this report You will discover:
  • 10 Lotto Playing Mistakes You Should Avoid At Any Cost
  • 3 Essential Tips For Your Lottery Game
  • Should You Play 7 Ball Games?
  • The 5 Common Game & Ticket Mistakes You Should Never Make
  • What Secret Number Should You Always Include?
  • ...And Much More
Enter Your Email Below To Get This FREE Report
Long Awaited Report On Lottery...

Lottery Secrets - The 10 Winning Strategies To Massive Lottery Success

Silver Lotto System
In this report You will discover:
  • 10 Lotto Playing Mistakes You Should Avoid At Any Cost
  • 3 Essential Tips For Your Lottery Game
  • Should You Play 7 Ball Games?
  • The 5 Common Game & Ticket Mistakes You Should Never Make
  • What Secret Number Should You Always Include?
  • ...And Much More
Enter Your Email Below To Get This FREE Report
Contact Us | Copyrights | Customer Service Policy | Disclaimer | Privacy | Terms & Conditions