i have my questions commented in the program . i had to put most of program up so you could see what im doin. please help
PROTYPES

void side_on_die(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& ns6);
int roll_hold(int roll, int answer, int turn);
int if_roll();
void if_hold();
int main()
side_on_die(ns1,ns2,ns3,ns4,ns5,ns6);
    roll_hold(answer);
    roll=if_roll();
    if_hold();
    whose_turn=turn_it_is(player1,player2);

//function that rolls dice. IT DOESNT ROLL I GUESS YOU CAN SAY. I WAS THINKING OF PUTTING THIS IN A SWITCH STATEMENT BUT HAVING PROBLEMS WITH GENERATING RANDOM NUMBER

int if_roll()
{
    int which_Num;
    unsigned seed = time(0);
            srand(seed);
            which_Num = rand() % 7;
    return which_Num;
}

//function that holds.IT HOLDS BUT NOT HOW I WANT IT 2. U KNOW Y?

void if_hold()
{
    system("pause");
}

//function that shows value of side_of_die. IS THIS DOING THIS?

void side_on_die(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& ns6)
{
   ns1&=1; ns2&=2;ns3&=3; ns4&=4;ns5&=5;ns6&=6;
}

// function that does action after user has choosen to roll or hold. problem is that it just goes to system pause after the cout statement regardless of the letter choosen. U KNOW Y?

char roll_hold( int& answer)
{
    cout<< " Would you like to roll or hold? press r for roll and h for hold.: ";
    cin >> answer;
        if (answer=='h')
        {
            cout<< system("pause");
        }
        return answer;
}

Recommended Answers

All 7 Replies

//function that rolls dice. IT DOESNT ROLL I GUESS YOU CAN SAY. I WAS THINKING OF PUTTING THIS IN A SWITCH STATEMENT BUT HAVING PROBLEMS WITH GENERATING RANDOM NUMBER

int if_roll()
{
    int which_Num;
    unsigned seed = time(0);
            srand(seed);
            which_Num = rand() % 7;
    return which_Num;
}

You want to seed the RNG once and only once and do it main at the very beginning. Move lines 4 and 5 outside of the function and stick them at the top of main. Other than that, the function looks fine. it returns a random number from 0 to 6. That is what you want?

//function that holds.IT HOLDS BUT NOT HOW I WANT IT 2. U KNOW Y?

void if_hold()
{
    system("pause");
}

What does "holding" mean? What are you trying to accomplish. You say it works, but not how you want it to. How do you want it to work? system ("PAUSE") is generally to be avoided. You'll find a whole bunch of threads explaining why. But what do you want to do?

//function that shows value of side_of_die. IS THIS DOING THIS?

void side_on_die(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& ns6)
{
   ns1&=1; ns2&=2;ns3&=3; ns4&=4;ns5&=5;ns6&=6;
}

No clue what you are trying to do here, but I feel certain this isn't what you want. If you are trying to show the side of a die, I imagine you want something like this:

void side_on_die(int die)
{
    cout << die;
}

// function that does action after user has choosen to roll or hold. problem is that it just goes to system pause after the cout statement regardless of the letter choosen. U KNOW Y?

char roll_hold( int& answer)
{
    cout<< " Would you like to roll or hold? press r for roll and h for hold.: ";
    cin >> answer;
        if (answer=='h')
        {
            cout<< system("pause");
        }
        return answer;
}

No it does not pause no matter what you enter. It returns to main and then if_hold() is called and THAT function will always pause. Try deleting the call to if_hold() in main.

You want to seed the RNG once and only once and do it main at the very beginning. Move lines 4 and 5 outside of the function and stick them at the top of main. Other than that, the function looks fine. it returns a random number from 0 to 6.


What does "holding" mean? What are you trying to accomplish. You say it works, but not how you want it to. How do you want it to work? its a game of pigmania. system ("PAUSE") is generally to be avoided. You'll find a whole bunch of threads explaining why. But what do you want to do?

No clue what you are trying to do here, but I feel certain this isn't what you want. If you are trying to show the side of a die, I imagine you want something like this:

void side_on_die(int die)
{
    cout << die;
}

No it does not pause no matter what you enter. It returns to main and then if_hold() is called and THAT function will always pause. Try deleting the call to if_hold() in main.

yes i want it to generate random numbers 1-6. and im making a game of pigmainia. and where u say u dont know wat im doing here i am trying to set the values to the side of dice so the generator would have something to randomize. but idk how i will give values to the side of dice and how will the generator know wat numbers to randomize

//function that shows value of side_of_die.

This description suggests you wanted to display something.

If you are trying to ASSIGN numbers to the variable, you have it right, but get rid of the ampersands inside the function.

