为什么我的C程序第二次调用函数时会崩溃?

时间:2015-09-25 10:00:52

标签: c for-loop

我是C的初学者,并且有一个任务来构建一个具有各种功能的程序,可以在Yatzy游戏的实现中使用。

它仍然在进行中,但是我已经卡住了,当我运行它直到我尝试选择在运行之后选择运行throwDice()或readDieValues()时,它都能正常工作。之前他们中的任何一个。如果我选择多次调用printMenu()函数,它就可以工作。我想问题是for-loops,但我无法弄清楚它出错的地方......帮忙?

#include <stdio.h>
#include <stdlib.h> // rand(), srand()
#include <time.h>    // time()

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 main(void){
    //Constants-Yatzy always has 5 die with no more and no less than values 1-6
    const int nrOfDice = 5;
    const int nrOfDieValues = 6;
    int dice[nrOfDice];             //Holds the values for the die
    int choice;                     //Holds the choice that the user inputs

    //Prints the menu to the user at the beginning of the program
    //Asks the user for a number between 1 and 4. (-1 terminates the program)
    printMenu();
    printf("\nMake your choice: ");
    scanf(" %d", &choice );

    //As long as the user doesn't want to terminate the program it 
    //excecutes the request and asks for a new number

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

}

/* Function:    throwDice
 * Description: Gives the 5 die random numbers between 1 and 6.
 * Input:       Optional to give a seed to the random-function.
 * Output:      None.
 */
void throwDice(int dice[], int nrOfDice, int nrOfDieValues){
    int i;
    int seed;
    printf("Enter seed (1 gives a random seed): ");
    scanf("%d", &seed);
    // seed 1 sets the seed at "random", (uses the current time for seed)
    //any other number will be used as a direct seed 
    if (seed==1){
        srand(time(NULL));
    }else{
        srand(seed);
    }
    //Sets random values to each die
    for (i=0; i<nrOfDice; i++){
        dice[i]= (rand()%6)+1;
        printf(" %d\n", dice[i]);
    }

}

/* Function:    readDieValues
 * Description: Manually inputs values to 5 different die.
 * Input:       5 positive integers between 1 and 6.
 * Output:      None.
 */

void readDieValues(int dice[], int nrOfDice){
    int i;
    //Sets values to each die from the user input
    for(i=0; i<nrOfDice; i++){
        printf("Die: ");
        scanf(" %d", &dice[i]);
    }
}

/* Function:    printMenu
 * Description: Prints out the menu to the user.
 * Input:       None.
 * Output:      A menu with different number choices.
 */
void printMenu(void){
    printf("MENU: \n0.  Display the menu \n1.  Make a random throw \n"
           "2.  Enter die values for a throw \n3.  Display the die values for the throw \n"
           "4.  Display the score for the throw \n-1. End program \n");

    }

6 个答案:

答案 0 :(得分:4)

您正在使用此功能

case 1:
      throwDice(*dice, nrOfDice, nrOfDieValues);
      break;
case 2:
      readDieValues(*dice, nrOfDice);
      break;

但是dice是一个数组int dice[nrOfDice];。将*dice置于函数调用中就像放dice[0]

case 1:
      throwDice(dice, nrOfDice, nrOfDieValues);
      break;
case 2:
      readDieValues(dice, nrOfDice);
      break;

您需要在*之前移除dice

答案 1 :(得分:1)

*dice案例中将dice替换为switch,因为我们要传递的是骰子的地址而不是值

        case 1:
            throwDice(dice, nrOfDice, nrOfDieValues); //*dice
            break;
        case 2:
            readDieValues(dice, nrOfDice); //*dice

int dice[]是一个数组,*dice将包含数组的第一个元素,但dice将保存数组的(起始)地址。
中,我们只将数组的地址传递给像void throwDice(int dice[])这样的函数,以防止将数组的所有元素复制到堆栈。

答案 2 :(得分:0)

函数调用错误。你应该写:

throwDice(dice, nrOfDice, nrOfDieValues);

readDieValues(dice, nrOfDice);

您没有收到编译器警告吗?

答案 3 :(得分:0)

throwDice(* dice,nrOfDice,nrOfDieValues);需要改为

throwDice(dice,nrOfDice,nrOfDieValues);

同样适用于

readDieValues(* dice,nrOfDice);需要更改为

readDieValues(dice,nrOfDice);

答案 4 :(得分:0)

当在switch中调用throwDice()和readDieValues()时,你传递* dice,这是dice数组中的第一个int,当这些函数需要一个int数组时。

要传递整个数组,只需使用不带任何星号的名称:

throwDice(dice, nrOfDice, nrOfDieValues);

答案 5 :(得分:0)

大多数人都指出了你错误调用函数的语法。在我的回答中,我只是清除你的电话错误的原因 传递骰子而不是*骰子的原因是*骰子是指存储在骰子指向的地址的数据。但是在原型中你将第一个参数声明为指针,强制你传递“地址”来调用这个函数。 array也是pinter,其名称用作指向内存开始地址的指针。