• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

C Programming Project Problems..

Hey guyz,

I am just having few problems with the program again. This is a followup from the previous post titled: "C Programming Project"

Just in case, i am going to post u the question or the task.

Hey guyz,

I got a c programming project due in the next 2 days.. and i am stuck.....

Here is the project:

Write a program that will simulate a slot machine.
The program should start by presenting the user with a menu: 2 dollars for regular prizes or 4 dollars for increased prize values or 0 to stop. Make sure you validate user’s input.

The program then should generate randomly three values. The program should display to the user the combination.

The lucky combinations are:
First prize – 3 cherries
Second prize – 3 apples
Third prize – 3 bananas

If the user played only 2 dollars, for all lucky combinations offer as a prize a regular prize of 50 dollars.
If the user played 4 dollars, offer as first prize 500 dollars, second prize 250, third prize 100 dollars.

For an unlucky combination offer 0 dollars.

The program should continue the execution until the user chooses to quit.
Before quitting the program should display to the user the amount of money the user spent and won during the game.


[ok. these are the functions that i am supposed to implement in the project]

int generateNum (void);//generates and returns a random int
void displayCombination (int slot);//displays bananas    
                                 // apples or cherries
int checkCombination(int slot1, int slot2, int slot3);//checks if combination is lucky and returns
                         // corresponding prize
int displayPrize(int prize, int change);//displays the
//amount won (including zero) and returns //amount won


Here is how a sample RUN looks like:

Sample run:
Welcome to my casino where everyone is a winner!

Enter change:
$2 for regular prize or
$4 for increased prize value or
$0 to quit => 44

Enter change:
$2 for regular prize or
$4 for increased prize value or
$0 to quit => 4

     ----------------------
     |CHERRY|BANANA|BANANA|
     ----------------------

NOT LUCKY! You get $0

Enter change:
$2 for regular prize or
$4 for increased prize value or
$0 to quit =>2

     ----------------------
     |APPLE| APPLE | APPLE |
     ----------------------
CONGRATULATIONS! You get $50

Enter change:
$2 for regular prize or
$4 for increased prize value or
$0 to quit =>0

You spent 6 dollars and won 50 dollars!
Good bye………
-----------------------------------------------------------------------------------------


Ok and here is my solution to the above:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

int generateNum(void);
void displayCombination1 (int slot1);
void displayCombination2 (int slot2);
void displayCombination3 (int slot3);
int checkCombination(int slot1, int slot2, int slot3);
int displayPrize(int prize, int change);



static int s1, s2, s3, chhange,amount;

main()
{
 
 int gen,change, flag;
 
 printf("*****WELCOME TO MY CASINO*****\n\n");
// while(change !=2 && change !=4 && change !=0) {
 printf("\nENTER <$ CHANGE $> \n");
 printf("\n");
 printf("$2 for regular prize or \n");
 printf("$4 for increased prized value or \n");
 printf("$0 to quit => ");
 scanf("%d", &change);  //}
 printf("\n");
 chhange = change;
 
//while (change!=0)
srand(time (NULL));

s1 = generateNum();
displayCombination1 (s1);
s2 = generateNum();
displayCombination2 (s2);
s3 = generateNum();
displayCombination3 (s3);           // displayCombination function
checkCombination(s1,s2,s3);         // checkCombination function
printf("\n");



}      


int generateNum(void)
{
      int num,i;  
    num = 1+rand()%3;
 return (num);
}


void displayCombination1 (int slot1) {


switch(slot1)   {

case 1: printf("<Apples--");
            break;

case 2: printf("<Bananas--");
        break;

case 3: printf("<cherries--");
             break;                  }
                         
}



void displayCombination2 (int slot2) {


switch(slot2)   {

case 1: printf("Apples--");
            break;

case 2: printf("Bananas--");
        break;

case 3: printf("cherries--");
             break;
                  }
                         
}


void displayCombination3 (int slot3) {


switch(slot3)   {

case 1: printf("Apples> ");
            break;

case 2: printf("Bananas> ");
        break;

case 3: printf("cherries> ");
             break;
                  }
                         
      }

int checkCombination(int slot1, int slot2, int slot3)
{

 int prizeval;
 amount =0;
 prizeval = 0;
 if((slot1=slot2) && (slot2==slot3))
 
 prizeval+= displayPrize(slot1,chhange);
 
 else
   printf("Not so lucky! You win $0 \n");
 
  amount += chhange;
return ((slot1=slot2) && (slot2==slot3));
}


int displayPrize (int prize, int change) {

 if (change ==2)
 
  printf("Congratulations! You have won $50");
 
  else
 
   switch (s1) {
   
   case 1: printf(" Congratulations! You have won $500");
           break;
               
   case 2: printf(" Congratulations! You have won $250");
                  break;
               
   case 3: printf(" Congratulations! You have won $100");
                  break;
               
                   }
                    
            return (s1);         
                    
                     }  
 
 
---------------------------------------------------------

I was just testing..the program for $2 and $4 entries.

The $2 entry seems fine for all combinations. I mean..no errors..

But, for the $4 entry,  All of them coming out right except when the combination is: B-C-C, A-B-B, C-B-B, A-C-C

The output is:

1. So, for B-C-C its showing: Congratulations! you have won $250
2. and for A-B-B its saying: Congratulation! you have won $500
3. and for C-B-B its saying: Congratulations! you have won $100
4. and for A-C-C its saying: Congratulations! you have won $500


So, something is going really wrong, when the last 2 combinations are the same....I can't figure out whats going on here..

I mean..it is supposed to check the combination for equality..., hmm, why is it giving the output like that for the above..??

Thanks

0
tarababu
Asked:
tarababu
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi tarababu,

In this function:

> int checkCombination(int slot1, int slot2, int slot3)
> {

>  int prizeval;
>  amount =0;
>  prizeval = 0;
>  if((slot1=slot2) && (slot2==slot3))

>  prizeval+= displayPrize(slot1,chhange);

>  else
>    printf("Not so lucky! You win $0 \n");

>   amount += chhange;
> return ((slot1=slot2) && (slot2==slot3));
> }


Your return statement is in error.  The first half should read (slot1 == slot2), you've only got one equal sign.




Good Luck!
Kent
0
 
hal3000Commented:
Hi tarababu,
Also note that the same error occurs in the if statement in the checkCombination function.

Cheers!
0
 
tarababuAuthor Commented:
holy smokes!! Didnt even notice that..small thing....

Man, looks like i need some rest :)

Thank You very much dudez.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now