我的do while循环是否由于此功能而不起作用?

我正在制作一个程序,要求用户猜测计算机正在考虑的数字1-100。

在程序的最后,当用户猜到正确的数字时,我试图让程序询问用户是否要再次播放(重新启动程序)。

To solve this, i tried using a do while loop & char repeat;. The loop is stretching from almost the beginning of the program, until the end, althought without success. Does anyone know what im doing wrong? Is it because of the function talfunktion, that the loop won't pass?

码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int talfunktion (int tal, int guess, int tries, char repeat);

int main () {

do {
    srand(time(NULL));
    int tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of 
    int guess; //guess is the guessed value of the user
    int tries = 0; // amount of tries it took until getting correct
    char repeat;


    printf("Psst, the right number is: %d \n", tal); // remove later, not relevant to uppg.

    printf("Im thinking of a number between 1 and 100, guess which!");
    printf("\nEnter: ");
    scanf("%d", &guess);
    guess = talfunktion(tal, guess, tries, repeat);

    getchar();
    getchar();
    return 0;

    }

    int talfunktion(int tal, int guess, int tries, char repeat) {
        do {
            if (guess < tal) {
                tries++;
                printf("\nYour guess is too low, try again!");
                printf("\nEnter: ");
                scanf("%d", &guess);
            }
            else if (guess > tal) {
                tries++;
                printf("\nYour guess is too high, try again!");
                printf("\nEnter: ");
                scanf("%d", &guess);
            }
        } while (guess > tal || guess < tal);

        if (guess == tal) {
            printf("\nCongratulations, that is correct!");
            tries++;
            printf("\nYou made %d attempt(s)", tries);
            printf("\nPlay Again? (y/n)");
            scanf("%c", &repeat);
    }
} while (repeat == 'y' || repeat == 'Y');


}



评论