473,396 Members | 1,966 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,396 software developers and data experts.

C++ beginner- How to roll two dice??

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 roll in the google but I can't really understand the codes..

I just want to ask how to roll two dice in a really simple way?
Or use the loops? For loops or while loops?
Does there have any function for it or----?

thanks alot....

---------------------------------------------This is the direction..!
Write a program that will 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.

Example output
How many rolls would you like? 10

D1: 3 D2:6 Total: 9
D1: 4 D2:2 Total: 6
D1: 3 D2:1 Total: 4
D1: 5 D2:2 Total: 7
D1: 1 D2:1 Total: 2
D1: 2 D2:6 Total: 8
D1: 4 D2:4 Total: 8
D1: 6 D2:4 Total: 10
D1: 3 D2:4 Total: 7
D1: 5 D2:2 Total: 7

Dice Roll Totals
2=1
3=0
4=1
5=0
6=1
7=3
8=2
9=1
10=1
11=0
12=0
---------------------------------------------------------
Jan 16 '08 #1
4 6011
RedSon
5,000 Expert 4TB
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 roll in the google but I can't really understand the codes..

I just want to ask how to roll two dice in a really simple way?
Or use the loops? For loops or while loops?
Does there have any function for it or----?

thanks alot....

---------------------------------------------This is the direction..!
Write a program that will 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.

Example output
How many rolls would you like? 10

D1: 3 D2:6 Total: 9
D1: 4 D2:2 Total: 6
D1: 3 D2:1 Total: 4
D1: 5 D2:2 Total: 7
D1: 1 D2:1 Total: 2
D1: 2 D2:6 Total: 8
D1: 4 D2:4 Total: 8
D1: 6 D2:4 Total: 10
D1: 3 D2:4 Total: 7
D1: 5 D2:2 Total: 7

Dice Roll Totals
2=1
3=0
4=1
5=0
6=1
7=3
8=2
9=1
10=1
11=0
12=0
---------------------------------------------------------
You might want to try looking up srand(...) and rand(...) those things will probably be the biggest help to you.
Jan 16 '08 #2
Studlyami
464 Expert 256MB
First break down the logic of the program in regular words (don't worry about code). What steps would you do (in real life) to get the results that you wanted. Then after you have figured out the process convert that over into code. As far as determining the random part (the rolling of the dice) use Red Son's advice and look up srand and rand. If you still have problems with it post back up your questions and show us what you have tried.
Jan 16 '08 #3
hi

sorry for posting solution code, never again.

A good way to solve your problem would be to use rand() from stdlib.h and try to get a number between 1 and 6.
this could be done by producing a real number between 0 and 1 and keep for examble , from the real number 0.678284584... the 0.6 and multiply it by 10 and do that until you get a number between 1 - 6
Jan 17 '08 #4
RedSon
5,000 Expert 4TB
hi,

read this peace of code i think will help you:

this function creates a random int number between [low....high] in your case put low = 1 and high = 6 and it make use of rand() from stdlib.h, so

deleted

this function is taken from a library made by Eric S. Roberts who wrote the book
"The Art and Science of C" ISBN 960-209-791-4.
First off, we do not give code solutions to homework problems. Second, we do not post copyrighted code. This is your first last and only warning about this. Please read the posting guidelines so you will understand what is acceptable and what is not.
Jan 17 '08 #5

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

Similar topics

5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
8
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
7
by: Rensjuh | last post by:
Hello, does someone have / know a good C++ tutorial for beginnners? I would prefer Dutch, but English is also fine. Hoi, heeft / kent iemand nog een goede C++ tutorial voor beginners? Het liefste...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
20
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
10
by: hamza612 | last post by:
I want to start learning how to program. But I dont know where to start. From what I've heard so far c++ is not a good lang. to learn as a beginner because its very complicated compared to others...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.