2013-10-07 71 views
-3

我在這裏有一個問題,如果有人可以幫我在這裏,真的很好。這是我第一次使用這個程序,所以不要去評判。基本的C++骰子游戲

#include <cstdlib> 
#include <iostream> 

using namespace std; 
int throw1, throw2, throw3, throw4; 
int bet1 = 100; 
int bet2 = 300; 
int bet3 = 500; 
int bet=(bet1, bet2, bet3); 
int deposit; 
int account; 

int main(){ 
    int count = 0; 

    while(count < 3){ 

     cin>>deposit; 

     while(deposit>5000 || deposit<0){    //Makes sure so that my deposit is between 0-5000 
      cout<<"deposit failed"<<endl; 
      cin>>deposit; 

     } 
     account = deposit; 
     cout<<"You have deposited" <<deposit<<"Swedish Krona"<<endl; 
     cout<<"and you have this much cash on your account"<<account<<"Swedish Krona"<<endl; 

     if (konto>499){     //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more 
      cout<<"please place your bet"<<endl; 
      cout<<"bet1=100, bet2=300, bet3=500"<<endl; 
      cin>>bet1; 
      cin>>bet2; 
      cin>>bet3; 
      account = (deposit - bet); 
      cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl; 
     } 
     else if(account>299){ 
      cout<<"please place your bet"<<endl; 
      cout<<"bet1=100, bet=300"<<endl;  
      cin>>bet1; 
      cin>>bet2; 
      account =(deposit - bet); 
      cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl; 
     } 
     else if(account>99){ 
      cout<<"please place your bet"<<endl; 
      cout<<"bet1=100"<<endl; 
      cin>>bet1; 
      cout<<"you have placed your bet"<<bet<<"Swedish Krona"<<endl; 
     } 

     while (account<100 || deposit>5000){ 
      cout<<"insufficient funds"<<endl; 
      cin>>deposit; 
      account=deposit; 
     } 

     { 
      cout<<"Throw dice"<<endl; 
      srand(time(0)); 
      Throw1 = rand() % 6 + 1; 
      Throw2 = rand() % 6 + 1; 
      Throw3 = rand() % 6 + 1; 
      Throw4 = rand() % 6 + 1; 
      cout<<"You rolled"<<Throw1<<endl; 
      cout<<"You rolled"<<Throw2<<endl; 
      cout<<"Computer rolled"<<Throw3<<endl; 
      cout<<"Computer rolled"<<Throw4<<endl; 
     } 
    } 
    count++; 

    system ("pause"); 
} 

所以這裏的一點是,由於某種原因,我總是打賭500,即使型投入1或BET2,我不知道如何解決這個問題。然後我的循環功能(int count 0; while(count < 3)count++)它開始無休止地循環,沒有我按任何東西,即使我在簡單的編碼中使用相同的循環功能,如只需鍵入一些cout < <的東西它的工作正常,但是當我在此代碼中使用它時,流失,有誰知道爲什麼會發生這種情況,如果有人能回答,將感謝,先進的感謝。

+7

如果您將變量重命名爲英文並翻譯註釋會很有幫助 –

+3

在執行程序時只需調用srand()一次。永遠不要把它放在一個循環中。 – drescherjm

+1

無法通過'int bet =(bet1,bet2,bet3)'來猜測你的意思。我建議您再次查看關於變量和賦值的基礎知識。 – molbdnilo

回答

5

你在下面的註釋中聲明你的代碼,(int count 0; while(count < 3)count++)看起來像一些奇怪的forwhile循環的混合物。請再次檢查您的C++教科書/在線教程,瞭解如何編寫正確的循環。

在您顯示的代碼中,在您的while循環中,您不修改count變量 - 因此,如果在循環之前計數爲< 3,它將永遠循環。你的代碼的縮進真的是令人誤解。我冒昧地對您的代碼進行了重新格式化 - 現在您應該看到count++聲明實際上超出了您主要的while循環!

如果您想要固定次數的操作,建議使用for循環,這會讓您很難忘記增量!

+0

是的,我看到這個也是,所以改變了它,現在它已經開始橫衝直撞了,它循環了我希望它循環的數量,但它無法讓我重做我的投注,我只需按下一些東西就可以了。自動,賭當我用這個代碼隨機我做它工作正常 – beastmode28

+0

告訴我們的程序輸出與您的輸入,以及你真正期望發生的事情,然後有人可能能夠幫助你 – codeling

9
int bet1 = 100; 
int bet2 = 300; 
int bet3 = 500; 
int bet=(bet1, bet2, bet3) 

最後一行將這樣的評價:100,300,500。結果逗號分隔的表達的名單將是最後的價值,這是500所以你的賭注變量將始終設置爲500

