gpt4 book ai didi

c++ - 我如何在设定数量的回合后告诉我的应用程序 "stop running"?

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:35 27 4
gpt4 key购买 nike

所以我正在制作一款回合制骰子游戏,该游戏仿照这款名为“地下 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");
}
}

最佳答案

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

例如迭代一些代码 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() 来求解。

关于c++ - 我如何在设定数量的回合后告诉我的应用程序 "stop running"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43293796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com