2017-03-28 36 views
-1

讓一對夫婦改變..但仍然無法正常工作。現在會發生什麼,如果我輸入'y'沒有任何反應,程序就會凍結,如果輸入'n',它會繼續for循環。如何向每個玩家詢問他們是否需要另一張牌

這是我到目前爲止。玩家由我陣列的線代表,他們的手是cols。 CardtabCardHandTab是代表他們具有EX:1♥的牌的wchar,其餘的是包含每張牌的值的模擬陣列。

其實只是意識到他們都會得到一張卡,不管他們的答案,因爲一做,同時檢查條件下執行的代碼後,但並沒有解決我的問題..

void anotherCard(const wchar_t* CardTab[], int ValTab[], const wchar_t* CardHandTab[4][5], int ValHandTab[4][5]) 
{ 
    for (int i = 0; i < 4; i++) 
    { 
     char answer = 0; 
     wcout << "Player" << i + 1 << "Would you like another? y/n" << endl; 
     cin >> answer; 
     while (answer == 'y' || answer == 'Y') 
     { 
      int x; 
      do 
      { 
       x = rand() % 51 + 0; 
      } while (CardTab[x] == NULL); 

      CardHandTab[i][3] = CardTab[x]; 
      ValHandTab[i][3] = ValTab[x]; 
      CardTab[x] = NULL; 
      ValTab[x] = 0; 
     } 
    } 
} 
+0

移動代碼到while循環。 – NathanOliver

+0

I.e.一旦你進入外部循環,'answer'永遠不會改變。所以當然如果'answer =='y''(它應該在循環的頂部而不是底部進行檢查)將會永久循環。 – WhozCraig

+0

不是你在問什麼,我只是覺得我必須指出。蘭特在分銷和質量方面絕對糟糕。你應該使用現代的C++ [random](http://en.cppreference.com/w/cpp/numeric/random)工具。 –

回答

0
I forgot it needs a way to exit.. adding a break; fixed it 

    void anotherCard(const wchar_t* CardTab[], int ValTab[], const wchar_t* CardHandTab[4][5], int ValHandTab[4][5]) 
    { 
     for (int i = 0; i < 4; i++) 
     { 
      char answer = 0; 
      wcout << "Player" << i + 1 << "Desirez vous une autre carte? y/n" << endl; 
      cin >> answer; 
      while (answer == 'y' || answer == 'Y') 
      { 
       int x; 
       do 
       { 
        x = rand() % 51 + 0; 
       } while (CardTab[x] == NULL); 

       CardHandTab[i][3] = CardTab[x]; 
       ValHandTab[i][3] = ValTab[x]; 
       CardTab[x] = NULL; 
       ValTab[x] = 0; 
       wcout << CardHandTab[i][3]; 
       break; 
      } 
     } 
    } 
+0

也許你想用if()而不是while()。創建循環的唯一/毫無意義僅在第一次迭代時強制中斷。 – drescherjm

+0

'x = rand()%51 + 0;'只會給你51個可能的值(0 .. 50)。 – drescherjm

相關問題