2016-11-08 52 views
-1

當它運行中獎或損失從銀行獲得或添加後再次運行時,銀行被設置爲25美元的銀行而不是更新的銀行在while循環之外無法獲取int更新

int main() 
{ 
srand(time(0)); 
int bank = 25; 
int total; 

char answer; 

cout << "Come play Spin the Wheel. The wheel has numbers from 1-10." << endl 
    << "If you spin an even number you lose that amount. If you spin" << endl 
    << "an odd number you win that amount. You start with a 25$ bank." << endl; 
cout << "Your bank is $" << bank << ". Would you like to spin the wheel? (y/n):" << endl; 
cin >> answer; 

while (toupper(answer) == 'Y') 
{ 
    int num = rand() % 10 + 1; 

    if (bank <= 10) 
    { 
     cout << "Sorry you must have more than 10$ to play" << endl; 
    } 
else if (num % 2 == 0) 
    { 
    total = bank + num; 
     cout << "You spun a " << num << " and won $" << num << endl; 
     cout << "Your bank is now: $" << total << endl; 
    } 

    else 

    { 
     total = bank - num; 
     cout << "You spun a " << num << " and lost $" << num << endl; 
     cout << "Your bank is now: $" << total << endl; 
    } 
    cout << "Would you like to play Again (y/n) ?" << endl; 
    cin >> answer; 
} 





return 0; 
} 

當它運行時的輸贏採取或從銀行加入,然後再次運行該銀行重新設置爲25 $銀行沒有更新的銀行

+0

你在哪裏更新銀行? – RyanTCB

+0

它最初是cout <<「您的銀行現在是:$」<< bank + num << endl;但我試圖把它扔到一個單獨的變量下,這也不起作用 – Drewdinie

+0

在結尾cout <<「你想再次玩(y/n)嗎?」 << endl; \t \t cin >> answer;它再次運行while循環。對不起,我真的很新,沒有別的辦法再次運行遊戲,至少得知 – Drewdinie

回答

0

初始化銀行是25美元這個功能。在while循環內只有總數更新不是銀行。

另一個問題出現時,錢太少。我想你希望一次顯示你的消息,但是你被困在循環中,因爲它永遠不會被破壞。

0

我相信你的else if和else語句是倒退的。

num % 2 == 0 

表示數字是偶數,如果爲真。指示說,如果數量是偶數,你就會失去這筆錢。

銀行將永遠不會小於或等於10,因爲你永遠最初25後,其設定銀行什麼總是25

的總變量似乎是多餘的。也許增加或減去直接從銀行滾存的金額。

0

您需要設置

bank = total 

否則你永遠不會改變它的值