Pergunta

I've been searching the site for possible answers to this problem, and although they're all similar they don't seem to be the exact same problem that I have, which is why I've been forced to open this question. SO I need to make a dice game that is supposed to roll 2 dice ranged from 1-6 and the user is supposed to guess what the number will be. The program then outputs the values of the die and reroll's if the guessed value isn't the real value of the 2 die. If it is then the program stops rolling the die and tells you how many rolls it took for the die to reach your guessed value.

For some reason my program keeps rolling the die over and over without stopping and I'm not exactly sure why. I tried testing it in a seperate program and have gotten even more confused as to why I still can't get different values even with srand() being called only once at the beginning of main.(I realized that, among a few other problems were what was wrong with the functions throwCalc1 and the unnecessary throwCalc2) If I try to place rand() outside a variable, I get different values, but if I put it within a variable the values stay the same. I've tried making the variable a function and it still doesn't work as the compiler gives me an error saying "initialization makes pointer from integer without a cast"

test function:

        int main(void)
    {
        srand(time(NULL));
        int i;

        int *throwCalc = rand() % 6 + 1;
        for(i = 0; i < 6; i++) {
            printf("value is: %d\n", *throwCalc);
        }
        return 0;
    }

original program:

    #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define _CRT_SECURE_NO_WARNINGS
#define MIN 2
#define MAX 12

int getInt(int min, int max) {
    int retry = 1;
    int value;
    char after;
    int cc;

    do {
        printf("Enter total sought \n"
               "Range must be within [%d - %d]", min, max);
        cc = scanf("%d%c", &value, &after);
        if(cc == 0) {
            printf("bad char or 0 input, please re-enter input");
            clear();
        } else if (after != '\n') {
            printf("Error:Trailing characters, please re-ente input");
            clear();
        } else if (value < min || value > max) {
            printf("Error: value outside of range, please re-enter input");
            clear();
        } else {
            retry = 0;
        }
    } while(retry == 1);

    return value;
}

void clear() {
    while (getchar() != '\n') {
        ; //intentional empty statement
    }
}

int throwCalc1() {
    int a = 1, b = 6, n;
    srand(time(NULL));
    n = a + rand() % (b + 1 - a);
    return n;
}

int throwCalc2() {
    int a = 1, b = 6, n;
    srand(time(NULL));
    n = a + rand() % (b + 1 - a);
    return n;
}

int throwResult(int input, int getcalc1, int getcalc2) {
    int i = 0;
    do {
        throwCalc1();
        throwCalc2();
        printf("Result of throw %d : %d + %d", i, getcalc1, getcalc2);
        i++;
    } while(input != getcalc1 + getcalc2);
    printf("You got your total in %d throws!\n", i);

    return 0;
}

int main(void)
{
    int input = getInt(MIN, MAX);
    int getCalc1 = throwCalc1();
    int getCalc2 = throwCalc2();

    printf("Game of Dice\n");
    printf("============\n");
    printf("hi number is: %d", input);
    throwResult(input, getCalc1, getCalc2);

    return 0;
}
Foi útil?

Solução 2

Right now you are calling throwCalc1() and throwCalc2() within your loop, but throwing away the results. You need to save those results in a pair of variables:

do {
    getcalc1 = throwCalc1();
    getcalc2 = throwCalc2();
    printf("Result of throw %d : %d + %d", i, getcalc1, getcalc2);
    i++;
} while(input != getcalc1 + getcalc2);

After you've done this, you might notice that getcalc and getcalc2 don't need to be parameters to that function - they can just be local variables within throwResult().

In addition, your throwCalc1() and throwCalc2() functions are identical, so you can remove one them and just call the remaining one twice.

Your test function should look like:

int main(void)
{
    srand(time(NULL));
    int i;
    int throwCalc;

    for(i = 0; i < 6; i++) {
        throwCalc = rand() % 6 + 1;
        printf("value is: %d\n", throwCalc);
    }
    return 0;
}

Outras dicas

You do this once at the top of main:

int getCalc1 = throwCalc1();
int getCalc2 = throwCalc2();

And then expect the values to update just by calling throwCalc1() & 2 again.

Besides fixing srand(), have throwCalc1 & 2 return values into local variables instead of passing something in.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top