•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Techies' Lounge section within the Tech Talk category of DaniWeb, a massive community of 455,974 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,766 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Techies' Lounge advertiser: Programming Forums
Views: 764 | Replies: 1
![]() |
| |
•
•
Join Date: Sep 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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?
#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?
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 980
Reputation:
Rep Power: 6
Solved Threads: 97
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
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
I am in mourning for my country.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Techies' Lounge Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
api asm assembly x86 programming hla demo blackjack blogger blogging c c++ ccna code coding competition compiler compilers computer debugging developer development errors evaluation framework gdata google handhelds high-performance innovation java languages mcse microsystems networking news object online open oriented phone planning platform practices programming python rim rss samsung software step sun tools tutorials xml
- need help correcting "blackjack" c++ game (C++)
- need help with game making (Java)
- Tkinter Button(s) (Python)
- Python - Card Class - Graphics Win (Python)
Other Threads in the Techies' Lounge Forum
- Previous Thread: flickering monitor
- Next Thread: Cannot Remote Desktop into computer.


Hybrid Mode