473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

dice loop problem - never stops, and dice loop total is wrong

I am a c++ beginner and i am working on a dice loop lab.

This program should roll two dice (each dice is from 1 to 6) and then total them together. The user should be able to input how many times they would like to have the dice rolled. Your program should output the rolls and once the rolls are done, it should output how many of each number had been rolled.

//this program will run forever and never stops expect i input the number than is smaller than 6.
//and therefore, the result is wrong for not printing result of the total dice roll 7 to 12.



Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <time.h>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6. int roll_dice();
  7. rand();
  8. int main(int argc, char* argv)
  9. {
  10.     int n_rolls;
  11.     int roll_dice();
  12.     int aRolls[12];
  13.     int dieRoll;
  14.     srand((unsigned)time(0));
  15.  
  16.     cout << "How many times would you like to roll the dice?" << endl;
  17.     cin >> n_rolls;
  18.  
  19.     for (int k = 1; k < 12; k++)
  20.     {
  21.         aRolls[k] = 0;
  22.     }
  23.  
  24.  
  25.     for (int i = 1; i <= n_rolls; i++)
  26.     {
  27.         dieRoll = roll_dice();
  28.         i = roll_dice();
  29.         dieRoll = rand() % 6 + 1;
  30.         i = rand() % 6 + 1;
  31.         cout << "D1: " << i << setw(10) << 
  32.             "D2: " << dieRoll << setw(10) << 
  33.             "total:" << i + dieRoll << endl;
  34.         aRolls[dieRoll - 1]++;
  35.     }
  36.  
  37.  
  38.  
  39.  
  40.     cout << "                    " << endl;
  41.     cout << "Dice Roll Totals" << endl;
  42.  
  43.     for (int j = 1; j < 12; j++)
  44.     {
  45.         cout << j + 1 << " = " << aRolls[j] <<  endl;
  46.     }
  47.     return 0;
  48. }
  49.  
  50. int roll_dice()
  51.  
  52. {
  53.     int result;
  54.     result = rand() % 6 + 1;
  55.     return result;
  56. }
  57.  
Jan 28 '08 #1
1 1784
Ganon11
3,652 Expert 2GB
Your program is giving you trouble because, in your first loop, you use i as one of your die result variables. But i is controlling the loop. Essentially, you are making your loop act random and haywire. There's very little chance it will do what you expect. Make a second die_result variable to store the second die; leave i alone.
Jan 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: lawentzel | last post by:
I am new to PHP and am an avid RPG gamer. I am looking to use PhpMyChat to be able to game with friends all over the US. With that having been said, PhpMyChat seems to offer a lot of what I am...
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
4
by: Richard Buckle | last post by:
Hi fellow Pythonistas, I've been using Python in anger for some time, and I must say, as I wrote in <http://macprang.sourceforge.net/>: "It's refreshing beyond words to use a language that so...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
0
by: Bree | last post by:
The game requires it not to accept negative numbers. At the moment it isnt, and it is urgent I find the solution asap. So if anyone can help I would much appreciate it. Thanks Bree This is the...
17
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my...
1
by: lenest | last post by:
I need help writing a program.... You are to write a python program to accomplish the following: a.. Play a dice game of Craps using a random number generator to simulate the roll of the...
4
by: clairelee0322 | last post by:
Hello guys again! I am a C++ beginner and I am working on another project.. It's called Dice Lab with Loops.. unforunately I don't even know how to start the program.... Don't blame me for not pay...
1
by: clairelee0322 | last post by:
I can run my dice loop but the only problem is that the number 1 can't be rolled no matter how large the number can roll.. i tried to change then value of x, rrRolls - x ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.