使用 while (!(cin ... 并得到闪烁提示
using while (!(cin ... and getting blinking prompt
我遇到的问题是当我执行程序时它停在 "you bet _"。当我取出 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 .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;
}
答案很简单。您搞砸了第一个 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 循环逻辑表达式中的代码将始终至少被调用一次。
我遇到的问题是当我执行程序时它停在 "you bet _"。当我取出 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 .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;
}
答案很简单。您搞砸了第一个 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 循环逻辑表达式中的代码将始终至少被调用一次。