在设定金额轮次后,我如何告诉我的应用程序“停止运行”?

时间:2017-04-08 11:57:01

标签: c++

所以我正在制作一个基于转弯的骰子游戏,模仿这个名为“地下chinchiro”的游戏,该游戏取自动画片“Kaiju”。我需要为我的程序设置一个限制,以便它只运行一定数量的轮次,  我只是编码的初学者,对于我在代码中看到的任何不寻常的事情感到抱歉。

#include <iostream>
#include <string>
#include <time.h>
using namespace std;

void roll_3_dice(int &dice1, int &dice2, int &dice3) 
{
    dice1 = rand() % 6 + 1;
    dice2 = rand() % 6 + 1;
    dice3 = rand() % 6 + 1;
    return;
}


int main()
{
int cash = 90000;
int wager; 
int r;


//dealer's die
int dealer1;
int dealer2;
int dealer3;

// your die
int mdice1;
int mdice2;
int mdice3;


for (int i = 0; i < 10; i++)
{
cout << "Wager up boy!"<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}

cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;

cout << endl;
cout << "Dealer will now roll the dice" << endl;

roll_3_dice(dealer1, dealer2, dealer3);


cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;

cout << "It's your turn to roll the dice." << endl;
cout << endl; 
cout << "Press any key to roll the dice" << endl;
cin >> r;

roll_3_dice(mdice1, mdice2, mdice3);

cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;


system ("pause");
 }
}

3 个答案:

答案 0 :(得分:0)

看看for循环。 For循环允许您运行代码进行一定数量的迭代。

e.g。迭代一些代码7次。

int number_of_iterations = 7;
for(int i = 0; i < number_of_iterations; i++) {
    // Your code that you would like to iterate over goes here.
 }

修改 正如OP所指定的那样(在下面的评论中),问题似乎是程序没有停止通过for循环的每次迭代从用户接收输入。

这可能有很多原因。我最好的猜测是stdin缓冲区不清楚,std::cin继续从缓冲区读入。这可以通过在读入您的输入之前调用cin.clear()来解决。

答案 1 :(得分:0)

是学习如何使用常量的时候......

定义一个做

const int max_round = 5;

while这么长的回合是<=而不是max_round

答案 2 :(得分:0)

你的问题很不清楚。编辑代码,找到发生问题的部分并仅粘贴该部分。

喜欢:

while(cin>>wager)
{
   if(condition fails) 
   { 
       break; 
   }
}