Thread: Dice Roll Program

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Talking Dice Roll Program

    I need to simulate a dice rolling program in plain C that rolls 10 times and then reports what the highest and lowest roll was....I've written a program to generate the two die but it will only run once. I tried a while statement
    (while numberRolls != 10) so that it would keep generating the rolls until it reached 10 rolls but it didn't work. Please help! I have an idea on how to get the highest and lowest roll but any help would be appreciated! THANKS!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Please show code.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Here is my code so far.....

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
       int stime;
       int ltime;
    
       ltime = time(NULL);
       stime =(unsigned) ltime/2;
       srand(stime);
    
        int die1 = rand()%6 + 1;
        int die2 = rand()%6 + 1;
        int numberRolls = 10;
        int userSelection;
        int count = 0;
        count++;
    
        printf("The dice will roll ten times and report what each roll was.\n");
        printf("Hit any number key when ready.\n");
        scanf("%d", &userSelection);
        
        while(numberRolls != 10) {
    
        for(numberRolls = 0; numberRolls < 10; numberRolls++);
        printf("Dice roll %d was %d and %d.\n", numberRolls, die1, die2);
        }
    
       return 0;
    }
    I tried taking the while statement out and it compiles but shows only roll #10.
    AHHHHH!!!!! With the while statement in there it only shows roll #1.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    at the bottom where the dice actually rolls, it's supposed to be count, die1, die2 instead of numberRolls that i used as the first integer.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your logic is all wonky. You initialize numberRolls to 10, then only perform what's in the while loop if numberRolls is not 10. Since this isn't the case, you do nothing. Generally a loop to do something x number of times looks something like this:
    Code:
    for (numberRolls = 0; numberRolls < 10; numberRolls++) {
        // put dice rolling code here
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
        
        while(numberRolls != 10) {
    
        for(numberRolls = 0; numberRolls < 10; numberRolls++);
        printf("Dice roll %d was %d and %d.\n", numberRolls, die1, die2);
        }
    While loops are an alternative to for loops, particularly useful when you have no idea how many times the program needs to loop.

    In your case, delete the while loop (it's useless), and just use the for loop. In order for the for loop to work, you need to remove the semi-colon from the end of it's first line of code:

    Code:
    for(numberRolls = 0; numberRolls < 10; numberRolls++);
    You never want a semi-colon on the end of a for loops first line (never is a very long time OK, but let's go with that anyway).

    You need logic inside your for loop. Imagine that you were doing this at the kitchen table. How would you do it? What would you need to keep track of, and what kind of data would that be?

    Make notes (on paper or mentally) of your steps to do something, and you'll have a way to code up a lot of problems in real life.

    What would you suggest here, for inside your for loop?

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    thank you so much!

    Thank you for the advice about the for statement....That's exactly why I couldn't get it to print more than once. The problem is now that it rolls ten times, it rolls the same exact two numbers each time. I'm also wanting to incorporate something that tells you your highest and lowest roll. I thought about trying min() and max() but not sure if that's what I need.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The problem is now that it rolls ten times, it rolls the same exact two numbers each time.
    That's because you only assign die1 and die2 one time, outside the for loop. They need to be calculated inside the for loop, so that you get a new random number and thus a new roll each time.

    I thought about trying min() and max() but not sure if that's what I need.
    Yes, that is essentailly what you need. The most common way is to do something like this:
    Code:
    max = 0;  // initialize max to something smaller than the smallest possible roll
    ...
    // inside your for loop
    if (current > max) { // if the current roll is greater than the current max
        max = current;   // set max to the current roll
    }
    And of course the opposite for tracking the minimum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. Dice program getting 0s
    By domezy in forum C Programming
    Replies: 4
    Last Post: 10-27-2009, 03:52 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. 1st real program in c++
    By nbpetts in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2006, 04:57 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM