2013-10-07 88 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