Thread: Very simple dice game

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    3

    Very simple dice game

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    
    
    
    
    int main() {
    
    
        int dice1;
        int dice2;
        int dice3;
        int sumold;
        int sumnew;
        int runtime;
        char user_guess[10];
    
    
    
    
        runtime = 1;
        while(runtime==1) {
    
    
            srand(time(NULL));
            dice1 = (rand() % 6) + 1;
            dice2 = (rand() % 6) + 1;
            dice3 = (rand() % 6) + 1;
            sumold = dice1 + dice2 + dice3;
    
    
    
    
            printf("You have rolled:\n%d    %d    %d\nYour total is: %d\n", dice1, dice2, dice3, sumold);
            printf("\nWill your next total be higher, lower or the same (q to quit)? (h/l/s/q): ");
    
    
    
    
            while(runtime==1) {
                gets(user_guess);
                srand(time(NULL));
                dice1 = (rand() % 6) + 1;
                dice2 = (rand() % 6) + 1;
                dice3 = (rand() % 6) + 1;
                sumnew = dice1 + dice2 + dice3;
    
    
    
    
                printf("\nYou have rolled:\n%d    %d    %d\nYour total is: %d\n", dice1, dice2, dice3, sumnew);
                if((user_guess=="h" || user_guess=="H") && sumnew > sumold) {
                    printf("\nYOUR PREDICTION WAS CORRECT!!\n");
                }
                else if((user_guess=="l" || user_guess=="L") && sumnew < sumold) {
                    printf("\nYOUR PREDICTION WAS CORRECT!!\n");
                }
                else if((user_guess=="s" || user_guess=="S") && sumold == sumnew) {
                    printf("\nYOUR PREDICTION WAS CORRECT!!\n");
                }
                else if(user_guess=="q" || user_guess=="Q") {
                    runtime = 0;
                }
                else {
                    printf("\nYOUR PREDICTION WAS WRONG!!\n");
                }
                sumnew = sumold;
                printf("\nWill your next total be higher, lower or the same (q to quit)? (h/l/s/q): ");
            }
        }
        printf("Congratulations on completing the game!");
        return 0;
    }

    Supposed to be an easy challenge that I found in a Youtube tutorial (YouTube). I'm very new to C so its probably a simple mistake but i just can't get it to work. The program is intended to function like this:

    1. Print out 3 random dice numbers and a total
    2. Take input from the user as to whether the next 3 rolls will produce a sum that is higher/lower/same as previous roll (or quit)
    3. Print out 3 more random dice numbers and a total
    4. Let the user know if their prediction was correct or allow them to exit the game
    5. If the user stays, back to step 2

    The intended output is this:
    Code:
    You have rolled:
    1    1    2
    Your total is: 4
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): h
    
    You have rolled:
    3    5    3
    Your total is: 11
    
    YOUR PREDICTION WAS CORRECT!!
    OR this:

    Code:
    You have rolled:
    1    1    2
    Your total is: 4
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): q
    
    You have rolled:
    3    5    3
    Your total is: 11
    
    Congratulations on completing the game!
    But this is the output im getting with the code above:

    Code:
    You have rolled:
    1    1    2
    Your total is: 4
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): h
    
    You have rolled:
    3    5    3
    Your total is: 11
    
    YOUR PREDICTION WAS WRONG!!
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): l
    
    You have rolled:
    2    3    4
    Your total is: 9
    
    YOUR PREDICTION WAS WRONG!!
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): q
    
    You have rolled:
    1    6    5
    Your total is: 12
    
    YOUR PREDICTION WAS WRONG!!
    Im using codeblocks as an IDE and I tried the debugging function. The program seems to be skipping the "if" nest entirely except for the "wrong prediction part".
    First post on this forum so sorry if its too long or badly formatted. Thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    By using (user_guess=="q"), you're actually comparing the addresses, not the contents. To compare the contents, use strcmp().
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    Yesss, i got it to work by using strcmp, thank you so much for the help

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,231
    By the way, you should call srand only once in your program, typically near the start of the main function, as the standard pseudorandom number generator should normally only be seeded once.

    You must avoid the use of gets as it is inherently vulnerable to buffer overflow. fgets is a plausible alternative, though note that it stores the newline entered by the user if there is space for it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    Actually, that's one of the other things i couldn't get to work, i tried using the line:

    scanf("%c", user_guess)

    which made the program work for the first iteration, but skip the user input stage every alternate iteration after that. E.g:

    Code:
    You have rolled:
    6    3    5
    Your total is: 14
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): l
    
    You have rolled:
    5    3    3
    Your total is: 11
    
    YOUR PREDICTION WAS CORRECT!!
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q):
    You have rolled:
    3    1    4
    Your total is: 8
    
    YOUR PREDICTION WAS WRONG!!
    
    Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): s
    So it works first run, not the second, works for third run.

    Then i tried: fgets(user_guess, 2, stdin)
    which produced the same result.

    gets() was the only one i used that made it work every iteration. I changed the srand line to only happen at the start, thanks for the tip.
    Last edited by vinnie; 12-27-2017 at 06:55 AM. Reason: bad wording

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by vinnie View Post
    Actually, that's one of the other things i couldn't get to work, i tried using the line:

    scanf("%c", user_guess)

    which made the program work for the first iteration, but skip the user input stage every alternate iteration after that. E.g:
    There's an explanation and simple fix for that here: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-25-2010, 10:07 AM
  2. Help With Dice Game
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2009, 04:08 PM
  3. Help With Dice Game
    By CaliJoe in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2009, 04:35 PM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  5. Craps the Dice Game
    By ketchup57v in forum Game Programming
    Replies: 11
    Last Post: 12-01-2001, 04:53 AM

Tags for this Thread