Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining the outcomes from toss of dice

Status
Not open for further replies.

tmcneil

Technical User
Nov 17, 2000
294
US
All,

I have some code here that I need some help on. Some background information on this project is, two die are tossed (as you know, faces of each dice are numbered 1 through 6.) If sum of the faces landing up is 2, 3, 6, or 7 then Shooy-Mooy wins. If the sum of the faces that land up is 4, 5, 11, or 12 then the opponent wins. Otherwise the toss is a draw.

I'm on the last part of this assignment. What's left is that I have to determine the total number of tosses, Shooy-Mooy’s score, Opponent’s score, the number of tosses that resulted in a draw, and the overall winner. So far, I have written this code, but I am not sure where I should do the calculations. Any help would be greatly appreciated.

Code:
#include <iostream>
#include <cstdlib>  //access to rand() function

using namespace std;

#define DASHES "-----------------------------------------------------------"
#define SEPARATOR "--------------"
#define TOSS 20

int main (void)
{
    // prototypes
	void top_of_page_info (void);
    void welcome_header (void);
    int  toss_die (void);
    void toss_winner (int &dice1, int &dice2);
    void keep_output_window_open (void);

    //variable declarations
    int dice1, dice2;     // variables for storing random #'s for both die.
    int i;                // counter

    // functions
    top_of_page_info ();
    welcome_header ();

    //srand(time(NULL));   //To seed or not to seed, that is the question...
    
    // process 20 tosses of the dice
    // call functions, toss_die() and toss_winner()
    for (i = 1; i <= TOSS; i++) // process info on 20 tosses
    {
        dice1 = toss_die();
        dice2 = toss_die();
        toss_winner (dice1, dice2);
    }

    cout << endl << endl;
    cout << "Total Number of Tosses = " << i-1 << "." << endl << endl;
    cout << "Shooy-Mooy's Score     = " << "TBD" << "." << endl;
    cout << "Opponent" << "'s Score = " << "TBD" << "." << endl;
    cout << "Number of Draws        = " << "TBD" << "." << endl;

    // function
    keep_output_window_open ();

    return 0;
} // end of main program
//=========================end of main()=======================================

//*****************************************************************************
// This method/function prints student info on top of page.
//-----------------------------------------------------------------------------
//  Parameters Passed : none
//  Value(s) Returned : none
//  Local Variables   : none
//-----------------------------------------------------------------------------
// Constant Identifier     Type       Description
//    NAME                 string   String of characters
//*****************************************************************************

#define NAME "Todd McNeil"

void top_of_page_info (void)
{    
    cout << DASHES;
	cout << "\n\t\tProgrammer Name: " << NAME;
	cout << "\n\t\tSection: CSI 155/Section 875";
    cout << "\n\t\tLab/Project No.: Lab #3 Part B";
	cout << "\n\t\tDate   : 02/16/2006\n";
	cout << DASHES << endl << endl << endl;
}
//-------------------------end top_of_page_info--------------------------------

//*****************************************************************************
// This method/function prints student info on top of page.
//-----------------------------------------------------------------------------
//  Parameters Passed : none
//  Value(s) Returned : none
//  Local Variables   : name   char
//*****************************************************************************

void welcome_header (void)
{  
    char name[15];   //user input, opponent's name

    cout << "Please Enter Your Name: ";
    cin  >> name;

    cout << endl << DASHES << endl << endl;
    cout << "\t    Tossing Die Game - Shooy-Mooy vs. " << name;
    cout << endl << "\t\tNo Seed Supplied for this Run";
    cout << endl << endl << "\t\t     " << SEPARATOR << endl << endl;
}
//-------------------------end welcome_header--------------------------------

//*****************************************************************************
// This method/function generates a random number between 1 and 6.
//-----------------------------------------------------------------------------
//  Parameters Passed : none
//  Value(s) Returned : random number
//  Local Variables   : none
//*****************************************************************************

int toss_die(void)
{
    return(rand() % 6 + 1);
}
//---------------------------- end toss_die------------------------------------

//*****************************************************************************
// This method/function determines the winner of the toss
//-----------------------------------------------------------------------------
//  Parameters Passed : none
//  Value(s) Returned : random number
//  Local Variables   : none
//*****************************************************************************

void toss_winner(int &dice1, int &dice2)
{
    int sum;                 // sum of each roll
    sum = dice1 + dice2;

    switch (sum)
    {
       case 2: cout << "Him" << endl;
               //smScore = smScore + 2;
               break;
       case 3: cout << "Him" << endl;
               //smScore = smScore + 3;
               break;
       case 6: cout << "Him" << endl;
               //smScore = smScore + 6;
               break;
       case 7: cout << "Him" << endl;
               //smScore = smScore + 7;
               break;
       case 4: cout << "Me"  << endl;
               //oppScore = oppScore + 4;
               break;
       case 5: cout << "Me"  << endl;
               //oppScore = oppScore + 5;
               break;
       case 11: cout << "Me" << endl;
                //oppScore = oppScore + 11;
                break;
       case 12: cout << "Me" << endl;
                //oppScore = oppScore + 12;
                break;
       default: cout << "Draw" << endl;
                //draw = draw + draw;
                break;
    }
}
//---------------------------- end toss_die------------------------------------

//*****************************************************************************
// This function keeps the window open to copy the output.
//-----------------------------------------------------------------------------
//  Parameters Passed : none
//  Value(s) Returned : none
//  Local Variables   : ch
//-----------------------------------------------------------------------------
//*****************************************************************************

void keep_output_window_open (void)
{
     char ch;
     
     cout << "\n******************That's All for Today*********************\n";
     cout << "\n\nPress any letter or digit key to exit: ";
     cin >> ch;
} 
//---------------end keep_window_open------------------------------------------
 
Code:
//smScore = smScore + 2;
Based on these commented out lines, it looks like you were already thinking of using global variables to keep score. That's definitely one way to do it.

Another way is to pass those variables to toss_winner() by reference or return a value indicating who won from toss_winner() and then incrementing the appropriate value yourself...

Code:
//srand(time(NULL));   //To seed or not to seed, that is the question...
You should also uncomment this line before handing in the assignment.

One thing I found that was strange is that I've never seen anyone declare function prototypes inside the main() function; but if it works that's great.
 
cpjust,

That's how my professor likes us to declare function prototypes. Should it be outside the main function? Well, I figured out how to get the scores, # of draws and winner since it turned out I was already on the right track. I am having one problem though, I'm trying to return the opponent's name from the function, but I am getting an error like this:
Code:
void welcome_header (char &oppName);

char oppName[15];

welcome_header (oppName);   //cannot convert parameter 1 from 'char[15]' to 'char&'

void welcome_header (char &oppName)
{  
    //char oppName[15];   //user input, opponent's name

    cout << "Please Enter Your Name: ";
    cin  >> oppName;

    cout << endl << DASHES << endl << endl;
    cout << "\t    Tossing Die Game - Shooy-Mooy vs. " << oppName;
    cout << endl << "\t\tNo Seed Supplied for this Run";
    //cout << endl << "\t\tSeed Supplied for this Run";
    cout << endl << endl << "\t\t     " << SEPARATOR << endl << endl;
}
If I use this as my oppName variable: char oppName;, then I can only use one character and if I type out a whole name, my window closes and does not stay open like it should at the end of program execution. So, how can I fix the char?

Todd
 
Try changing your prototype to:

Code:
void welcome_header(char* oppName);

Passing just the array name you are passing a pointer to the first element of the array.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top