Thread: Problem with Dice Program

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    Problem with Dice Program

    So i am trying to create a sort of dice game that when the numbers 7 or 11 are rolled on the first roll you win, but if a 2,3 or 12 are rolled you lose.
    But if any other number is produced the program jumps to the while loop and starts spitting out numbers until either a 7 is rolled or you roll the same number you started with.

    Here is my program;
    Code:
    #include<stdio.h>
    int main()
    {
        /*declare*/
        int D1;
        int D2;
        int sum, sum2, point;
        printf("Press Any Key To Start");
        getchar();
        srand(time(0));
        D1 = 1 + rand() % 6;
        D2 = 1 + rand() % 6;
        sum = D1 + D2;
        printf("\nRoll: %d\n", sum);
        if ( sum == 7)
            printf("\nYou Win");
        if (sum == 11)
            printf("\nYou Win");
        if (sum == 2)
            printf("\nYou lose");
        if (sum == 3)
            printf("\nYou lose");
        if (sum == 12)
            printf("\nYou lose");
        point = sum;
         while((sum2!=1)&&(sum2!=sum))
        {
            D1 = 1 + rand() % 6;
            D2 = 1 + rand() % 6;
            sum2 = D1 + D2;
            printf("  \nRoll: %d+%d=%d\n ", D1, D2, sum2);
        }
        if (sum2 == 7)
        {
            printf("You lose");
        }
        else 
    
    
        {
            printf("\nyou win");
        }
    }
    Now my problem is when the dice rolls and the first if statement is met it still jumps to the while loop instead of ending the program, and also for some reason the while loop doesnt like the;
    Code:
    if (sum2 == 7)
    printf("You lose");
    as when it prints 7 it completly ignores it.

    Any ideas?


    Thanks for your help in advance.
    Last edited by Lambarda; 05-25-2012 at 01:12 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while((sum2!=1)&&(sum2!=sum))
    Why are you comparing with 1, if you're looking for a 7 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    Okay
    i managed to get the while loop to accept the
    Code:
    if(sum2 == 7)
    { printf("You lose");


    Now i just need a 2nd opinion with how to stop the program if any of the inital if statement conditions are met.

    New code(minor adjustment)
    Code:
    #include<stdio.h>
    int main()
    {
    	/*declare*/
    	int D1;
    	int D2;
    	int sum, sum2, point;
    	printf("Press Any Key To Start");
    	getchar();
    	srand(time(0));
    	D1 = 1 + rand() % 6;
    	D2 = 1 + rand() % 6;
    	sum = D1 + D2;
    	printf("\nRoll: %d\n", sum);
    	if ( sum == 7)
    		printf("\nYou Win");
    	if (sum == 11)
    		printf("\nYou Win");
    	if (sum == 2)
    		printf("\nYou lose");
    	if (sum == 3)
    		printf("\nYou lose");
    	if (sum == 12)
    		printf("\nYou lose");
    	point= D1+D2;
    	while ((sum2 != 7) && (sum2 != sum))
    	{
    	
    		D1 = 1 + rand() % 6;
    		D2 = 1 + rand() % 6;
    		sum2 = D1 + D2;
    		printf("  \nRoll: %d+%d=%d\n ", D1, D2, sum2);
    		}
    		
    		if (sum2 == 7 )
    		{
    			printf("\nYou lose");
    		}
    		else if (point = sum2 )
    		{
    			printf("\nYou win");
    		}
    	}

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    4
    was a miss type when fiddling around, change it since

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    4
    Yay, i got it finished!
    here is my program for people interested in what i changed. ( all i had to add was return 0; not pretty but made it work :P)
    Code:
    #include<stdio.h>
    int main()
    {
    	/*declare*/
    	int D1;
    	int D2;
    	int sum, sum2, point;
    	printf("Press Any Key To Start");
    	getchar();
    	srand(time(0));
    	D1 = 1 + rand() % 6;
    	D2 = 1 + rand() % 6;
    	sum = D1 + D2;
    	printf("\nRoll: %d\n", sum);
    	if ( sum == 7)
    	{
    		printf("\nYou Win");
    		getchar();
    		return 0;
    	}
    	if (sum == 11)
    	{
    		printf("\nYou Win");
    		getchar();
    		return 0;
    	}
    	if (sum == 2)
    	{
    		printf("\nYou lose");
    		getchar();
    		return 0;
    	}
    	if (sum == 3)
    	{
    		printf("\nYou lose");
    			getchar();
    		return 0;
    	}
    	if (sum == 12)
    	{
    		printf("\nYou lose");
    		getchar();
    		return 0;
    	}
    	point = D1 + D2;
    	while ((sum2 != 7) && (sum2 != sum))
    	{
    		D1 = 1 + rand() % 6;
    		D2 = 1 + rand() % 6;
    		sum2 = D1 + D2;
    		printf("  \nRoll: %d+%d=%d\n ", D1, D2, sum2);
    	}
    	if (sum2 == 7 )
    	{
    		printf("\nYou lose");
    	}
    	else if (point = sum2 )
    	{
    		printf("\nYou win");
    	}
    }
    Thanks for the replies and help!

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Lambarda View Post
    Code:
    	else if (point = sum2 )
    	{
    		printf("\nYou win");
    	}
    }
    This doesn't do what you want. It will always be true if sum2 is not 7 but greater than 0. Remember that there's a difference between assignment (=) and an equality check (==)
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Lambarda View Post
    Code:
        if ( sum == 7)
        {
            printf("\nYou Win");
            getchar();
            return 0;
        }
        if (sum == 11)
        {
            printf("\nYou Win");
            getchar();
            return 0;
        }
        if (sum == 2)
        {
            printf("\nYou lose");
            getchar();
            return 0;
        }
        if (sum == 3)
        {
            printf("\nYou lose");
                getchar();
            return 0;
        }
        if (sum == 12)
        {
            printf("\nYou lose");
            getchar();
            return 0;
        }
    Do you know that C has boolean operators like and (&&) and or (||) so that you can simplify your 5 if-checks to just 2?

    Out of curiosity: Is this exercise from the book "C Programming - A Modern Approach" from K.N.King?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Roll Program
    By HeidiPagel in forum C Programming
    Replies: 7
    Last Post: 12-13-2010, 10:39 AM
  2. Blackjack And Dice Program
    By arda1404 in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2010, 11:37 AM
  3. Dice program getting 0s
    By domezy in forum C Programming
    Replies: 4
    Last Post: 10-27-2009, 03:52 AM
  4. Dice Rolling Program help
    By courtesan in forum C Programming
    Replies: 3
    Last Post: 08-17-2005, 03:14 PM
  5. rolling dice program
    By sunny2005 in forum C Programming
    Replies: 20
    Last Post: 03-21-2005, 04:45 PM