0

Just started learning functions and have a good grasp on them thanks to this thread I posted Passing variable through switch statement with functions.

Creating a dice game doing this I am having trouble with, however. It seems like it should be easier than the previous question I asked, but it is not. I am having trouble passing the three random dice through the functions. Also, as expected, my if statement at the end is not working, but I have no clue why. Here is my current as stands. Sorry in advance for my obnoxious menu name

    #include<stdlib.h>
    #include<stdio.h>
    #include <time.h>
    #include <ctype.h>

    #define MAXROLLS 5
    #define LOWERBOUND 1
    #define UPPERBOUND 6
    #define PAUSE system("pause")

    int diceOne, diceTwo, diceThree;
    int currentDiceSum=0, totalDiceSum=0;
    int quit= 0;
    int count = 0;





    char menuChoice ()
    {

    char choice;

    printf("\n\n==============================================================================\n");
    printf("\n\n==  W E L C O M E     T O     M Y     D I  C E     R O L L I N G     G A M E==\n");
    printf("\n\n==============================================================================\n");
    printf("\n Roll the dice, but you only get 5 rolls! You can't play forever, you know. \n");

    printf("Main Menu\n");
    printf("A.Roll the Dice\n");
    printf("B.Display the Result of Last Roll\n");
    printf("C.Quit\n");


    printf("Enter your choice:   ");
    scanf(" %c", &choice);
    choice = toupper(choice);

}


int rollTheDice() 
{
    int diceOne = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    int diceTwo = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    int diceThree = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    srand((unsigned)time(NULL));



    return diceOne;
    return diceTwo;
    return diceThree;



}


int getDiceRoll()
{

    currentDiceSum = diceOne + diceTwo + diceThree;
    totalDiceSum+= currentDiceSum;
}


int quitTotal()
{

   totalDiceSum+= currentDiceSum;


}

int main()
{


     while(quit!=1) //begin menu loop
    {
        char menu;



        menu = menuChoice();
        switch(menu)
        {
            case 'A':
            {
                 rollTheDice();
                 printf("Dice are rolled!\n");
                 count++;
                 printf("You have %i rolls left.\n", MAXROLLS - count);
                 break;
            }


            case 'B':

               getDiceRoll();  
               printf("Dice 1: %d\n", diceOne);

               printf("Dice 2: %d\n", diceTwo);

               printf("Dice 2: %d\n", diceThree);

               printf("Dice Total: %d\n", currentDiceSum);

            break;
            case 'C':
              quitTotal();
               printf("Number of rolls: %d\n", count);

               printf("Total of all dice for all rolls: %d\n", totalDiceSum);

               printf("Goodbye, hope to see you again!!!\n");
               PAUSE;
               quit = 1;
                break;
            default:
                printf("Please enter A,B,C\n");
                break;
        } //end switch
    } // end loop


if (count == MAXROLLS)

{

    printf("Sorry, your rolls are up!!!\n");


    printf("Your final roll was:\n");

    printf("Dice 1: %d\n", diceOne);

    printf("Dice 2: %d\n", diceTwo);

    printf("Dice 3: %d\n", diceThree);

    currentDiceSum = diceOne + diceTwo + diceThree;

    printf("Your final dice sum was\n");
    printf("Dice Total: %d\n", currentDiceSum);

    totalDiceSum+= currentDiceSum;



    printf("Number of rolls: %d\n", count);
    printf("Total of all dice for all rolls: %d\n", totalDiceSum);
    printf("Goodbye, hope to see you again!!!\n");



}

} //end function

As of now I am at a lost. I believe that I can only return one result per function. So perhaps I need to create three separate functions for each dice?

Community
  • 1
  • 1
Session
  • 33
  • 1
  • 5
  • 2
    You don't need to "return" them, just don't *declare* them in that function - they are already global. (Also, look up some documentation for `srand`. As it is, it does not much useful.) – Jongware Jul 12 '15 at 23:28
  • Have you learned about arrays yet? If so, consider the merits of `int dice[3];` as your variable. – Jonathan Leffler Jul 13 '15 at 03:06

2 Answers2

0

Change your function as follows:

typedef struct{
  int diceOne;
  int diceTwo;
  int diceThree;
}DiceRolls;

DiceRolls rollTheDice() 
{
  DiceRolls dice;

  dice.diceOne = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
  dice.diceTwo = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
  dice.diceThree = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
  return dice;
}

Per comments below, it is advisable that the call to srand() is done only once during init.

0

In C, functions can only return one value. Because the return for diceOne comes first, diceOne is returned from rollTheDice(). If you just want it to work and don't want to bother with structs or anything, I would remove the 'int's before your variable declarations to assign values to the global variables instead of creating new local vaiables, which results in rollTheDice() looking like this:

int rollTheDice() 
{
    diceOne = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    diceTwo = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    diceThree = LOWERBOUND + rand() % (UPPERBOUND - LOWERBOUND + 1);
    srand((unsigned)time(NULL));
}
Xenotoad
  • 362
  • 2
  • 12