Printing an array and more advanced questions

Hello,

INTRO

Im new to C++ and fairly new to programming in general. I figured a good exercise for me to become familiar with C++'s functionality and syntax would be to create a simple program with a few features based off poker. My first exercise is usually to parse my poker hands.

I know my code is very noobish. I know there are MUCH better ways to create a deck of cards rather than a huge array. However please ignore that. :P

PROBLEM 1

Can you please help me print my array to the console? At the moment it gives me a number. I dont understand where the error in logic is happening. Im assuming it is in my loop.

PROBLEM 2

My idea was to have the array shuffle itself and then deal 2 cards to 9 players, dealing a card to each player before the second round of dealing. I dont know how to get C++ to assign array contents to another variable.

In PERL, I would use the push command to do this. Can you please point out the function or the name of this term in programing so I can look it up myself?



CODE:


/*How many times can you be dealt cards before getting pocket aces
52 cards 9 seats
seat 1 is to left of dealer
shuffle cards array,
*/


#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <ctime>

using namespace std;

void card (vector <string> &deck, string suit)
{

for (int i = 1; i <= 13; i++)
{

string blah;

if (i == 1) blah = "A";
if (i < 10) blah = i;
if (i == 10) blah = "10";
if (i == 11) blah = "J";
if (i == 12) blah = "Q";
if (i==13) blah = "K";
blah += " " +suit;
deck.push_back (blah);
}
}

int main()
{
int pselection;
vector <string> p1;
vector <string> p2;
vector <string> p3;
vector <string> p4;
vector <string> p5;
vector <string> p6;
vector <string> p7;
vector <string> p8;
vector <string> p9;
vector <string> deck;

cout << "\nPlease select what seat you would like to sit in."
<< "\n(Correct inputs include a single digit numbers 1 through 9)"
<< endl;
cin >> pselection;

if (pselection > 9) {
cout << "\nYou have entered an incorrect input."
<< "\nPlease select what seat you would like to sit in."
<< "\n(Correct inputs include a single digit numbers 1 through 9)"
<< endl;
cin >> pselection;
}
else {
cout >> "One moment please while we shuffle the deck.";
}

//refers to the pointer to create the deck

card (deck, "H");
card (deck, "S");
card (deck, "C");
card (deck, "D");
return 0;
}
Last edited on
HI Ek0,

You cannot do what you want with and array of chars. A vector of strings will work better.

1
2
3
4
5
#include <vector>
#include <string>

typedef std::vector<std::string> Cards;
Cards cards;


Character strings are done with double quotes, not single quotes. Those can only be used to assign individual characters. I'm surprised it compiles.
closed account (S6k9GNh0)
You can't assign two char to a char variable. Each element of the array = 1 char variable.

Also, if it's a card game, there is always a set number of cards. Why is a vector needed. I would just make a small struct that defined its value and card type and then make an array of that which should be predefined. A vector shouldn't be needed.
Last edited on
Vector is always the better choice. "Shouldn't be needed?" Sigh. Arrays are prone to all sorts of errors that vectors just aren't. Arrays are low-level junk that C++ programmers no longer have to deal with unless they are doing low-level programming. And that stuff just sucks.
Thanks for the responses.

I noticed that char would not work but the fact that it compiled was strange. What is a better option to use in that case? Declaring it as a string and int does not work.

Do I need to completely change my methodology?


Thanks PanGalatic, Im looking into vectors now.
closed account (S6k9GNh0)
Yeah it's pretty prone to mistake if the array needs to move. But it doesn't...At all...It's a constant 52 at all times....

I would still go with my struct idea whether or not you choose vector or array.
Characters are represented by single quotes. I noticed PanGalactic noted that above incorrectly.

char bob = 'Y';

http://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx

I learned a little bit as well from here..
Last edited on
Eh??? What's wrong with this statement?

Character strings are done with double quotes, not single quotes. Those can only be used to assign individual characters.
closed account (S6k9GNh0)
I revised, and then revised again. I meant to say noted....Not that it's incorrect but since he was using a char variable he was supposed to use single quotes. :/
Topic archived. No new replies allowed.