954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dice program

I'm writing a program that rolls a die a number of times that the user enters, and counts up each time a one, two, three, and so on is rolled. Right now, it only prints out zero's. Any guidance would be appreciated!

Here's my code:

// dice.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
    int six = 0;
    int result[6] = {0};
    int numberOfRolls;
    cout << "Enter the number of times that you want to roll the die: ";
	cin >> numberOfRolls;
    for (int counter= 1; counter < numberOfRolls; counter++)
	{
		
		int answer = result[rand() % 6 + 1];
		if(answer == 1)
			one++;
		if(answer == 2)
			two++;
		if(answer == 3)
			three++;
		if(answer == 4)
			four++;
		if(answer == 5)
			five++;
		if(answer == 6)
			six++;
	}
    cout << "The number of one's rolled is: " << one << endl;
    cout << "The number of two's rolled is: " << two << endl;
    cout << "The number of three's rolled is: " << three << endl;
    cout << "The number of four's rolled is: " << four << endl;
    cout << "The number of five's rolled is: " << five << endl;
    cout << "The number of six's rolled is: " << six << endl;
	system("pause");
	return 0;
}
rfrapp
Junior Poster in Training
57 posts since Dec 2011
Reputation Points: 27
Solved Threads: 0
 

1) use an array to add up the numbers ( int roll[6] )
2) calculate your randomanswer (do not add 1 giving 0 => 5)
3) use answer as an index into roll[] and increment ( roll[answer]++; )

At the end of the loop,roll[3] will contain the number of 3's "rolled".

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

you need to seed rand as well

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
 

1) use an array to add up the numbers ( int roll[6] ) 2) calculate your randomanswer (do not add 1 giving 0 => 5) 3) use answer as an index into roll[] and increment ( roll[answer]++; )

At the end of the loop,roll[3] will contain the number of 3's "rolled".

I just began learning arrays in programming class, and I have a question for you. Wouldn't

roll[3]


show how many 4's there were? My reasoning behind that is, doesn't it start at zero? Please excuse my ignorance, as I have said, I just began learning arrays. Thank you for your answer!

rfrapp
Junior Poster in Training
57 posts since Dec 2011
Reputation Points: 27
Solved Threads: 0
 

Excellent question! Yes, you are correct.

Since the random value will be 0-5, that will equate to a roll of 1-6. Your output must compensate for that value shift.

I was hoping you'd come up with that yourself. :icon_biggrin:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: