2012-02-15 17 views
0

私は単純なダイス用のこのコードを持っています。単位をベットしてすべてを賭けています...あなたが正しいとすれば、あなたが選んだサイコロの量を倍にしてください...あなたが間違っていても少し(あなたが選んだ数の範囲+選んだダイスの数で)あなたは何も失うことはありません。もしあなたが本当にあなたが失う...whileループは何らかの理由で何をしなければならないのですか

私は基本的に2つのことを念頭に置いたwhileループを持っています:ユーザーがBUを持っているか、試しに "no"または "No"を入力しなかった場合。 ..何らかの理由でそれはちょうどうまくいかない...笑。なぜどんなアイデア?賭けの仕組みが働いていれば、それはbetting.currentBU == 0であると認識しますが、whileループはただ反応しません。

#include <iostream> 
#include <string> 
#include <cstdlib> 
#include <time.h> 
#include <limits> 

using namespace std; 

struct Dices{ // structure containing all the dice related integers 
    int dice; 
    int total; 
    int choice; 
} Dices = {0,0,0}; 

struct betting{ // structure containing all the betting integers 
    int currentBU; 
    int bettedBU; 
} betting = {100, 0}; 


int DiceThrow(int dicechoice, int totalnum){ // a method for the dice being rolled 
    for(int i=1; i <= dicechoice;i++){ 
      totalnum = totalnum + (rand() % 6 + 1); //total number, repeated by the loop for every dice 
     } 
    return totalnum; 
} 

int winningbet(int dicea, int cBU, int bBU){ // in case the user guesses it right 
    std::cout << "Congratulations, you got it right! \n"; 
    cBU = cBU + (dicea * bBU); // give him money... 
    return(cBU); 
} 
int losingbet(int dicen, int totaln, int choicen, int cBU2, int bBU2){ //in case the user guesses wrong 
    if(choicen > (totaln+dicen) || choicen < (totaln+dicen)) // checks how wrong he is, if he's too off, he loses BUs 
     cBU2 = cBU2-bBU2; 
    else 
     std::cout << "you we're so close, you don't lose any BUs! \n"; //if he was really close, just let him know he was close 
    return(cBU2); 

} 

int main(){ 
    string decision; // decision if they want to keep playing or not 
    srand ((unsigned int)time(NULL)); 
    while(decision != "no" || decision != "No" || betting.currentBU != 0) // makes sure of the decision AND that he still has BUs 
    { 
     Dices.total = 0; 
     std::cout << "how many dice would you like to use? "; 
     std::cin >> Dices.dice; 
     std::cout << "how many How many BUs are you betting?(" << betting.currentBU << " BUs left) "; 
     std::cin >> betting.bettedBU; 
     if(betting.bettedBU > betting.currentBU){ // if he doesn't have enough BUs 
      std::cout << "Sorry, you don't have that many BUs..."; 
      std::cout << "Want to try again with a different amount?(Yes/No) "; 
      std::cin >> decision; 
     } 
     else{ 
      std::cout << "guess what number was thrown: "; 
      std::cin >> Dices.choice; 
      Dices.total = DiceThrow(Dices.dice, Dices.total); 
      if(Dices.choice == Dices.total){ 
       betting.currentBU = winningbet(Dices.dice, betting.currentBU, betting.bettedBU); 
       std::cout << "Want to try again?(Yes/No) "; 
       std::cin >> decision; 
      } else{ 
       std::cout << "Sorry, the number was " << Dices.total << "... better luck next time \n" ; 
       betting.currentBU = losingbet(Dices.dice, Dices.total, Dices.choice, betting.currentBU, betting.bettedBU); 
       if(betting.currentBU > 0){ 
        std::cout << "Want to try again?(Yes/No) "; 
        std::cin >> decision; 
       } 
      } 

     } 
    } 
    if(betting.currentBU == 0){ 
     std:cout << "sorry, you ran out of BUs..."; 
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    } 
    else{ 
     std::cout << "your final BU count is: " << betting.currentBU << "\n"; 
     std::cout << "Thanks for playing, see you next time! (Press ENTER to terminate...)"; 
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    } 
    return 0; 

} 
+0

と等しくない「いいえ」に等しく、currentBUいないかどうかを確認する必要がありますか?あなたの入力を認識しませんか?あなたはあなたが持っている問題を本当に定義するわけではありません。 – Pochi

答えて

3

これは想定されていませんする:

while(decision != "no" && decision != "No" && betting.currentBU != 0) 

私たちは、「なし」に決定等しくないと、正確にあなたの問題は何ですか0

+0

yessir ...私は||の論理を混乱させました。 && ...ありがとう;) –

3

あなたのテストはどの意志ループ限り、これらの3つのうちのいずれかに該当するとして、while (A || B || C)です。 decisionは同時に"no""No"の両方に等しいことはできませんので、これらの2つの等しくないテストの少なくとも1つは常に真ですので、ループは永遠にループします...

+0

答えをありがとう;)うん、私は間違いなく論理を混乱させた|| && ... smdhで –

関連する問題