Printing a vector of strings?

Hello,

How can I go about printing a vector of strings. For example, if I wanted to print the strings (cards) push_backed to vector p1.

I believe I have to make a for loop correct?


//How many times can you be dealt cards before getting pocket aces

#include <iostream>
#include <string>
#include <vector>
#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()
{
//Declarations

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;
int p10 = 1;

//Select seat player would like to sit in

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 || pselection < 1) {

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");

//shuffle

srand (time(NULL));
random_shuffle (deck.begin(), deck.end());

//Deal cards
if (p10 = 1){
p1.push_back (deck[0]);
p2.push_back (deck[1]);
p3.push_back (deck[2]);
p4.push_back (deck[3]);
p5.push_back (deck[4]);
p6.push_back (deck[5]);
p7.push_back (deck[6]);
p8.push_back (deck[7]);
p9.push_back (deck[8]);
p10 = 2;
}

else {
p1.push_back (deck[9]);
p2.push_back (deck[10]);
p3.push_back (deck[11]);
p4.push_back (deck[12]);
p5.push_back (deck[13]);
p6.push_back (deck[14]);
p7.push_back (deck[15]);
p8.push_back (deck[16]);
p9.push_back (deck[17]);
}

int who;
cin >> who;
return 0;
}

Last edited on
1
2
3
4
5
6
7
8
9
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;
Didn't it occur to you to put these in a vector of vectors?

It compiles fine for me.
Im very new and had no idea that you could do that. Also I am not trying to be efficient, I am attempting to grasp fundamentals. Ill work on efficiency later once I can actually get my vectors to print. :P

How can I go about printing a vector of strings. For example, if I wanted to print the strings (cards) push_backed to vector p1. I believe I have to make a for loop correct? I cannot find good examples of how to have the console output the elements of a vector.

I greatly appreciate your response and participation in my programing practice.
Last edited on
closed account (S6k9GNh0)
Well being efficient is part of fundamental learning. When your learn you need to think about which is better and why and by that you gain experience and smarts. It's not good to make bad practices at the beginning of programming.

Normally you can go through an array of strings like so:

1
2
3
4
for (int i = 0; i <= 9; ++i)
{
   std::cout << MyStringArray[i] << std::endl; //If you use a name like that, your bound to get shot.
}
Last edited on
Computerquip,

I understand your perspective. However besides Hello world this is my first C++ program. I have never taken a computer science class or what not... I thought it was a good idea to try and understand C++ syntax and basic features instead of attempting to memorize C++'s book of functions.

I dont know how placing multiple vectors within a single affects how I call upon the variables within it and wanted to avoid the issue if possible.

closed account (S6k9GNh0)
What I find myself doing is making a program for my math homework. I make an algorithm, ask my program a question and have it answer me. And then I find ways to optimize and make it stronger. Sometime's I'll dawdle over a function for minutes even hours trying to figure out the fastest most proper way to do something. By that I look techniques up and I implement them. I stick to standard known operations...
Thank sounds like an amazing exercise. Im going to do that. :)

Also I realized I need to use an iterator I believe... at least... However Im having difficulty having it print both cards of a player.
Topic archived. No new replies allowed.