根据用户输入或条件重新运行c ++程序

时间:2019-04-12 13:06:17

标签: c++ arrays loops

我正在学习c ++,作为数组和用户输入的练习,我试图编写一个简单的扑克应用程序。 由于我是本课程的开始,所以我对c ++语言的了解仅是main()函数需要执行代码。我已经写了几行代码,这些代码是最终应用程序的基础,现在可以正常工作。我想实现一个循环,以根据用户输入并在每次执行后应用程序范围为鱼类可变数量的条件下重新运行应用程序。我该如何实现?另一个问题是关于数组中随机元素的使用。有什么好的参考资料可供我学习如何做吗?

这是我的代码:

#include <iostream>
using namespace std;

int main(){
    string name;
    int bet;

    int fish = 100;
    char seed[4][10] = {"hearts","clubs","diamonds","spades"};
    int cards[9] = {2,3,4,5,6,7,8,9,10};

    std::cout << "Welcome in PokerBash! Please enter your name:" <<std::endl;
    std::cin >> name;

    std::cout << "Your name is " << name <<std::endl;

    std::cout << "You have a credit of:" << fish <<std::endl;
    std::cout << "Please enter your bet:" <<std::endl;
    std::cin >> bet;

    std::cout << "Your cards are " << seed[2] << " " << cards[3] << " " << seed[1] << " " << cards[7] <<std::endl;

    std::cout << "Your credits after this bet:" << fish - bet <<std::endl;

    return 0;
}

2 个答案:

答案 0 :(得分:4)

如果用户想要或fish小于0,则可以通过依赖于布尔值playing的{​​{3}}循环来停止循环最初为true。因此,如果发生两个事件之一,请将playing设置为false,循环将停止:

int main() {
    //variables

    bool playing = true;
    while (playing) {
        int fish = 100;

        //poker game

        if (fish < 0) { //no money
            playing = false;
        }
        else {
            char input;
            std::cout << "would you like to play again? (y/n): ";

            std::cin >> input;
            if (input != 'y') {
                playing = false;
            }
        }
    }
}

如您所见,这一直重复到我输入的不是'y'为止:

would you like to play again? (y/n): y
would you like to play again? (y/n): y
would you like to play again? (y/n): n

要从数组中选择随机元素,您可以像使用while一样使用<random>中的实用程序。要从数组中获取随机元素,您基本上只需创建一个随机数并将其用作数组索引:

#include <iostream>
#include <random>

int main() {
    std::random_device rd;
    std::mt19937_64 engine(rd());
    std::uniform_int_distribution<int> distribution(0, 8);


    int cards[9] = { 2,3,4,5,6,7,8,9,10 };
    while (true) {
        std::cout << cards[distribution(engine)] << '\n';
    }
}

一些重要的事情:

std::random_device rd;
std::mt19937_64 engine(rd());

仅执行一次(永不循环)。这是用于初始化伪随机生成器engine

  

std::uniform_int_distribution<int> distribution(0, 8);

添加一个发行版。请注意,由于您的int cards[9]具有9个元素,因此范围必须从08,因为数组从0开始并以其大小结尾- 1,您可能已经知道。 :)

运行此命令,您会看到它随机打印出从210的卡号:

2
10
7
9
2
4
9
10
8
9
8
6
8
2
10

这些是您进一步实施的帮助点。我还添加了一些有关您的代码的注意事项,但这对问题本身不是必需的。


您应该注意,您不应该use namespace std-您可以阅读std::mersenne_twister_engine原因。


此外,代替:

char seed[4][10] = { "hearts","clubs","diamonds","spades" };

使用:

std::string seed[4] = { "hearts","clubs","diamonds","spades" };

要使用here,请包含<string>标头。


您写了std::cin >> name;,但这不适用于带空格的字符串,例如:

Welcome in PokerBash! Please enter your name:
Stack Danny
Your name is Stack

要获取全名,请使用

std::getline(std::cin, name);

答案 1 :(得分:1)

尝试一下

#include <iostream>
using namespace std;

int main()
{
    string name;
    int bet;
    int fish = 100;

    char seed[4][10] = {"hearts", "clubs", "diamonds", "spades"};
    int cards[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

    while (1)
    {

        std::cout << "Welcome in PokerBash! Please enter your name ( Enter q to quit ):" << std::endl;
        std::cin >> name;

        if(name == "q")
            exit(0);

        std::cout << "Your name is " << name << std::endl;

        std::cout << "You have a credit of:" << fish << std::endl;
        std::cout << "Please enter your bet:" << std::endl;
        std::cin >> bet;

        std::cout << "Your cards are " << seed[2] << " " << cards[3] << " " << seed[1] << " " << cards[7] << std::endl;

        std::cout << "Your credits after this bet:" << fish - bet << std::endl;    
    }

    return 0;
}