Reputation: 119
I'm a beginner in C and have an assignment to build a program with various functions that can be used in an implementation of a Yatzy game.
It's still a work in progress, but I've gotten stuck, it all works when I run it until I try to choose to run either throwDice() or readDieValues() after I've run either of them before. If I choose to call the printMenu() function several times it works. I guess the problem is with the for-loops, but I can't quite figure out where it goes wrong... Help?
#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");
}
Upvotes: 2
Views: 1303
Reputation: 1822
Most of people pointed to the syntax where you called a function wrongly. In my answer i am just clearing why your call is wrong the reason for passing dice instead of *dice is that *dice refer to data stored at the address pointed by dice. but in prototype you declare first argument as pointer which force you to pass "address" to call this function. array is also pinter whose name is used as pointer to address of the start of the memory.
Upvotes: 0
Reputation: 49200
Replace *dice
with dice
in switch
case as we want to pass the address of dice not the value
case 1:
throwDice(dice, nrOfDice, nrOfDieValues); //*dice
break;
case 2:
readDieValues(dice, nrOfDice); //*dice
int dice[]
is an array so, *dice
will have the 1st element of array but dice
will hold the (starting) address of array.
In c we pass only the address of array to function like void throwDice(int dice[])
to prevent copying of all the elements of array to stack.
Upvotes: 1
Reputation: 1220
When calling the throwDice() and readDieValues() in the switch, you pass *dice, which is the first int in the dice array, when those functions need an array of int.
To pass the whole array, just use its name without any asterisk:
throwDice(dice, nrOfDice, nrOfDieValues);
Upvotes: 0
Reputation: 505
You're using your function like this
case 1:
throwDice(*dice, nrOfDice, nrOfDieValues);
break;
case 2:
readDieValues(*dice, nrOfDice);
break;
But dice
is an array int dice[nrOfDice];
. Putting *dice
in the function call is like putting dice[0]
.
case 1:
throwDice(dice, nrOfDice, nrOfDieValues);
break;
case 2:
readDieValues(dice, nrOfDice);
break;
You need to remove the *
before dice
.
Upvotes: 4
Reputation: 7
throwDice(*dice, nrOfDice, nrOfDieValues); needs to be changed to
throwDice(dice, nrOfDice, nrOfDieValues);
same for
readDieValues(*dice, nrOfDice); which needs to be changed to
readDieValues(dice, nrOfDice);
Upvotes: 0
Reputation: 2972
The function calls are wrong. You should write:
throwDice(dice, nrOfDice, nrOfDieValues);
and
readDieValues(dice, nrOfDice);
Did you not get a compiler warning?
Upvotes: 0