473,779 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 1799
Ganon11
3,652 Recognized Expert Specialist
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
1958
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 looking for. It allows you to send a message to individual players (chatters). It allows for actions using "/me". The only thing it really needs is the capability to do a dice roll. Can someone point me to a site which might have some examples...
102
4562
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 spend twice the time to complete. If the loop is included it takes about 7.48 seconds to complete, but when removed it takes about 11.48 seconds. Does anybody have a suggestion as to why this is so and whether I can trust the results of the...
4
1766
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 freely combines such a small, clean syntax with such a powerful synthesis of procedural, object-oriented and functional techniques." My (hardcore C++) workplace is now very much converted to Python for
101
6423
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 I can check cases by brute force? EC
0
2351
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 code so far. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
17
9609
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 simulation, one or more of my sums is zero, which is highly unlikely. So I'm sure there's a bug in my code. But I can't tell what it is, even after re-reading it several times. Can someone give me a hint? Here is my code:
1
6221
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 dice, the code for the rolling of the dice should take place in a user written module named rolldice. b.. The rules of the game are as follows:
4
6050
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 attention in the class.. Seriously, my teacher just ask us to do the sample programs in the textbook and prepare for the finals... Everytime I ask him question... he has to open the textbook to find the answer.. I've been searching for dice...
1
1567
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 arrRolls++; but it gets another number that never rolled. sorry my English is not that good..
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.