So I cant get the division to work on my c++ homework.
This is a diceroll game and im trying to get sum1=num1/diceRoll to get the percentage of # of times divided by the amount of time the dice is being rolled. It keeps giving me 0 because I assigned num1 to equal 0 which makes sense but it won't let me do without the 0. Any suggestions?? Thanks!


}

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;

int main ()
{
	const int maxDice =6;
	int diceRoll;
	int rolled;
	int dice = 1;
	int num;
	int num1=0;
	int num2=0;
	int num3=0;
	int num4=0;
	int num5=0;
	int num6=0;
	double sum1;
	
	 srand ((unsigned int)time(NULL));
	

	cout << "How many times would you like to roll the dice? ";
	cin >> diceRoll;
	


	for (rolled = 1; rolled <=diceRoll; rolled++)
	
	{
	
	num = rand() %6 +1;
	

	if (num == 1)
	{
		 num1++;
		 

	}

	else if (num == 2)
	{
		num2++;
	}

	else if (num==3)
	{
		num3++;
	}

	else if (num==4)
	{
		num4++;
	}

	else if (num==5)
	{
		num5++;
	}

	else if (num==6)
	{
		num6++;
	}


	}
	cout << "# Rolled         # Times           % Times" << endl;

	sum1= num1/diceRoll;

	cout << "1\t\t"<< num1  << "			" << sum1 << endl ;
	cout << "2\t\t" << num2 << endl << "2\t\t"<< num3 << endl;
	cout << "4\t\t" <<num4 << endl <<"5\t\t" << num5 << endl << "6\t\t" << num6 << endl;

	system ("PAUSE");
	return 0;
}

I don't know what you're asking, could you be a little more clear?

Basically I'm trying to do divison and all it gives me is zero. So if you actually compile the program it gives you a number where it says "# Times" which is "num1" and that is a random number. All i'm trying to do is divide that "num1" number with the number the user inputs assigned as "diceRoll"

num1 gives a different number everytime since I am using the srand function but after hours of trying so hard I was able tofigure it out. The reason why I was getting a 0 all the time as an output is because diceRoll was an int and not a double. Now I am trying to figure out how to align the math work by decimal. This is what I tried

cout << "   1\t\t" << "\t  " << num1 << "			" << fixed << setprecision(2) << setw(2) << num1/diceRoll*100 << " %" << endl;
	cout << "   2\t\t" << "\t  " << num2 << "			" << fixed << setprecision(2) << setw(2) << num2/diceRoll*100 << " %" << endl;
	cout << "   3\t\t" << "\t  " << num3 << "			" << fixed << setprecision(2) << setw(2) << num3/diceRoll*100 << " %" << endl;
	cout << "   4\t\t" << "\t  " << num4 << "			" << fixed << setprecision(2) << setw(2) << num4/diceRoll*100 << " %" << endl;

this still won't align them by decimal places I'm not sure why

Is this for yahtzee???

No it's just a c++ program im doing on visual studio for homework

This article has been dead for over six months. Start a new discussion instead.