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 = {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];
one++;
two++;
three++;
four++;
five++;
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;
}``````

## All 4 Replies

1) use an array to add up the numbers ( `int roll` )
3) use answer as an index into roll[] and increment ( `roll[answer]++;` )

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

you need to seed rand as well

1) use an array to add up the numbers ( `int roll` )
3) use answer as an index into roll[] and increment ( `roll[answer]++;` )

At the end of the loop, roll 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``

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!

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:

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.