3

您增加count外部環路,所以它會總是爲零。無論是將它內環路(正確的縮進是關鍵!),或者可能使用for循環,而不是:

for (count = 0; count < 3; ++count) { ... } 
+0

我試過了,但沒有感謝 – beastmode28

0

一些建議,

  • 地方提示符存款(insattning)到一個函數
  • 將您的提示輸入函數
  • 在提示下注前檢查是否有足夠的錢
  • 將輸入輸入到字符串中,然後驗證輸入(未在下面完成,還)
  • 檢查該賭注有效(= 100,= 300,= 500,賭注< = KONTO)

下面是這些方便的功能,

#include <string> 
#include <cstdlib> 
#include <iostream> 

using namespace std; 
int kast1, kast2, kast3, kast4; 
int bet1 = 100; 
int bet2 = 300; 
int bet3 = 500; 
int bet=0; //assignment didn't make sense 
int insattning=0; 
int konto=0; 

//deposit 
int get_insattning() 
{ 
    int good = 0; 
    while(!good) 
    { 
     cout<<"deposit"<<endl; //prompt for deposit 
     cin>>insattning; 
     if(insattning>5000 || insattning<0)//Makes sure so that my deposit is between 0-5000 
     { 
      cout<<"insattning fel, var vänlig och gör rätt denna gången"<<endl; 
     } 
     else good = 1; 
    } 
    cout<<"du har nu satt in" <<insattning<<"kr"<<endl; 
    return insattning; 
} 

目前尚不清楚對我而言,你是否想要1次賭注100,300或500或3次賭注。這是第一個,

//bet 
int get_bet() 
{ 
    int good = 0; 
    int bet; 
    std::string validbets = ""; 
    if(konto<100){ cout<<"you need more money"; return 0; } 
    while(!good) 
    { 
     cout<<"var vänlig och placera ditt bet"<<endl; 
     if(konto>=100){ validbets = "bet1=100"; } 
     if(konto>=300){ validbets += ", bet=300"; } 
     if(konto>=500){ validbets += ", bet=500"; } 
     cout<<validbets<<endl; 
     cin>>bet; 
     if(bet >= konto) { 
      cout<<"you don't have enough money"<<endl; 
      continue; 
     } 
     if (bet==500){     //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more 
      cout<<"du har så här mycket på kontot nu "<<konto<<" kr"<<endl; 
      good = 1; 
     } 
     else if(bet==300){ 
      cout<<"du har så mycket på kontot nu "<<konto<<" kr"<<endl; 
      good = 1; 
     } 
     else if(bet==100){ 
      cout<<"du har nu bettat "<<bet<<" kr"<<endl; 
      good = 1; 
     } 
     else { 
      cout<<"you must place valid bet"<<endl; 
      continue; 
     } 
    } 
    return bet; 
} 

現在你的主要遊戲更清潔/更易於閱讀。我不知道勝利條件是什麼,或者支付,而且由於你的提示都沒有英文,我看不懂他們講下一步該怎麼做,

int main() 
{ 
    int count = 0; 
    int bet; 
    srand(time(0)); 
    for(count=0; (count < 3); count++) 
    { 
     konto = get_insattning(); 
     if (konto<100) 
     { 
      cout<<"du har inte nog med pengar, vänligen sätt in pengar"<<endl; 
      continue; 
     } 
     cout<<"och du har så här mycket i ditt konto "<<konto<<" kr"<<endl; 
     bet = get_bet(); 
     //when you bet, reduce konto by bet 
     konto = (konto - bet); 
     { 
      cout<<"slå tärningar"<<endl; 
      kast1 = rand() % 6 + 1; 
      kast2 = rand() % 6 + 1; 
      kast3 = rand() % 6 + 1; 
      kast4 = rand() % 6 + 1; 
      cout<<"Du fick"<<kast1<<endl; 
      cout<<"du fick"<<kast2<<endl; 
      cout<<"datorn fick"<<kast3<<endl; 
      cout<<"datorn fick"<<kast4<<endl; 
     } 

你需要編寫代碼來確定你是否贏得或丟失,然後添加到KONTO當你贏了,

 //did you win or lose? 
     //win? add money to konto 
     //lose? you have already deducted from konto 
    } 
    system ("pause"); 
} 

這些建議可以幫助你解決你的程序。

+0

非常感謝您的幫助,只是發生了一個問題,循環無休止地進行着。 – beastmode28

+0

看看您在每個循環中的終止條件。你可能會考慮(正如我所說),用允許「退出」或「退出」條目替換提示輸入。或者當輸入<0時退出?哦,你可以在cin上檢查EOF(文件結尾)。 – ChuckCottrill