2016-03-25 8 views
0

Das Problem, das ich habe, ist, wenn ich durch das Programm gehe es bei "Sie wette _" stoppt. Wenn ich die while (! Cin) Linie herausnehmen es funktioniert richtig.Ich bin mir nicht sicher, warum dies ausgeführt wird und die blinkende Aufforderung.Was fehlt mir? Vielen Dank.mit while (! (Cin ... und blinkend) prompt

Es ist dieser Block, der ausgeführt wird, wenn es nicht sein sollte, wenn ich es gut gebe . Anzahl

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

Hier ist das volle Programm:

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); 
      } 

        } 

} 

Sie es nicht glauben, relevant, aber hier ist die functions.cpp Datei

#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; 
} 

Antwort

0

Die Antwort ist recht einfach. Sie haben mit dem ersten Anruf cin durcheinander gebracht. Stattdessen sollte Ihr Code wie folgt aussehen:

Mit Ihrer vorherigen Version haben Sie den Benutzer immer zweimal nach Eingabe gefragt. Zuerst mit der Einzelleitung cin Anruf und dann in der While-Schleife. Deshalb blieb es stecken. Beachten Sie, dass der Code im logischen Ausdruck der while-Schleife immer mindestens einmal aufgerufen wird.

+0

Ohh die While-Schleife ist ein Cin Call? Macht jetzt Sinn. Ich dachte, es wurde nur aufgerufen, wenn cin aus irgendeinem Grund etwas anderes als ein int bekommt. Danke für die Antwort. –