In this program, the two dices will be rolled. The user will prompt a sum they want to obtain. The dices will keep rolling until the desired sum is receieved. I want to see how many times will the dices be have to rolled until I get the correct sum. My problem here is that in the end the dices rolled is always to one. Is there something wrong with my function. If so, can you give me a hint of how to fix it please :)
int sum (int total)
{
int dice1;
int dice2;
int sumset;
sumset=0;
int counter;
while (sumset!=total)
{
counter=0;
dice1=rand()%6+1;
dice2=rand()%6+1;
sumset=dice1+dice2;
counter++;
}
return counter;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"The following program will role the dice until it achieves a your output number: ";
cout<<"Enter the sum you want to get from a pair of dice: ";
int number;
cin>>number;
while(number<2 || number>12)
{
cout<<"Invalid input";
cin>>number;
}
cout<<"\n\tThe number of dice you rolled to obtain the sum is: " <<sum (number);
_getch();
return 0;
}
You need to also have a condition for if sumset > total, otherwise the loop will often run forever.*
You also need to initialize sumset to zero.
Also, you a rolling two dice in the loop but only incrementing counter by one.
*Technically not forever, eventually, the integer will overflow and we will keep going around and around all the numbers until the user number is found, but this is a scenario we really do not want to see happening.
Matt, just to be on the right page. The maximum sum I can get from two dices will be 12. Suppose user wants 8 as a sum. If the sum does not equal total the loop will keep repeating itself. How is sumset >total be a factor? I can't visually see...do you mind writing it in the code? And I already have sumset inialitzed to zero
The way your function is right now, as soon as sumset becomes greater than total, your loop will never end, if by chance you were unlucky enough not to throw the right pair in the first couple of throws. Here's an improved function that should do as you needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int sum (int total)
{
int dice1;
int dice2;
int sumset = 0;
int counter = 0;
while (sumset!=total)
{
sumset=0; //Reset sumset for upcoming dice throw totals
dice1=rand()%6+1;
dice2=rand()%6+1;
sumset=dice1+dice2; // Add together the two values of the dice
// If sumset does not equal total, try again
counter+=2; // Increase counter by two. One for each die
}
return counter;
}
First roll, di 1: 3
First roll, di 2: 2
sumset = 5
Second loop
Second roll, di 1: 5
Second roll, di 2: 1
Sumset = 11
Now, sumset is > the total the user wanted and is going to keep growing.. Do you see the problem? Sumset needs to be set to zero at the beginning of the loop.
I understand what your saying, but this is how i'm perceiving this to be.
user chooses 8
First Loop.
First roll, di 1: 3
First roll, di 2: 2
Sumset=3+2
sumset=5
Checks while loop. "Does 5 = 8" No, so it enters the loop again.
second loop.
Second roll, di 1: 5
Second roll, di 2:1
5=5+1
sumset=6 //6 gets stores in sumset and the 5 is removed
I do get the point your trying to make; however, I still can't spot the problem. If I used sumset=sumset+diceone+dice2; then should this not be the equation that adds the previous sumset.
You are correct. I misread the loop also. The variable counter = 0, should be outside the while loop, so counter can be increased, instead of being reset to zero each time. You should also add srand() to randomize the results at the beginning.
// Dice Roll.cpp : main project file.
#include <iostream>
#include <ctime>
#include <conio.h>
#include <random>
usingnamespace std;
int sum (int total)
{
int dice1;
int dice2;
int sumset;
int counter =0;
while (sumset!=total)
{
dice1=rand()%6+1;
dice2=rand()%6+1;
sumset=dice1+dice2;
counter++;
cout << sumset << endl; // Show results of the rolls, until total is obtained..
}
return counter;
}
int main()
{
srand((unsigned) time(0)); // randomize
cout<<"The following program will role the dice until it achieves a your output number: ";
cout<<"Enter the sum you want to get from a pair of dice: ";
int number, rolls;
cin>>number;
while(number<2 || number>12)
{
cout<<"Invalid input";
cin>>number;
}
rolls = sum (number);
cout<<"\n\tThe number of times you rolled to obtain the sum of " << number << " is: " << rolls;
_getch();
return 0;
}