0

I'm learning c++ and as an exercise with arrays and user input, I'm trying to write a simple poker application. Since I'm at the begin of this course, all I know about the c++ language is that the execution of the code is demanded to the main() function. I've write some lines of code that is the base of the final app, it works fine for now. I want to implement a loop to re run the app based on the user input and on a condition that for the scope of the app will be the amount of th fish variable quantity after every execution. How I can achieve this? Another question is about the use of random elements from an array. Is there any good reference where I can learn how to do this?

This is my code:

#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;
}
dwpu
  • 258
  • 3
  • 19
  • 1
    "_I want to implement a loop_" What's stopping you in doing that? If you don't know the syntax of loops in C++, consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Apr 12 '19 at 13:09
  • I know about the loops with the `for` or `while`, but I don't know how to implement it to re run the program. – dwpu Apr 12 '19 at 13:14
  • Why do you need to re-run the program, instead of re-running the logic of the program? – Algirdas Preidžius Apr 12 '19 at 13:27

2 Answers2

3

You can do a loop that stops if the user wants to or fish is less than 0 by making a while loop that depends on some boolean playing that is initially true. So if one of the two events happen, set playing to be false and the loop stops:

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;
            }
        }
    }
}

as you can see, this repeats until I enter something that isn't '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

to choose a random element from an array you would use the utilities from <random> like their std::mersenne_twister_engine. To get a random element from an array you would basically just need create a random number and use that as the arrays index:

#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';
    }
}

some important things from here:

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

is done only once (never in a loop). It is for initializing your pseudo random generator engine.

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

adds a distribution. Note that, because your int cards[9] has 9 elements, the range has to go from 0 to 8 as arrays start at 0and end at their size - 1, as you might probably already know. :)

Running this you can see it randomly prints out the card numbers from 2 to 10:

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

These are your helping points to implement further. I add some more things I noticed about your code but are not necessary to the question itself.


You should note that you should not use namespace std - you can read here why.


Also, instead of:

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

use:

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

To use std::string include the <string> header.


you wrote std::cin >> name; but this doesn't work for strings with spaces, like look here:

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

To get the full name, use

std::getline(std::cin, name);
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • Thanks for your suggestions. I'm trying with the `do(){ }while();` and it'a nice starting point, I will try your code to implement a more robust solution for my simple app. Since I need to show the cards of a poker game, is there a simple way also to check the user cards to output a win message ?I was thinking to use the `if()` `else` control structure to do this, but maybe it will be too complex? – dwpu Apr 12 '19 at 13:56
  • if you have to check for something you will have to use `if()` statements no matter what. But if it's 20 if statements in a row you probably did something wrong. Feel free to post your code here (with a pastebin link) for me to take a look at that approach. – Stack Danny Apr 12 '19 at 14:01
  • For now I didn't write this part of the code, I need first to do a list of the winning cards of a poker single manche. I've asked this as you had understood because I want to avoid a bigger `if()` structure, I think there is a better way to do this task. For every suggestion feel free to contact me in chat – dwpu Apr 12 '19 at 14:18
1

Try this,

#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;
}
Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19