Link to home
Start Free TrialLog in
Avatar of tarababu
tarababu

asked on

C-Programming Cont-II

Hey guyz,

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

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. HEre is my solution to the problem 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=0;
static int prizeval;
main()
{
   //clrscr();
      int change;
      srand(time (NULL));
      printf("\n");
      printf("\n*************************");
      printf("\n  WELCOME TO MY CASINO " );
      printf("\n*************************");
      printf("\n");
      printf("\n");
      printf("\n");
      do {
            do {
                  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 !=2 && change !=4 && change !=0);  //checks the validity of input from user
            if(change==0) {
                  printf("You spent $ %d and won $ %d", amount, prizeval);
                  getch();
                  return 0;

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


}
int generateNum(void) //generates random numbers from 1-3
{
      int num;
      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)
{
      prizeval =0;
      if((slot1==slot2) && (slot2==slot3))       //checks the equality of slot #s
            prizeval+= displayPrize(slot1,chhange);  // adds the prizeval everytime the if becomes True
      else
            printf("Not so lucky! You win $0 \n");
      amount += chhange;
      return ((slot1==slot2) && (slot2==slot3));  // returns true or false (1 or 0)
}


int displayPrize (int prize, int change)    //Displays the prize won
{
      if (change ==2)
            printf("\nCongratulations! You have won $50");
      else
            switch (s1) {
                  case 1:
                        printf("\n\nCongratulations! You have won $500");
                        return 500;
                        break;
                  case 2:
                        printf("\n\nCongratulations! You have won $250");
                        return 250;
                        break;
                  case 3:
                        printf("\n\nCongratulations! You have won $100");
                        return 100;
                        break;
            }
}

------------------------------------------------------------------------------------------------------------

I am having problems with:  "prizeval" in checkcombination function.

In main function, i would have to calculate the total amount of money spent, and the total amount of money won.

"prizeval" calculates the total amount of money won..

But, the problem is prizeval is displaying the correct value one time,  and other times it is displaying the wrong value, like -1, 34..

Sometimes, its going back and forth. Correct value one time and wrong value other times...

Some random number.., dont know where it is getting from..

I tried to fix it..by declaring it different places etc.. but, it doesnt seem to be working.,,

Can u guyz..help me out here...?

Thanks.


ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of course,u have to return 50; in the part where the user wins $50,as Kent Pointed out in his post.
Avatar of tarababu
tarababu

ASKER

oops! yeah, for global, i dont have to put static beside them.

ok. now it seems to be working.

Thanks!
Hi tarababu,


Actually, in this context 'static' isn't redundant -- it's irrelevent.  On global variables the 'static' qualifier reduces the scope of the variable to 'local' to the source file where it is defined.  The default behavior of global variables (and functions) is that they are also accessible externally.


Kent
Hi Kent,

I know that you are right about the static and global variable difference but i've been disillusioned by my compiler about this issue.

I know this may be due to my old compiler(TurboC 3.0):

/////////PROG 1//////////////
static int i=10;//static global variable.shouldnt be accessible in sec.c
int m1ain() {
    printf("%d\n",10);          
    printf("%d\n",11);          
    printf("%d\n",12);          
    return 0;
}
////////////////////////////////

/////////PROG 2//////////////
#include<stdio.h>
#include"fi.c"
#define SIZE 256
int main(int argc, char* argv[]) {
    char buf[SIZE];
    printf("%d",i);//using i from fi.c.This works even when i give static int i in fi.c
    while(fgets(buf,SIZE,stdin)) {
       printf("Read: %s\n",buf);
    }
    return 0;
}
////////////////////////////////

Hi ankuratvb,

'i' is accessable to all of the functions in the source file so if what you've posted is in a single file, both functions can reference 'i'.

If they are separate source files and prog1 is named "fi.c" so that it is "included" when prog2 is compiled, the 'i' can still be referenced by main().


Kent
They are separate files and prog1 is named "fi.c".

If the static variable is accessible across multiple files when they are included,how would i demonstrate that i has scope local to the source file in which it is defined.

The only other way to have multiple files is to have the concept of a project.Is the scope restriction valid there??

Sorry for sounding like a newbie and pestering you with my questions. :~(
i can reference 'i' in any function in prog2,not only main().
I have no idea why?
Hi ankuratvb,


/*  Program 1  */

static int i;




/*  Program 2  */


main ()
{
  i = 0;
}


If both of those programs are in the same source file, everything is fine as 'i' will be known to main().


/*  Program 1  */

static int i;




/*  Program 2  */

extern int i;

main ()
{
  i = 0;
}


Now in the example above, assume that the two programs are separate source files.  Program 1 will compile and produce an integer named 'i' in the globals block.

Program 2 will compile and will attempt to reference the integer variable 'i' that is defined in another program.

When the two programs are linked together, an error will be generated because 'i' will not have been found by program 2 because it is local to program 1.


Removing the 'static' qualifer so that the two files look like the following will work as intended:

/*  Program 1  */

int i;




/*  Program 2  */

extern int i;

main ()
{
  i = 0;
}


Kent
Hi kent,

What is the link b/w the 2 programs?

>When the two programs are linked together
No includes,no project??


ankuratvb,

Ahha...

When you compile and run a program, an awful lot of things happen other than the few lines that you actually write.  These other functions are part of the C library and are merged with your program to form an executable.

#include <stdlib.h>

extern int errno;


main ()
{
  printf (" errno=%d\n", error);
}


'errno' is a standard variable that the system uses.  Many of the system functions return an error number in 'errno'.  fopen(), read(), findfirst(), etc. all set errno to non-zero when an error occurs.  'errno' isn't part of your program, it's part of the C library just as are the functions that I just mentioned.

linking is the process of taking the object file that was created when you compiled you program and adding in all of the C library routines that you need.

#include <stdlib.h>

extern int errno;


main ()
{
  char Buffer[100];

  strcpy (Buffer, "Test");
  printf (" errno=%d\n", error);
}


Compile and run both of these programs.  When you compile them, turn on the option to generate a loadmap.  How you signal generating a loadmap is different between compilers, but if you're using an IDE the help file will tell you.  Look at the two loadmaps and see how much the two programs actually differ just because you called strcpy().


Kent
Hi kent,

As far as the C standard library is concerned,thats fine.

But when it comes to linking 2 user programs together,
In your example above,
/*  Program 1  */

int i;

/*  Program 2  */

extern int i;

main ()
{
 i = 0;
}

How will the compiler which 2 programs im trying to link together.Will it search in all source files in the current dir.
Also,what if there is a conflict i.e. more than 1 file has declaration for i as a global variable.

What compiler are you using?
You wouldnt like what i use but its Borland's TurboC++ 3.0
I would too like it.  :)  I'm a huge Borland fan!

There are several ways that you can compile two programs into a single executable.

Under the "Project" tab you'll note that you can enter a name for the project.  For simplicity, let's pretend that the project name is "Test2".  The project file name would be "Test2.prj".  In this file you simply name all of the files that will be used to build your program.  Put "Program1.c" and "Program2.c" in the file on separate lines (without the quotes).  Now when you compile your program, the compiler will use both of these files.

You can also do it the "hard way" with the command line compiler, TCC.  But let's try the easy way first.  :)

Kent
Ok.Finally got it to work.

Thanx to you Kent.