I was wondering how to assign the ace a value in a blackjack program and convert a number to a string. So far I have:

#include <iostream>
using namespace std;



struct card{
int value;
string face;//had this as a char, but need a string for 10
int suit;
};



int main()
{
card deck[52];
int cardIndex;


for(int suit=0; suit<4; suit++)//repeat for times for 4 suits
{
for(int i=0; i<13; i++)//13 cards in a suit
{
cardIndex = i+(suit*13);
//this calculates the spot in the cards
//based on what card and also what the suit is


//since we're already inside the suit at this point, assign it
deck[cardIndex].suit = suit;


//now assign the values
if(i<8)
{//for all cards under 10
deck[cardIndex].value = i+2;//move in multiples of 13
//plus 2 because the index is off and the cards start at 2


//at this point, we can assign the face as the value
//a problem we have, is that the value is a number, not a string
//how do i convert a number to a string???
//deck[cardIndex].face = ???;
}
else
{//for cards 10 through ace
deck[cardIndex].value = 10;
if(i==8)//we're at 10
{
deck[cardIndex].face = "10";
}
else if(i==9)//Jack
{
deck[cardIndex].face = "J";
}
}
}
}


return 0;

Am I doing anything wrong?

You might get more help if you'd posted this to the C++ forum.

As to storing face values based on your loop index, your if...else branching seems adequate.

As you proceed, the Ace will be a problem in scoring - you will have to consider it as either a 1 or an 11. It's not too hard, I give this as a first semester problem from time to time.

Please use some indenting and other whitespace to improve readability. That will also encourage others to help you.
Val

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.