2016-03-25 8 views
0

私が抱えている問題は、プログラムを通過するときに「あなたが賭ける_」で止まることです。while(!(cin)...と点滅しているプロンプト

これは私がそれを良いものにしてはならないときに実行されているこのブロックですここ数

while (!(cin >> bet) || bet < 0) 
{ 
     cout << "Bad input - try again: "; 
     cin.clear(); 
     cin.ignore(INT_MAX, '\n'); // NB: preferred method for flushing cin 
    } 

は完全なプログラムです:

Create a program to allow a user to play a modified “craps” games. The rules will be as follows: 

1) User starts with $50.00 as their total. 
2) Ask the user for their bet (maximum bet is their current total). 
3) Throw a pair of dice. 
    a) If the total value is 7 or 11, the user is an instant winner. The user has the amount bet added back into their total. Ask the user if they wish to play again and if so they start at step two above. 
    b) If the total value is 2, 3, or 12, the user is an instant loser. The user has the amount bet deducted from their total. Ask the user if they wish to play again and if so they start at step two above. 
    c) If the total is anything else, remember this total as the “point” and roll again. 
    i) If the new total is equal to the “point”, the user wins and the process is the same as winning in (a) above 
    ii) If the new total is a 7, the user loses. And the process is the same as losing in (b) above. 
    iii) If the new total is anything else the user must roll again and again try to match the “point” of the first roll. 


*/ 

// user input twice and closing out if user is out of money 

#include <iostream> 
#include "Header.h" 
#include <fstream> 


using namespace std; 

int main() 
{ 
    int money = 50; 
    int bet; 
    bool hasMoney = true; 
    bool wantsToPlay = true; 
    int roll; 
    char yesOrNo; 
    bool secondLoop = true; 
    InitDice(); 


    // Loop (user has money && they want to play) 
    while (hasMoney && wantsToPlay) { 
     // Get Bet 
     cout << "How much would you like to bet?" << endl; 
     cout << "$"; 
     cin >> bet; 
     cout << "You bet " << bet << endl; 
     while (!(cin >> bet) || bet < 0) // <<< note use of "short circuit" logical operation here 
     { 
      cout << "Bad input - try again: "; 
      cin.clear(); 
      cin.ignore(INT_MAX, '\n'); // NB: preferred method for flushing cin 
     } 

     if (money > 0) { 
      cout << "You ran out of money so we are done here." << endl; 
      hasMoney = false; 

     } 


     while (bet > money) { 
      cout << "Please bet below your current balance: $" << money << endl; 
      cout << "$"; 
      cin >> bet; 
     } 
       // Throw Dice 
     roll = ThrowDice(); 
     cout << "You rolled a " << roll << endl; 
     // if (Total is 7 or 11) 
     if ((roll == 7) || (roll == 11)) { 
      // Win 
      cout << "You win!!!" << endl; 
      // Add Bet to their money 
      money = bet + money; 
      cout << "Your current balance is now: $" << money << endl; 
      // Continue loop 

     //if (Total is 2 or 3 or 12) 
     } else if (((roll == 2) || (roll == 3) || (roll == 12))) { 
      cout << "You lose!" << endl; 
      money = money - bet; 
      cout << "Your current balance is now: $" << money << endl; 

     //if (Total is something else) 
     } else { 
      // Point is set to Total (money) 
      money = roll; 
      cout << "Your total has now been set to your roll which was " << money << endl; 
      // second loop 
          //Throw Dice 
      do { // cant be a DO while loop I think 
       cout << "Throwing the dice again."; 
       roll = ThrowDice(); 
       cout << " You got a " << roll << "." << endl; 
       //if (Total is Point) 
       if (roll == money) { 
        cout << "You win!!!" << endl; 
        //Win 
        //add bet to their money 
        money = bet + money; 
        //Back to the outside loop (asking if they want to play again) 
        cout << "Do you want to play again? (y/n): " << endl; 
        cin >> yesOrNo; 
        if ((yesOrNo == 'y') || (yesOrNo == 'Y')) { 
         cout << "They do want to play again :D" << endl; 
         wantsToPlay = true; 
         break; 
        } else { 
         cout << "Okay, thanks for playing" << endl; 
         wantsToPlay = false; 
         exit(0); 
        } 


       //if (Total is 7) 
       } else if (money == 7) { 
        //Lose 
        // Subtract their bet 
        cout << "You lose. i need to get to outside loop ehre" << endl; 
        money = money - bet; 
        break; 
        // Back to the outside loop 

       //if (Toal is something else) 
       } else { 
        cout << "Roll again? (y/n): " << endl; 
        char rollAgain; 
        cin >> rollAgain; 
        if (rollAgain == 'y' || rollAgain == 'Y') { 
        } else if (rollAgain == 'n' || rollAgain == 'N') { 
         cout << "Well, you are walking away with $" << money << ". Thanks for playing!" << endl; 
         exit(0); 
        } 

       } 



      } while (secondLoop); 
      } 

        } 

} 
それはここでは関係だが、考えてはいけない

は、答えは非常に簡単ですfunctions.cppファイル

#include <stdlib.h> // for random number functions 
#include <time.h> 
#include <ctype.h> 
#include <string> 



#include "Header.h" 

using namespace std; 


void InitDice() 
{ 
    srand (time (0)); 
} 

int ThrowDice() 
{ 
    return ThrowDie() + ThrowDie(); 
} 

int ThrowDie() 
{ 
    int  Value; 

    Value = rand(); // rand gives back a random number from 0 thru 32767 (known as RAND_MAX) 
    Value = (Value % 6) + 1; 
    return Value; 
} 

答えて

0

です。あなたは最初にcinコールと混乱しました。代わりに、あなたのコードは次のようになります:

cout << "How much would you like to bet?" << endl; 
cout << "$"; 
while (!(cin >> bet) || bet < 0) // <<< note use of "short circuit" logical operation here 
{ 
    cout << "Bad input - try again: "; 
    cin.clear(); 
    cin.ignore(INT_MAX, '\n'); // NB: preferred method for flushing cin 
} 
cout << "You bet " << bet << endl; 

あなたの以前のバージョンでは、常に入力のために2回入力を求められました。まず、単一行cinをコールしてwhileループを実行します。だからこそ、それは立ち往生した。 whileループの論理式のコードは常に少なくとも1回呼び出されることに注意してください。

+0

ああ、whileループはcinコールですか?今や意味をなさない私は何らかの理由でcinがint以外のものを取得した場合に呼び出されたと考えました。答えをありがとう。 –

関連する問題