Wrong:

void side_on_die(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& ns6)
{
   ns1&=1; ns2&=2;ns3&=3; ns4&=4;ns5&=5;ns6&=6;
}

Right:

void side_on_die(int& ns1,int& ns2,int& ns3, int& ns4, int& ns5, int& ns6)
{
   ns1=1; ns2=2;ns3=3; ns4=4;ns5=5;ns6=6;
}
which_Num = rand() % 7;

This generates a number from 0 to 6. If you want to generate a number from 1 to 6, do this:

which_Num = 1 + rand() % 6;

Did you look in your other 2 threads concerning these questions? I told you how to generate the die faces in your "generate random number" thread.

To generate die faces you would take a random number modulus 6 (which generates 0-5) then add 1 (which shifts it to 1-6). This could then be contained in a function (called for example "roll()") that returns an int.

int roll();

int main() {
  int dieValue = 0;

  //seed the random number generator

  //begin a loop

    /* ...other code... */
    dieValue = roll();
    /* ...other code... */

  //end loop

  /* ...other code... */

  return 0;
}

int roll() {
  int faceValue = 0;
  /* randomly generate a number 1-6, store it in faceValue */
  return faceValue;
}

ok yeah i went back and i got it but now when i look at the function that handle takin of a turn. it says your turn player 97047758 not 1 or 2 like i was hoping it would do.

//function that tells the user whos turn it is . & HOW WILL I HOLD THE NUMBER THAT THE USER ROLLS AND KEEP TRACK OF IT SO I CAN SAY WHO WINS WHEN THEY REACH 100.
int turn_it_is(int player1, int player2)
{
    int go_again, turn;
    cout <<  " it's your turn "
            "next player " << (((turn % 2) ==1) ? player1:player2) << endl;
    return turn;
//function that displays current score. DOES NOT PRINT CURRENT SCORE 
void display_score(int curr_score)
{
    int gam_answ;
    curr_score = gam_answ++;
}

If you want a variable to be useful, you need to declare it properly and place a useful value in it. 90% of your variables are implicitly declared (which isn't even technically legal) and not one of them is initialized. The variables you do explicitly declare you aren't putting useful values in, so everything contains "garbage" values.

How do you expect gam_answ to be of any value if you don't put anything useful in it? Do you think the correct value is just going to magically appear in it? NEWS FLASH! It doesn't.

In addition, how do you expect to show something on the screen if you don't tell the program to show something? What is display_score() supposed to do? Currently it does nothing useful, it adds a garbage value to curr_score............then what....?.................. Answer: It returns and the new garbage value of curr_score is lost to oblivion.

It's good that you are trying to use functions, but you need to organize your thoughts better.

I think you need to go back to square 1. Set this program aside for a while. Try to create the smaller parts of it independently, then once you have them working, incorporate them into a final program.

Take the framework I gave you in post 5 and try to write a program that generates a random number between 1 and 6 then displays it on the screen. After that, try to write the same program using the roll() function instead of doing it directly in main(). Do you see where I'm going with this?

What sort of tutorial document are you using? Do you have a textbook? I think you're trying to take on more than you can handle.

when i run this it only ask if u would like to roll or hold only once its not reading the second if statement and not computing the roll right and alternating players

// function that does action after user has choosen to roll or hold.
char roll_hold( int answer1, int answer2)
{
    int numb_on_dice1,numb_on_dice2,play1, play2,next_player,play1_total, play2_total;
    cout<< " Would you like to roll or hold? press r for roll and h for hold:";// ask user if want to roll or hold
    cin >> answer1; //gets answer
    if(answer1=='r')// if  chooses to roll
      {
          numb_on_dice1= answer1,play1_total; // is equal answer1,play1_total. i want to save and pass by refrence this value. HELP HERE.
          numb_on_dice1  = 1 + rand() % 7; // computes roll.
          cout <<  " it's your turn "
          "next player " << (((next_player % 2) ==1) ? play1:play2) << " player1 number is:" << answer1 << answer1++ << endl;
          cout<< " Would you like to roll or hold? press r for roll and h for hold:";// sapost to alternate players 
    cin >> answer2; // gets answer 2 for player 2.
            {                                                 // i just copied and paste but its the same thing i just did it for player 2
            if(answer2=='r')
                {
                    numb_on_dice2= answer2,play2_total;
                    numb_on_dice2  = 1 + rand() % 7;
                    cout <<  " it's your turn "
                    "next player " << (((next_player % 2) ==1) ? play1:play2) << " player2 number is:" << answer1 << answer1++ << endl;
                    cin >> answer1;
                }
            }
      }
    answer1=play1;
    answer2=play2;

return play1_total, play2_total;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.