1

I'm currently doing a school assignment and now I'm really stuck. The problem I have is that When I'm trying to copy the elements of the array dice to the array diceCheck the program goes in to some kind of infinite loop and I don't understand why. Help would be greatly appreciated! Edit: It's in the bottom in the function printScores.

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

void printMenu(void);
void throwDice(int dice[], int nrOfDice, int nrOfDieValues);
void readDieValues (int dice[], int nrOfDice);
void printDice(const int dice[], int nrOfDice);
void printScores(const int dice[], int nrOfdice, int nrOfDieValues);
int isThreeOfAKind(const int dieValues[], int nrOfDieValues);
int isSmallStraight(const int dieValues[], int nrOfDieValues);

int main(void)
{
    const int nrOfDice = 5;
    const int nrOfDieValues = 6;
    int dice[4], menuChoice = 0;

    printMenu();
    printf("\nMake your choice: ");
    while(scanf("%d", &menuChoice) != -1)
    {
        switch (menuChoice)
        {
            case 0:
                printMenu();
            break;
            case 1:
                throwDice(dice, nrOfDice, nrOfDieValues);
                printf("Make your choice: ");
            break;
            case 2:
                readDieValues(dice, nrOfDice);
                printf("Make your choice: ");
            break;
            case 3:
                printDice(dice, nrOfDice);
                printf("Make your choice: ");
            break;
            case 4:
                printScores(dice, nrOfDice, nrOfDieValues);
            break;
            case -1:
                return 0;
            break;
            default:
                printf("Invalid choice!\n");
            break;
        }
    }
    return 0;
}

void printMenu()
{
    printf("MENU:\n");
    printf("0.  Display the menu\n");
    printf("1.  Make a random throw\n");
    printf("2.  Enter die values for a throw\n");
    printf("3.  Display the die values for the throw\n");
    printf("4.  Display the score for the throw\n");
    printf("-1. End program\n");
}

void throwDice(int dice[], int nrOfDice, int nrOfDieValues)
{
    int choice, i;
    printf("Enter seed (1 gives a random seed): ");
    scanf("%d", &choice);
    if(choice == 1)
    {
        srand(time(NULL));
        for (i = 0; i < nrOfDice; i++)
        {
            dice[i] = ( rand() % nrOfDieValues) + 1;
        }
        printf("\n");
    }
    else
    {
        srand(choice);
        for(i = 0; i < nrOfDice; i++)
        {
            dice[i] = ( rand() % nrOfDieValues) + 1;
        }
        printf("\n");
    }
}

void readDieValues(int dice[], int nrOfDice)
{
    int i;
    for(i = 0; i < nrOfDice; i++)
    {
        printf("Die %d: ", (i+1));
        scanf("%d", &dice[i]);
    }
}

void printDice(const int dice[], int nrOfDice)
{
    int i;
    printf("Your dice: ");
    for(i = 0; i < nrOfDice; i++)
    {
        printf("%d ", dice[i]);
    }
    printf("\n\n");
}

int isThreeOfAKind(const int dieValues[], int nrOfDieValues)
{
}

int isSmallStraight(const int dieValues[], int nrOfDieValues)
{
}

void printScores(const int dice[], int nrOfdice, int nrOfDieValues)
{
    int diceCheck[4], i;

    for(i = 0; i < nrOfdice; i++)
    {
        diceCheck[i] = dice[i];

        printf("%d ", dice[i]); //these are just for myself to check if it worked
        printf("%d ", diceCheck[i]); //these are just for myself to check if it worked
    }
}
1
  • Generally when pasting code here, you should only paste the least amount of code required to illustrate your problem. Also, please try reduce the amount of unnecessary blank lines. Commented Oct 8, 2014 at 2:56

1 Answer 1

1

You have:

const int nrOfDice = 5;

but

int dice[4];
int diceCheck[4];

Your copying idea is correct but you are going one past the end of the array.

To avoid this sort of error, initialize both from the same expression, e.g.:

int dice[4];
const int nrOfDice = sizeof dice / sizeof dice[0];

or

const int nrOfDice = 4;
int dice[nrOfDice];

and inside the PrintScores function, instead of int diceCheck[4];, do:

int diceCheck[nrOfDice];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, that did solve it! :) Just out of curiosity, would e.g. int dice[5] and diceCheck[5] have worked? Even though if I were to write it like that the nrOfDice would have no use but still, so that I can understand a bit easier. I should have realized earlier to use the nrOfDice instead of "numbers", but now I know. Thanks man :)
Yes that would work, but it is error-prone to have your progrma rely on the same number appearing in two different places.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.