Classwork Program Trouble

I am supposed to be writing a program in class, here is the whole worksheet:
Classwork wrote:

Write a program that assists in a study of rolls of a die. The program should ask the user to roll a die and enter the result. The number of rolls should be entered at the start. Based upon the number entered, a looping procedure of your choice (for, while, do-while) is used to generate the question:

Roll die and enter result (1, 2, 3, 4, 5, or 6) #_:

When complete, the program should display the number and the percentage of 1's, 2's, 3's, 4's, 5's, and 6's. Program output should look similar to:

How many rolls to anaylze? "5"
Roll die and enter result (1, 2, 3, 4, 5, or 6) #1: "1"
Roll die and enter result (1, 2, 3, 4, 5, or 6) #2: "3"
Roll die and enter result (1, 2, 3, 4, 5, or 6) #3: "1"
Roll die and enter result (1, 2, 3, 4, 5, or 6) #4: "2"
Roll die and enter result (1, 2, 3, 4, 5, or 6) #5: "5"
Results:
2 1's, or 40%
1 2's, or 20%
1 3's, or 20%
0 4's, or 0%
1 5's, or 20%
0 6's, or 0%


I added quotes to the worksheet where it is showing user input.

Here is my current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Todd Dunaway
//Chapter 5: Loops Test Program
//1/13/2015
#include <iostream>
using namespace std;
int main()
{
	int rolls, result, rollnum;
	cout<<"How many rolls to analyze? ";
	cin>>rolls;
	cout<<endl;
	while(rolls>=1 && rollnum<=rolls)
	{
		rollnum++;
		cout<<"Roll die and enter result (1, 2, 3, 4, 5, or 6) #"<<rollnum<<": ";
		cin>>result;
	}
	cout<<"Results:"<<endl;
	cout<<"\t"<<"
return 0;
} 


Here is my output:
output wrote:

How many rolls to analyze? 5

Roll die and enter result (1, 2, 3, 4, 5, or 6) #1: 1
Roll die and enter result (1, 2, 3, 4, 5, or 6) #2: 3
Roll die and enter result (1, 2, 3, 4, 5, or 6) #3: 1
Roll die and enter result (1, 2, 3, 4, 5, or 6) #4: 2
Roll die and enter result (1, 2, 3, 4, 5, or 6) #5: 5
Roll die and enter result (1, 2, 3, 4, 5, or 6) #6: 6
Results:



I am having trouble with the while loop, it goes on to a sixth rollnum when the fifth rollnum should be the last. I think that is all I am having trouble with currently, I will reply with more questions if I need to.

Any help would be greatly appreciated.

Thanks,
SER
Last edited on
@TheUnholy

Get rid of the <= before rolls, and just use <. Also, you could remove the rolls>=1 && in the while statement. Do the checking for what the user enters at the point of entry. You could use
1
2
3
4
5
6
7
8
int rolls = 0;
do
{
  cout<<"How many rolls to analyze? ";
  cin>>rolls;
if(rolls < 1)
  cout << "We have to have at least one roll to analyze. Try again.." << endl;
}while (rolls <1);



Last edited on
@TheUnholy
You should initialize the variable rollnum in the program as might be taking garbage values and may cause the unexpected behavior of the while loop
whitenite1 wrote:

Get rid of the <= before rolls, and just use <. Also, you could remove the rolls>=1 && in the while statement. Do the checking for what the user enters at the point of entry. You could use
1
2
3
4
5
6
7
8
int rolls = 0;
do
{
  cout<<"How many rolls to analyze? ";
  cin>>rolls;
if(rolls < 1)
  cout << "We have to have at least one roll to analyze. Try again.." << endl;
}while (rolls <1);



Rohit Saluja wrote:

You should initialize the variable rollnum in the program as might be taking garbage values and may cause the unexpected behavior of the while loop


Thank you, both of these suggestions helped.

I have a question though, how would I get the program to output the results?
@TheUnholy

The best way I can think of, is first create an array of 6. Like int dice[6];
And initialize it to zeros. Then for each roll, increase dice[result-1], by 1. You need to subtract one, since arrays start at zero, not one. Then, when the user is finished throwing the die, get the percentage of each array location, and print them out.
Last edited on
whitenite1 wrote:

The best way I can think of, is first create an array of 6. Like int dice[6];
And initialize it to zeros. Then for each roll, increase dice[result-1], by 1. You need to subtract one, since arrays start at zero, not one. Then, when the user is finished throwing the die, get the percentage of each array location, and print them out.


I have yet to learn about arrays in my class, and my teacher doesn't want us going ahead of the other students. She is still learning programming as well so she might not even know about arrays, she was the only teacher willing to teach programming this year.
@TheUnholy

Sorry, I can't think of any other way to keep track of all the die throws, besides an array. It's also hard for me to understand a teacher, that may know less about programming than the students. To me, learning about arrays, should be one of the first things you learn about programming in a computer language, as it's hard to keep track of lists, etc., without them. I guess you should ask your 'teacher', what you should use instead of arrays to keep track of all the die throws.
Last edited on
In the absence of arrays. define 6 variables (initializing them to zero). Then use a switch statement switching on the result of the roll. Use 6 cases (1-6) and inside each case increment the appropriate variable. If you can't use a switch statement, then you will probably need an if else ladder.
Topic archived. No new replies allowed.