2016-10-26 14 views
0

私の質問はこれですが、whileループでint値を保存するにはどうすればいいですか?私のコードはすべてギャンブルに関するもので、1,000から始まり、現金を最大額にしたいのですが、私が設定した元の値に戻します。whileループでint値を保存する方法は?

私のコードは、現在gampbling結果に応じてCashWまたはCashLのいずれかを表示するには、この(私はそれがどのように悪いで新しい笑っていないのです注意してください)あなたのコードで

#include <cmath> 
#include <stdio.h> 
#include <iostream> 
#include <ctime> 
#include <cstdlib> 
using namespace std; 


int main() 
{ 
    char again = 'Y'; 
    int test; 
    int yes; 
    int CashW; 
    CashW = 1000; 
    int CashL; 
    CashL = 1000; 
    int yLose; 
    yLose = 500; 
    int xCash; 
    xCash = 1000; 
    int xRan; 
    srand(time(0)); 
    xRan = rand() % 100 + 1; 
    cout << " Welcome to the Gambling Game!" << endl; 
     cout << " If the number is above 50 I win!" << endl; 
     cout << " If the number is below 50 you lose!" << endl; 
     while (again == 'y' || again == 'Y') 
     { 
     cout << " The Number I Choose Is: " << xRan << endl; 


     CashL = xCash - xCash - xCash; 
     CashW = xCash + xCash; 


     if (xRan < 50) { 
      cout << " You win, rats!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      cout << " The cash you have now is: " << CashW << endl; 
      cout << " Type 1 to play again, type 2 to close the game." << endl; 
      cin >> yes; 

     } 

     if (xRan > 50) { 
      cout << " I win, you lose!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      cout << " The cash you have now is: " << CashL << endl; 
      cout << " Type 1 to play again, type 2 to close the game." << endl; 
      cin >> yes; 

     } 

     if (yes == 1) { 
      cout << " Cool, a gambling man! Time to make some cash" << endl; 
     } 

    } 


} 
+1

あなたが唯一のプレーヤーのための現在の現金を保存するための変数を使用する必要があります。それを増やすか、勝ち負けのif文で減らしてください。 whileループ内で乱数生成を移動して、毎回異なる結果を得る必要があります。 –

答えて

1

です。

残念なことに、結果を印刷し、xCashに保存しないでください。だから、次の反復で、あなたは同じxCash値で再び始まります!

結果を表示する行のすぐ下にxCash = CashW;またはxCash = CashL;を追加すると、簡単に解決できます。

+0

これはおそらく 'xCash + = CashW'と' xCash - = CashL'です。 –

+0

@RemyLebeau実際には私は躊躇した。 OPによって表示されるテキストは次のとおりです。*「現金は...」*。だから私はそれが本当に増分ではなく合計であると仮定しました。コメントをいただきありがとうございます。誤解を招いた場合に備えて、OPの調整に役立ちます。 – Christophe

1

それぞれの勝敗の額をxCashに更新することはありません。各ループ反復で新しい乱数を生成しているわけではありません。ループ変数againを決して更新しないので、無限ループに陥ってしまいます。

ではなく、より多くのこのような何かを試してみてください:

#include <cmath> 
#include <stdio.h> 
#include <iostream> 
#include <ctime> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 
    const int CashW = 1000; 
    const int CashL = 1000; 

    int xCash = 1000; 
    int xRan; 
    char answer; 

    srand(time(0)); 

    cout << " Welcome to the Gambling Game!" << endl; 
    cout << " If the number is above 50 I win!" << endl; 
    cout << " If the number is below 50 you win!" << endl; 

    do 
    { 
     xRan = rand() % 100 + 1; 
     cout << " The Number I Choose Is: " << xRan << endl; 

     if (xRan < 50) { 
      cout << " You win, rats!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      xCash += CashW; 
      cout << " The cash you have now is: " << xCash << endl; 
     } 

     else if (xRan > 50) { 
      cout << " I win, you lose!" << endl; 
      cout << " The cash you started with was: " << xCash << endl; 
      xCash -= CashL; 
      cout << " The cash you have now is: " << xCash << endl; 
     } 

     else { 
      cout << " dang, a draw!" << endl; 
     } 

     cout << " play again? " << endl; 
     cin >> answer; 

     if ((answer != 'y') && (answer != 'Y')) { 
      cout << " All done? Come back again another time!" << endl; 
      break; 
     } 

     cout << " Cool, a gambling man! Time to make some cash" << endl; 
    } 
    while (true); 

    return 0; 
} 
関連する問題