Storing Data and Using Loops

I've been assigned some homework in class, and I understand if homework is frowned upon. The problem is, I believe I have a learning disability, and while I believe I've already learned the information I'm asking about, I can't seem to recall it for the life of me.

I have two issues I'd like addressed, but first, assume that "Line 3: die dieRoll;" is pulled from a header file that's already been defined correctly; also, the problem that's been posed to us asks us to find how many times the user's inputted desired sum has been hit in the amount of times rolled.

So, my first issue is that with my timesRolled for loop, every time it loops, it continues to add the sum to the previous sum as well, which is not what I want happening. What I want is the timesRolled amount of sums, separately displayed, so essentially 10 die rolled 10 times, firs roll, all die are 1; sum would be 10. Second roll, all die roll 1 except one die rolls 2, so the sum for that roll would be separate and will be 11, and so on from that.

The next issue I'm having is I'm trying to figure out how to pull the amount of times desSum is hit from the amount of times rolled. I believe the commented out code at the bottom (/**/) is on the right path, but I could be wrong, so any pointers that could push me in the right direction would be greatly appreciated.

Thanks guys and gals!

For easier reference, my problems start at the first for loop. Everything else above that works fine.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
int main()
{
	die dieRoll;			
	int timesRolled;
	int desSum;
	int sum = 0;

	int numDieRolled[10];

	cout << "Enter the number of times 10 6-sided die are to be rolled: ";
	cin >> timesRolled;
	cout << endl;
	cout << "Enter the desired sum: ";
	cin >> desSum;
	cout << endl;

	while (desSum < 10 || desSum > 60)
	{
		cout << "The number you entered was invalid, please re-enter" << endl;
		cout << "a valid number for 10 6-sided die to be rolled: ";
		cin >> desSum;
		cout << endl;
	}

	cout << endl;

	cout << "Die rolls:" << endl;

	for (int j = 0; j < timesRolled; j++)
	{
		cout << "new roll" << endl;

		for (int i = 0; i < 10; i++)
		{
			dieRoll.roll();
			numDieRolled[i] = dieRoll.getNum();

			cout << numDieRolled[i] << endl;

			sum = sum + numDieRolled[i];

			cout << "The sum is " << sum << endl;
			cout << endl;
		}
	}

	cout << endl;

	/*if (sum == desSum)
	{
		int desSumArr[sum];
	}*/

	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
    // ...

    int cnt_sum_hits = 0 ; // number of times sum was equal to desSum

    for( int i = 0 ; i < timesRolled ; ++i )
    {
        int sum = 0 ; // the sum of this set of rolls of ten dice
        for( int i = 0 ; i < 10; ++i ) sum += dieRoll.getNum() ;
        if( sum == desSum ) ++cnt_sum_hits ;
    }

    // print out cnt_sum_hits 

http://coliru.stacked-crooked.com/a/b4fccedecf65c87a
Thanks! That did it!
Topic archived. No new replies allowed.