Dice Roller

This is a discussion on Dice Roller within the C Programming forums, part of the General Programming Boards category; I seem to be having problems with a dice rolling program that i am making. It rolls 2 6-sided dice, ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    Dice Roller

    I seem to be having problems with a dice rolling program that i am making. It rolls 2 6-sided dice, and i can get the random numbers no problem, its just making the program display the actual face of the die. when i run the program, it shows 2 of each die, even though the random numbers are random

    heres the whole program, any ideas on how to fix the display part are greatly appreciated.
    Code:
    /*
     *  diceroller.c
     *  Dice Rolling Program
     *
     *  Created by Koenig, Robert on 5/6/08.
     *
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
    	printf( "Press enter to continue\n" );
    	getchar();
    	srand((unsigned)time(NULL)); 
    	int d1=rand()%6; 
    	int d2=rand()%6;
    	d1 = d1+1;
    	d2 = d2+1;
    	if (d1 == 1);
    	{
    		printf("+---------+\n");
    		printf("|         |\n");
    		printf("|   (-)   |\n");
    		printf("|         |\n");
    		printf("+---------+\n");
    	}
    	if (d1 == 2);
    	{
    		printf("+---------+\n");
    		printf("|(-)      |\n");
    		printf("|         |\n");
    		printf("|      (-)|\n");
    		printf("+---------+\n");
    	}
    	if (d1 == 3);
    	{
    		printf("+---------+\n");
    		printf("|      (-)|\n");
    		printf("|   (-)   |\n");
    		printf("|(-)      |\n");
    		printf("+---------+\n");
    	}
    	if (d1 == 4);
    	{
    		printf("+---------+\n");
    		printf("|(-)   (-)|\n");
    		printf("|         |\n");
    		printf("|(-)   (-)|\n");
    		printf("+---------+\n");
    	}
    	if (d1 == 5);
    	{
    		printf("+---------+\n");
    		printf("|(-)   (-)|\n");
    		printf("|   (-)   |\n");
    		printf("|(-)   (-)|\n");
    		printf("+---------+\n");
    	}
    	if (d1 == 6);
    	{
    		printf("+---------+\n");
    		printf("|(-)   (-)|\n");
    		printf("|(-)   (-)|\n");
    		printf("|(-)   (-)|\n");
    		printf("+---------+\n");
    	}
    	printf("\n");
    	if (d2 == 1);
    	{
    		printf("+---------+\n");
    		printf("|         |\n");
    		printf("|   (-)   |\n");
    		printf("|         |\n");
    		printf("+---------+\n");
    	}
    	if (d2 == 2);
    	{
    		printf("+---------+\n");
    		printf("|(-)      |\n");
    		printf("|         |\n");
    		printf("|      (-)|\n");
    		printf("+---------+\n");
    	}
    	if (d2 == 3);
    	{
    		printf("+---------+\n");
    		printf("|      (-)|\n");
    		printf("|   (-)   |\n");
    		printf("|(-)      |\n");
    		printf("+---------+\n");
    	}
    	if (d2 == 4);
    	{
    		printf("+---------+\n");
    		printf("|(-)   (-)|\n");
    		printf("|         |\n");
    		printf("|(-)   (-)|\n");
    		printf("+---------+\n");
    	}
    	if (d2 == 5);
    	{
    		printf("+---------+\n");
    		printf("|(-)   (-)|\n");
    		printf("|   (-)   |\n");
    		printf("|(-)   (-)|\n");
    		printf("+---------+\n");
    	}
    	if (d2 == 6);
    	{
    		printf("+---------+\n");
    		printf("|(-)   (-)|\n");
    		printf("|(-)   (-)|\n");
    		printf("|(-)   (-)|\n");
    		printf("+---------+\n");
    	}
    	printf("\n");
    	printf("Die 1 = %i\n", d1);
    	printf("Die 2 = %i\n", d2);
    	return 0;
    }

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Remove the semi-colon from the end of all the conditionals, eg here:
    Code:
    if (d1 == 1); /*<-- bad semi-colon!*/
    You may wish to consider shifting the repetitive code to a function and to replace all those ifs with a switch statement.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    10
    ill keep that in mind for when i do if-then-else statements in the future.
    thanks for the help

  4. #4
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Mind if I show off my dice drawing algorithm? Basically 11 lines to do what your entire program did, consisting of the drawdice function and the 2 array definitions above it.

    I'm not trying to brag here... no, yes I am.

    (I'm not really that big a jerk. Joking, 'k. Just wanted to share an alternative solution I came up with awhile ago.)
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 02:43 AM
  2. Dice Simualtion
    By sillyman in forum C Programming
    Replies: 2
    Last Post: 04-22-2008, 02:43 PM
  3. Replies: 9
    Last Post: 11-11-2007, 02:31 PM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  5. Dice Rolling Program help
    By courtesan in forum C Programming
    Replies: 3
    Last Post: 08-17-2005, 04:14 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21