我遇到的问题是,当我通过该程序时,它停在“你打赌_”。当我取出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 :(得分: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;
使用以前的版本,您总是要求用户输入两次。首先使用单行cin
调用然后在while循环中调用。这就是它被卡住的原因。请注意,while循环的逻辑表达式中的代码将始终至少调用一次。