Thread: Please! Need help with slot machine code

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    Please! Need help with slot machine code

    Code:
    #include <stdio.h>#include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    //Function declarations
    void get_startAmt (int* startAmt);
    int get_betAmt (int betAmt, int startAmt);
    void randInt(int* winType, int* ran1, int* ran2, int* ran3);
    void calculate(int* winAmt, int* newAmt, int startAmt, int betAmt, int winType);
    void winLose (int winType);
    void slotMachine (int newAmt, int startAmt, int betAmt,
                      int winAmt, int ran1, int ran2, int ran3,
                      int winType);
    
    
    int main ()
    {
        //Variables
        int startAmt, betAmt, newAmt, ran1, ran2, ran3, winType, winAmt, tempBet;
    
    
        get_startAmt(&startAmt);
    
    
        calculate(&winAmt, &newAmt, startAmt, betAmt, winType);
    
    
        //Slot Machine
        slotMachine(newAmt, startAmt, betAmt, winAmt,
                        ran1, ran2, ran3, winType);
    
    
        return 0;
    }//Main
    
    
    void get_startAmt (int* startAmt)
    {
        printf("\n");
        printf("COP 2220 Project 3 - Patrick Glenn");
        printf("\n\n");
        printf("Let's play Crazy Eights!");
        printf("\n\n");
        printf("How many dollars do you want to start with (0 to quit)? ");
        scanf("%d", startAmt);
        do{
            if(* startAmt == 0)
            {
                printf("Fear is the path to the dark side.");
                printf("\n\n");
                exit (1);
            }//if
            if(*startAmt < 1)
            {
                printf("ERROR: Enter at least one dollar or zero if you don't want to play.");
                printf("\n\n");
            }
        }while(*startAmt < 1);
    
    
        return;
    }//get_startAmt
    
    
    int get_betAmt (int betAmt, int startAmt)
    {
        do{
            printf("How many dollars do you want to bet (enter 0 to cash out)? ");
            scanf("%d", &betAmt);
            if(betAmt == 0)
            {
                printf("Mind what you have learned. Save you it can.");
                printf("\n\n");
                exit(2);
            }
            if(betAmt > startAmt)
            {
                printf("Bet not which you do not have.");
                printf("\n\n");
            }
        }while(betAmt > startAmt);
    
    
        return betAmt;
    }//get_betAmt
    
    
    void randInt(int* winType, int* ran1, int* ran2, int* ran3)
    {
        srand(time(NULL));
        *ran1 = rand() % 8 + 1;
        *ran2 = rand() % 8 + 1;
        *ran3 = rand() % 8 + 1;
    
    
        if(*ran1 == 8 && *ran2 == 8 && *ran3 == 8)
        {
            *winType = 1;
        }
        else if(*ran1 == *ran2 && *ran2 == *ran3)
        {
            *winType = 2;
        }
        else if(*ran1 == *ran2 || *ran2 == *ran3 || *ran1 == *ran3)
        {
            *winType = 3;
        }
        else if(*ran1 == 8 || *ran2 == 8 || *ran3 == 8)
        {
            *winType = 4;
        }
        else
        {
            *winType = 5;
        }
    
    
        return;
    }//randInt
    
    
    void calculate(int* winAmt, int* newAmt, int startAmt, int betAmt, int winType)
    {
        switch(winType)
        {
        case 1:
            *winAmt = betAmt * 80;
            break;
        case 2:
            *winAmt = betAmt * 25;
            break;
        case 3:
            *winAmt = betAmt * 2;
            break;
        case 4:
            *winAmt = betAmt;
            break;
        case 5:
            *winAmt = 0;
            break;
        default:
            break;
            }
    
    
        return;
    }
    
    
    void winLose (int winType)
    {
    
    
        switch(winType)
        {
        case 1:
            printf("JACKPOT!");
            break;
        case 2:
            printf("Match Three!");
            break;
        case 3:
            printf(" Match Two!");
            break;
        case 4:
            printf("Crazy Eight!");
            break;
        }
    
    
        return;
    }//winLose
    
    
    void slotMachine (int newAmt, int startAmt, int betAmt,
                      int winAmt, int ran1, int ran2, int ran3,
                      int winType)
    {
        while(get_betAmt(betAmt, startAmt))
        {
            randInt(&winType, &ran1, &ran2, &ran3);
    
    
            printf("       _____       \n");
            printf(" _____/ 888 \\_____\n");
            printf("/  Crazy Eights   \\ ");
            printf("\n");
            printf("|=================|  __\n");
            printf("| 888     80x Bet | (  )\n");
            printf("| Match 3 25x Bet |  ||\n");
            printf("| Match 2  2x Bet |  ||\n");
            printf("| Eight    1x Bet |  ||\n");
            printf("|-----+-----+-----|  ||\n");
            printf("| %2d  | %2d  | %2d  |  ||\n", ran1, ran2, ran3);
            printf("|-----+-----+-----|  ||\n");
    
    
            if(winType == 1 || winType == 2 || winType == 3 || winType == 4)
                printf("|WIN!  WIN!  WIN! |__||\n");
            if(winType == 5)
            {
                printf("|      SORRY      |__||\n");
                printf("|    Try Again    |---'\n");
            }
            if(winType >= 1 && winType <= 4)
                printf("|   ");
    
    
            winLose(winType);
    
    
            if(winType >= 1 && winType <= 3)
                printf("   |---'\n");
            if(winType == 4)
                printf("  |---'\n");
    
    
            printf("|     _______     |\n");
            printf("|____/ o o   \\____|\n");
            printf("\\     o oo o      /\n");
            printf(" \\_______________/\n");
            printf("  U             U");
            printf("\n\n");
    
    
            winLose(winType);
    
    
            if(winType == 1)
                printf(" Good very good, you win $%d\n\n", winAmt);
            if(winType >= 2 && winType <= 4)
                printf(" You win $%d\n\n", winAmt);
            if(winType == 5)
                printf("Sorry, no winning combination.\n\n");
    
    
            printf("Your current total is $%d\n", newAmt);
    
    
            betAmt = 0;
        }
    
    
        return;
    }//slotMachine
    Sorry no comments in the code yet. I am stuck. The program runs and does almost everything. The problem I am running into is winAmt returns the wrong number (4 199 040) and newAmt returns the same (4 199 040) in slotMachine function. I thought I understood loops but I get the feeling I am not declaring something properly and I am losing betAmt and/or startAmt.

    Thank you in advance.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You're never assigning new values (or any values in fact; they're not initialised) to winAmt or newAmt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    <vader>
    I sense a disturbance in the source - a feeling that I've seen this code before...
    Need help with slot machine code
    <rasping breath>
    </vader>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    <vader>
    I sense a disturbance in the source - a feeling that I've seen this code before...
    Need help with slot machine code
    <rasping breath>
    </vader>
    Why are you wearing that ridiculous respirator, Salem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I get this slot machine code to run?
    By Nick Petraglia in forum C Programming
    Replies: 15
    Last Post: 11-21-2015, 10:27 AM
  2. Need help with slot machine code
    By p0l4r21 in forum C Programming
    Replies: 4
    Last Post: 10-31-2015, 01:55 AM
  3. Slot Machine Problem
    By DecoratorFawn82 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2013, 07:11 PM
  4. Slot Machine
    By nomi in forum C Programming
    Replies: 9
    Last Post: 01-03-2004, 10:13 AM
  5. IDEA: A Slot Machine (aka a fruit machine)
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:13 PM