C :: If Statements For Dice Rolling
Oct 9, 2014
I'm very new to C Programming and am doing some homework for my intro to C class. "There is a game where one can play and bet money on. Two dice are rolled and the numbers that appear on the dice are added together. If the total is 7 or 11, then the user wins. If the total is 2 or 12, then the user loses. If a different total appears. then the user gets their money back."
I have written the program below:
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main()
{
int d1,d2,sum;
d1=0;
d2=0;
}
[code]...
However, after finishing the program, I realized that I NEEDED to use random numbers!!! How do I make my program generate random numbers within a range of 1-12? Also, how do I make my program generate NEW random numbers EVERY TIME instead of ONCE??
View 1 Replies
ADVERTISEMENT
Oct 29, 2013
How would you code a simple random generator for dice rolling?
View 2 Replies
View Related
Dec 1, 2014
I'm trying to create a program that rolls a dice a few thousand times and print out how many times it lands on each face and then print out a graph using x's as a visual representation using classes.
output looks like this:
1 --- 2000 xxxxxxxxxxxxxxxxxxxxxxxxx
2 --- 1950xxxxxxxxxxxxxxxxxxxxxxxx
.
.
6 --- 2050 xxxxxxxxxxxxxxxxxxxxxxxxxxx
I need a method called int count(int face) inside my class that returns the number of times a particular face has appeared, I just don't know how to incorporate this. I think I should use a constructor and destructor but i'm new with those and knowing when to actually use them.
Here is what I have so far that shows how it should look when printed, just struggling to convert using a class.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
[Code]....
View 2 Replies
View Related
Feb 19, 2013
I am making a program where a computer and player take turns rolling dice.
There seems to be an issue somewhere in my loop where when the player selects from the possible dice choices, it rolls for the computer and does not select the choice and it immediately becomes the player's turn again.
I am trying to get it to where when the computer rolls the dice, it should display the list of possible values, and ask the user to hit the enter key in order for the computer to choose which value to select using the following command:
Code:
while ( getchar() != '
' ) ; I've tried it a hundred times and no matter where I put the command it still skips the computer's turn.
Below is my code:
Code:
while (compscore < 99 && playerscore < 99){
printf("You have rolled the following pair of dice:
");
a =("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL);
b =("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL);
multiply = a * b;
[Code] ....
View 11 Replies
View Related
Feb 1, 2015
I am building the game risk and I want to start off with a dice rolling mechanism. In risk 5 die are needed so I want to know the most efficient way of churning out the code for this. Since the players have the options of rolling 1, 2 or 3 die (max 3 for attacker, and max 2 for defender) I was thinking something like this after searching the web for a few different options:
const int minNum = 1;
const int maxNum = 6;
int numberGenerator(){
int x;
x = minNum + rand() % (maxNum - minNum + 1);
[Code] ....
Since I will be asking the player the option to choose how many die to roll, is this the most efficient way? I want to compare the total sums after the die are rolled. If they choose 3 dice (the attacker) I will simply call the first 3 methods and either 1 or 2 for the defender.
View 14 Replies
View Related
Sep 27, 2014
I wrote a code that mimics a dice, my code so far will ask for the user to input a number at which they want to hold or no longer throw the dice, it will also ask the user how many roll simulations they would like to do.
If a 1 is rolled the turn is over and the score is 0
After this input is recieved the code goes into a loop until the values have been met.
I would to add probability to my code so I need to know how many times a certain number was rolled and this is where I am stuck.
An example of how I would like my code to work, let's suppose hold at number is 17
We role the dice until we roll a 17 or larger, but if we roll a 1, then our score is 0.
Let's suppose the number of simulations is 5 and for simulation 1 we roll
2 + 5 + 6 + 4 = 17
and for simulation 2 we roll
5 + 4 + 1, but since we rolled a 1, the score is 0,
for simulation 3 we roll
3 + 4 + 5 + 6 = 18
for simulation 4 we roll
4 + 4 + 6 + 6 = 20
for simulation 5 we roll
3 + 4 + 5 + 5 = 17 so that means we got
0: 1 time 1/5 = .20
17: 2 times 2/5 = .40
18: 1 time 1/5 = .20
19: 0 times 0/5 = .00
20: 1 time 1/5 = .20
21: 0 times 0/5 = .00
22: 0 times 0/5 = .00
In other words the idea is to roll a 17 or larger (or whatever the hold number happens to be), but if you roll a 1, then your score is a 0.
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int dieRoll();
[Code] ....
View 4 Replies
View Related
Oct 28, 2013
I made this dice simulator which basically throws the dice 1 million times and outputs the frequency and percentage average for each side (1 to 6).
Everything is working fine, except my averages (floats) seem to be rounding up, causing 4% being "unassigned" at the end of the million rolls. I'm outputting with a setprecision of 2, but I only get 0's and no fractional numbers.
View 5 Replies
View Related
Oct 15, 2014
I have written the code below that allows the user to input any sided dice and any number of rolls. The program will print out the output in sequence. I need to sum these outputs into one number, but I am having trouble writing a sum function for this.
//This Program Creates the Sum of a Number of Dice Rolls of the Same Size
#include <stdio.h>
#include <math.h>
#include <time.h>
int rolldie();
int main(){
int x, n, a; //'x' n=number of rolls, a=number sided dice
[Code] ....
The output looks something like this currently:
Please Enter the Number Sided Dice Followed by the number of Rolls... 12 10
4 12 2 10 12 5 5 12 12 5 >Exit code: 0
View 2 Replies
View Related
Sep 23, 2014
I saw a program in which it uses rand like this:-
d1=rand() % 6+ 1;
where, d1 is any integer. The program is to simulate the roll of a dice. The whole program is this:-
Code:
#include<stdio.h>
#include<stdlib.h>
main() {
int i;
int d1, d2;
int a[13];
[Code] .....
View 10 Replies
View Related
Feb 19, 2013
I'm having a problem adding up the values of the dice rolls in a while loop. The program will only store the last value so how would I get it to store multiples values? Here is my program thus far. I would try an array but we haven't gotten to that yet.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int die_1=0, die_2=0, total_score1, total_score2, player1score;
char enter;
[Code] ....
What operators or equation I should use to accomplish this?
View 7 Replies
View Related
Feb 7, 2013
My issue is that my program dies whenever it attempts to readout the average and sum of the rolls. I think I need to include the size function in here somewhere but am not sure how to go about it. Possibly scrap the "do while" loop and go with something else, or just am completely missing something.
Here's the code:
#include<iostream>
#include<random>
#include<ctime>
#include<vector>
#include<cstdlib>
#include<iomanip>
using namespace std;
int sum5(int d1, int d2, int d3, int d4, int d5);
int DiceRoll();
[Code] ....
View 3 Replies
View Related
Mar 21, 2015
The only difficulty im having is creating a class and methods & being able to access them from my win form app. Can i get a few tips on the do's and donts of creating classes / methods and accessing them from form app.
This is what i have put together so far.
public partial class Form1 : Form {
private Image[] dicePics;
private int[] diceNum;
private Random randomize;
public Form1() {
InitializeComponent();
[code]....
View 3 Replies
View Related
Mar 25, 2013
I am writing a snakes and ladder program and I'm almost finished, but I am struggling with the dice to work in the way I want it to work. I want the dice to work like this :
Before each player throw the dice they must start at 0.
Each player must throw a 6 on the dice to move on the board.
If a player threw a 6 on the dice, that player can throw again.
But I ended up with two seperate dice, one for each player (game is only for two players).And when I run the program, both players don't start at 0. And when I throw the dice, both players move at the same time but with different values.
If one of my players threw a 6, they just keep on throwing until someone wins the game. I tried to use a switch and if statements but I couldn't get it right. And so I did this :
Code:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define SpaceBar 32// 32 is an ASCII value for a spacebar
[Code] ....
View 5 Replies
View Related
Nov 27, 2014
i have a project where i create a dice game, the user rolls 2 dice and the computer roles 2 dice. the player that wins 3 out of 5 rolls wins the game. I have completed the requirements, but i wanted to create a pass by value function for "void Dice()", I'm not too sure how that works?
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//creating my variables and their values
int compinput;
[Code] .....
View 5 Replies
View Related
Jan 24, 2014
I am trying to simulate a random dice roll as a basis for a chutes and ladders game
Code:
int main {
int i = 0, diceroll;
while (i>5) {
diceroll = 1+rand()%6;
i++;
}
return 0;
}
However the numbers get don't seem to be random, it always starts off with 6,6,5.. then its random.
View 10 Replies
View Related
Nov 7, 2013
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
using namespace std;
class Dice {
[Code] ....
Every time the roll_dice function get call twice for the same object, i receive the same dice values.
View 4 Replies
View Related
Oct 28, 2013
I am trying to code and compile a program that requests a uses to first inpu the number of sides they wish for a dice to have and secondly input the number of dice throws they would like to calculate the value for. The program will print an error message and default the number of sides the dice has to 5 if the number entered is below 4. Finally it will print out a percentage figure for each value that results from all the the dice rolls.
I have attached my header, driver and class files below along with accompanying error messages below them
// file name Exercise.cpp
#include <iostream>
#include "Die.h"
using namespace std;
int main() // the main program begins here
{
char restart = 'y'; // declare variables
double numRolls = 0.0;
int diceTot = 0;
[Code] .....
View 5 Replies
View Related
Feb 7, 2015
My problem is that some of my conditions are not being triggered when the price level of a medium or large discount is smaller than the cost of the widget(defined as .40) An example being ,base price, $2 med discount 80%, and large discount 90%. My med discount is,1.6(80%) off of 2, so $.40, which is fine, but 90% off mean 1.8(90%) off of a base of $2, which is $.20 which is too low., and should trigger the 4th if statement, Nick you are discounting large purchases too much!. However when I run this in the program, I am triggering the first if statement.
#include <math.h>
#include <stdio.h>
#define widget_cost 0.40
int main (){
float sellprice;
float discount_med_price;
[Code] ....
View 2 Replies
View Related
Apr 20, 2013
Am i using boolean values correctly? our professor wanted boolean value use for the if and else statements.
Code:
#include <stdio.h>
#include <math.h>
#define GRAVITY 9.8
#define LIMIT 500
#define SEC 5
[Code] ....
View 2 Replies
View Related
Jan 29, 2014
I've just started learning the C language, and I'm stuck on something that is probably quite simple.how to get IF statements working within WHILE loops. My code is below. I am trying to write a program to count the number of words in a sentence, and obviously stop counting after a full stop has been entered. I created a variable called 'spaces' which will increase by one after the user enters a space. However, when the IF statement is in place, the loop never terminates, even if I enter a full stop. When I delete the IF statement, the loop functions correctly.
Code:
#include <stdio.h>
int main()
{
char x;
char a;
char y;
int spaces = 0;
}
[code]....
View 4 Replies
View Related
Jul 3, 2013
I've got a very simple but annoying problem.
if (letter3=="
"){
letter3==letter;
PrintCharacterLineEnd();
This is a code. the string "letter3" contains the string "
". What I want to happen is when, letter3 contains the text "
" I want to go to the function PrintCharacterLineEnd.
However, as of right now it's not working.
View 14 Replies
View Related
May 3, 2012
The problem with this code is that there are no errors showing, but when i compile the program, the if else statements do not carry out as they should do. Both if statements show the same answer, for example the second if statement. If I type Y or N then I get the same thing, 'You will now choose an event'. How do i get rid of this problem? Is char supposed to be used or string?
#include <iostream>
#include <iomanip>
#include<stdlib.h>
[Code].....
View 1 Replies
View Related
Jan 26, 2013
im a newbie C user and im having a little trouble in these for loop of mine im using. the first iteration is all fine but on the second and succesive iterations the first gets statement is skipped. im making a program that would ask the user to input multiple informations for atleast 5 people. i was also asked to use structures.. here is the code i have come up so far.. ive been stuck in it for like 3 hours now.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
}
[code]....
gets part for the line "Enter ID Number is skipped on the second iteration..
View 3 Replies
View Related
Apr 25, 2013
When we say that the statements within the braces are indented, we mean a tab character distance? For example if we have :
Code:
// K&R Style
void some_function(void)
{
<1 tab>int x=1;
<1 tab>printf("Inside the function");
while(x<3) {
<1tab><1tab>printf("Inside the loop");
x++;
}
return;
}
That is right?
View 3 Replies
View Related
May 20, 2013
Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char).
So does that mean switch statements can only test if variable == value and nothing more, like > < >= <= != etc... ? I tried to make a program to test this and it seems like switch statements are limited to == but I'm not sure, maybe I'm doing something wrong.
This is the program I tried to make to test this:
Code:
#include <stdio.h>
int main () {
int n;
[Code]....
So is it true that switch statements only work with the built in == operator? if that was the case then I would feel rather meh about switch statements.
View 7 Replies
View Related
Oct 20, 2014
I am in comp. Sci 1 at my school and I have to write a program that deals with gamer scores and the trophy they get according to expert level. For example Tom is a beginner and gets a gold medal if >=10000 a silver >= 7500 bronze if >= 5000 and no trophy if he score <5000 ! I have do this scoring for Beginner, Immediate and Expert.
Should I set up the beginning like this:
case'b':
case 'B':
if (score >= 10000 )
trophy= gold
if (score >= 7500)
trophy = silver
if (score >= 5000)
trophy= bronze
else = none
Not really sure how to go about solving this problem
View 4 Replies
View Related