将数组元素复制到新元素 - C.

时间:2014-10-08 01:36:40

标签: c arrays arraycopy

我目前正在完成学校任务,现在我真的被困了。我遇到的问题是,当我试图将数组骰子的元素复制到数组diceCheck时,程序会进入某种无限循环,我不明白为什么。非常感谢帮助! 编辑:它位于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 个答案:

答案 0 :(得分:1)

你有:

const int nrOfDice = 5;

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

你的复制想法是正确的,但你要经过阵列末尾。

为了避免这种错误,请从同一个表达式初始化,例如:

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

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

并在PrintScores函数内部,而不是int diceCheck[4];,执行:

int diceCheck[nrOfDice];