rand() returns the same value after the first time

后端 未结 1 1820
后悔当初
后悔当初 2021-01-21 09:42

First, here\'s my code:

#include 
#include 
#include 
#include 

#define NUM_SUITS 4
#define NUM_R         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 09:50

    scanf("%d", &newcard); and scanf("%d", &stay); are a problem as the variables are bool, but the format specifier is for int. In 2 places, change like:

    // scanf("%d", &newcard);
    int t;
    scanf("%d", &t);
    newcard = !!t;
    

    3 functions return int but do not return anything. Change to void functions.

    // int main_hand() {
    void main_hand() {    
    

    There appears to be other logic issues too.
    1) Code sometimes exists without knowing if won/loss
    2) @Jongware comment above correct:

    Lastly: If the same random sequence is still coming up, strip code to a bare main srand time printf rand and see if that works. time(), if not working always returns -1.
    Or simple add the following and verify srand() called with different values.

     int main(void) {
       unsigned now = (unsigned) time(NULL);
       printf("now = %u\n", now);
       srand(now);
    

    0 讨论(0)
提交回复
热议问题