Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can someone please tell me why this keeps failing?

+4 votes
asked Apr 2 by DisgruntledLackey (160 points)
Roulette Game

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

#define minv 0
#define maxv 36

void showInstructions();
void makeBet(float *betType, float *selection);
void getBetAmount(float *betAmount);
void spinWheel(int *number);
float figureWinnings(int number, int selection, int betType, float betAmount);

int main()
{
    // all the variables
    int number;
    float betType, selection, getBA, spinW, betAmount, n, fW;
    // show the instructions
    showInstructions();
    //address of betType and selection is passed
    makeBet(&betType, &selection);
    // address of betAmount is passed
    getBetAmount(&betAmount);
    //address of number is passed is passed
    spinWheel(&number);
    // winnings is calculated
    fW += figureWinnings(number, selection, betType, betAmount);
    return 0;
}
// prints the instructions
void showInstructions()
{
    printf("***The Roulette Game***");
    printf("\n\n Instructions: ");
    printf("\n\n The roulette wheel has the numbers 0-36 on it.\n You can place your bet in one of three ways: ");
    printf("\n\t 1. Bet on a number (payout is 36 times the bet amount)");
    printf("\n\t 2. Bet on odd or even (payout is 2 times the bet)");
    printf("\n\t 3. Bet on a dozen ~ first 1-12, second 13-24, third 25-36 (payout is 3 times the bet)");
    printf("\n The number zero does not count for odd or even or dozen, but can count as a number bet.");
}

// (*betType != 1 || *betType != 2 || *betType !=3) says that if any of the bet is in 1 2 or 3 pass else the value entered is incorrect
void makeBet(float *betType, float *selection)
{
    printf("\n\n What type of bet would you like to make?\n\t1-number\n\t2-even/odd\n\t3-dozen\n\t");
    scanf("\n%f",betType);
    while ((*betType != 1 || *betType != 2 || *betType !=3))
    {
        printf("\nIncorrect. Please try again!\n\t");
        makeBet(betType,selection);
    }
    if (*betType == 1)
    {
        printf("Which number in the range 0-36 are you betting on?\n\t");
        scanf("\n%f",selection);
        while (*selection < minv || *selection > maxv)
        {
            printf("Invalid! Try again. Choose a number in the range 0-36\n");
            scanf("\n%f",selection);
        }

    }
    else if (*betType == 2)
    {
        printf("Odd or even? \n\t1-Odd \n\t2-Even\n\t");
        scanf("\n%f", selection);
        while (*selection != 1 && *selection != 2)
        {
            printf("Try again!\n");
            scanf("\n%f",selection);
        }
    }
    else if (*betType == 3)
    {
        printf("Which dozen are you betting on? \n\t1 (1-12) \n\t2 (13-24) \n\t3 (25-36)\n\t");
        scanf("\n%f",selection);
        while (*selection != 1 && *selection != 2 && *selection != 3)
        {
            printf("Try again!");
            scanf("\n%f",selection);
        }
    }

}
// gets the amount you want to bet
void getBetAmount(float *betAmount)
{
    printf("\nHow much $ would you like to bet?\n\t");
    scanf(" %f",betAmount);
}
// get a random int number in the range 0-36 here you need to use *number to take in the value
void spinWheel(int *number)
{

        srand(time(0)); // ensures different random number is generated every time program is run

        int lower=0, higher=36;
        *number= ((rand()%(higher - lower + 1))+lower );

        printf("\nThe number is %d", *number);
}
// calculate the winnings here you need to use winnings =-betAmount if lost
float figureWinnings(int number, int selection, int betType, float betAmount)
{
    float winnings=0;
    if (betType == 1)
    {
        if (selection == number) winnings = betAmount * 36;
        else
        winnings = -betAmount;
    }
    else if (betType == 2)
    {
        if (selection == 1)
        {
            if (number % 2 == 1)
            winnings = betAmount * 2;
            else
            winnings = -betAmount;
        }
        else if (selection == 2)
        {
            if (number % 2 == 0)
            winnings = betAmount * 2;
            else
            winnings = -betAmount;
        }

    }
    else if (betType == 3)
    {
        // calculate wins and losses
        if (selection == 1)
        {
            if (number >= 1 && number <= 12)winnings = betAmount * 3;
            else
            winnings = -betAmount;
        }
        else if (selection == 2)
        {
        if (number >= 13 && number <= 24)winnings = betAmount * 3;
        else
            winnings = -betAmount;
        }
        else if (selection == 3)
        {
            if (number >= 25 && number <= 36)winnings = betAmount * 3;
            else
            winnings = -betAmount;
        }
    }

    printf("\nThe winnings are %f", winnings);
    return winnings;

}

1 Answer

0 votes
answered Apr 8 by Peter Minarik (55,660 points)
while ((*betType != 1 || *betType != 2 || *betType !=3))
{
    printf("\nIncorrect. Please try again!\n\t");
    makeBet(betType,selection);
}

This expression will always be true. You wrote: if betType is not 1 or not 2 or not 3. Since betType can take only one value at a time, at least 2 of the 3 parts will always be true (even if it is 1, it is definitely not 2 and definitely not 3 at the same time).

Also, it is super weird that you store a choice in a float. Why not in an integral number?

Last, but not least, in the loop you keep calling the makeBet function over and over in case an invalid input was provided. This is also quite unusual.You could simplify the code to be similar like what you do with the selection. I'd do it like this:

int MakeAChoice(const char * message, int minChoice, int maxChoice)
{
    int choice;
    do
    {
        puts(message);
        scanf("%d", &choice);
    } while (choice < minChoice || choice > maxChoice);
    return choice;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...