Reputation: 1
I have this yahtzee program I am writing as a project. I was required to define all the functions and macros used. However, I don't know how to properly take input from the displayGameMenu function and use it to select a case from the switch statement. Instead of ever displaying the game menu, it infinitely loops the default case.
I tried calling the displayGameMenu before the case was reached, it still spits out the default case. I tried making an int variable equal to displayGameMenu, and then passing that into the switch() statement. Sorry in advance I am very new to coding.
The code is :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//declare global variables/macros
#define RULES 1
#define GAME 2
#define EXIT 3
#define ROLLS 3
// function prototypes
void gameRules();
void clearScreen();
int displayGameMenu();
void displayRandomDice();
int rollDie();
// main function
int main()
{
//declare int
int play = 1;
//initialize srand with time as seed
srand(time(0));
int input = displayGameMenu();
//initialize while loop controlled by play
while(play = 1) {
//list of cases to control function calls
switch(input)
{
case RULES :
gameRules();
break;
case GAME :
clearScreen();
displayRandomDice();
break;
case EXIT :
printf("Thank you for playing!");
play = 0;
break;
default :
printf("Incorrect option, hit enter and try again");
char enter;
scanf("%c", &enter);
}
}
// program executed successfully
return 0;
}
// gameRules function displays the Yahtzee and rules of the game
void gameRules ()
{
printf ("\t\t\t\tLET'S PLAY YAHTZEE!!! \n\n");
printf ("RULES OF THE GAME:\n");
printf ("\t1. The scorecard used for Yahtzee is composed of an upper section and a lower section.\n");
printf ("\t2. A total of 13 scoring combinations are divided amongst the sections. \n");
printf ("\t3. The upper section consists of boxes that are scored by summing the value of the dice matching the faces of the box.\n");
printf ("\t4. If a player rolls four 3's, then the score placed in the 3's box is the sum of the dice which is 12. \n");
printf ("\t5. Once a player has chosen to score a box, it may not be changed and the combination is no longer in play for future rounds.\n");
printf ("\t6. If the sum of the scores in the upper section is greater than or equal to 63, then 35 more points are added \n");
printf ("\tto the players overall score as a bonus. The lower section contains a number of poker like combinations.\n");
}
//clear screen
void clearScreen()
{
printf("\n\t\t\t\tHit <ENTER> to continue!\n");
char enter;
scanf("%c", &enter);
// send the clear screen command Windows
system("cls");
// send the clear screen command for UNIX flavor operating systems
// system("clear");
}
//display random dice function
void displayRandomDice()
{
//declare all 6 int type variables
int numRolls;
int die1;
int die2;
int die3;
int die4;
int die5;
//for loop incrementing by 1, until ROLLS
for( numRolls = 0; numRolls < ROLLS; ++numRolls )
{
//insert randomized numbers from rollDie into dice 1-5
die1 = rollDie();
die2 = rollDie();
die3 = rollDie();
die4 = rollDie();
die5 = rollDie();
//printf output randomized dice into nice looking table
printf("+-------+ +-------+ ------------------------|\n");
printf("| | | | | | |\n");
printf("| %d | | %d | %d | %d | %d |\n", die1, die2, die3, die4, die5);
printf("| | | | | | |\n");
printf("+-------+ +-------+ ------------------------|\n");
}
}
int displayGameMenu()
{
//declare int select
int select = 0;
//while loop
while(select = 0)
{
//printf displays options
printf("%d. Display Game Rules\n", RULES);
printf("%d. Start a game of Yahtzee\n", GAME);
printf("%d. Exit\n", EXIT);
//scanf get user input, store in select
scanf("%d", &select );
//return select
return select;
}
}
int rollDie()
{
//declare int dieValue
int dieValue = 0;
//sets dieValue equal to rand() with scaling factor 6 and shift factor 1
dieValue = rand() % 6 + 1;
//return dieValue
return dieValue;
}
Upvotes: 0
Views: 89
Reputation: 1153
Let's understand the problem,
In this block:-
int input = displayGameMenu();
//initialize while loop controlled by play
while(play == 1) {
//list of cases to control function calls
switch(input)
{
case RULES :
gameRules();
break;
case GAME :
clearScreen();
displayRandomDice();
break;
case EXIT :
printf("Thank you for playing!");
play = 0;
break;
default :
printf("Incorrect option, hit enter and try again");
char enter;
scanf("%c", &enter);
}
}
You are calling displayGameMenu();
once and assigning its returned value to input
. Let's assume it is 1. Now here while ( play == 1)
, so to exit this loop, you need to change it's value.
Then in next line, you've this switch(input)
statement, based on our choice 1
, case RULES
will be executed, and then it exits switch
statement. Note value of play isn't updated right now, so play
& input
is still 1
. That's why it executes the statement forever.
Solution:
//initialize while loop controlled by play
while(play) {
int input = displayGameMenu();
//list of cases to control function calls
switch(input)
{
case RULES :
gameRules();
break;
case GAME :
clearScreen();
displayRandomDice();
break;
case EXIT :
printf("Thank you for playing!\n");
play = 0;
break;
default :
printf("Incorrect option, Press any key to continue...\n");
getchar();
}
putchar('\n');
}
You have to update the value of input
by invoking displayGameMenu()
function each time, inside while
loop.
Update: This question is edited and then rolled back to its original form by @Gerhardh. So, to avoid any ambiguity please check @Gerhardh's comment under OP's question.
And now for the updated version of this question, there's another logical error in the program which is while(play = 1)
inside main()
and while(select = 0)
inside displayGameMenu()
,
which should be corrected with applying Equality(==)
operator instead of Assignment(=)
operator. If it's not corrected the loop condition will always be true
. Thus execution of the loop will be infinite
.
Upvotes: 1
Reputation: 4925
Only focusing on the issue you're asking about I see two problems.
1) int input = displayGameMenu();
should be inside the loop if you want the choices to be presented more than once.
2) while(select = 0)
assigns 0 to select
and evaluates to false, so the loop contents are skipped. Since there's no return outside the loop you have undefined behavior which means the program could crash or any value could be returned. Since you corrected that in an edit I would have expected your program to work properly the first time.
Here is a stripped down version of your program that behaves correctly for me.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define RULES 1
#define GAME 2
#define EXIT 3
int displayGameMenu();
int main()
{
int play = 1;
while (play)
{
int input = displayGameMenu();
switch (input)
{
case RULES:
printf("RULES chosen\n");
break;
case GAME:
printf("GAME chosen\n");
break;
case EXIT:
printf("EXIT chosen\n");
play = 0;
break;
default:
printf("DEFAULT\n");
break;
}
}
return 0;
}
int displayGameMenu()
{
int select = 0;
while (select == 0)
{
printf("%d. Display Game Rules\n", RULES);
printf("%d. Start a game of Yahtzee\n", GAME);
printf("%d. Exit\n", EXIT);
scanf("%d", &select);
return select;
}
return 0;
}
I tested it here: https://ideone.com/yv2WZm and the behavior is as expected.
Upvotes: 0