我是 C 语言的初学者,我的任务是构建一个具有各种功能的程序,该程序可用于实现 Yatzy 游戏。
这仍然是一项正在进行的工作,但我已经陷入困境,当我运行它时,一切都有效,直到我在之前运行过 throwDice() 或 readDieValues() 后尝试选择运行它们中的任何一个。如果我选择多次调用 printMenu() 函数,它就会起作用。我猜问题出在 for 循环上,但我不太清楚哪里出了问题......帮忙?
#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");
}
最佳答案
你正在像这样使用你的函数
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;
您需要删除骰子
之前的*
。
关于c - 为什么我的 C 程序在第二次调用函数时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779688/