Solved

C Programming Project Problems..

Posted on 2004-04-10
3
243 Views
Last Modified: 2010-04-15
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
Comment
Question by:tarababu
3 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 10798354
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
 
LVL 1

Assisted Solution

by:hal3000
hal3000 earned 250 total points
ID: 10798372
Hi tarababu,
Also note that the same error occurs in the if statement in the checkCombination function.

Cheers!
0
 

Author Comment

by:tarababu
ID: 10798418
holy smokes!! Didnt even notice that..small thing....

Man, looks like i need some rest :)

Thank You very much dudez.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now