Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze games gaming hard drive hardware hdmi internet laptop malware memory monitor motherboard netgear network printer problem ram random registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
C++ full deck problem

Reply  
Thread Tools
CodeNoob's Avatar
Junior Member with 3 posts.
 
Join Date: Sep 2006
Experience: Beginner
29-Sep-2006, 03:40 PM #1
C++ full deck problem
im trying to make a full deck of cards and i pretty much got it exept im trying to get it to output the suit as a symbol like ♣,♠,♥,♦ and it wont work my code is as follows:
/*"fulldeck"
programing by
John Wright*/

#include <iostream>


using namespace std;

struct card
{
char name;
int suit,value;
bool drawn;
};

int main()
{ card deck[53] ;
int i=0;
while (i<=39) // Makes Deck
{
deck[0+i].name='a'; //Type Of Card
deck[1+i].name='2';
deck[2+i].name='3';
deck[3+i].name='4';
deck[4+i].name='5';
deck[5+i].name='6';
deck[6+i].name='7';
deck[7+i].name='8';
deck[8+i].name='9';
deck[9+i].name='t';
deck[10+i].name='j';
deck[11+i].name='q';
deck[12+i].name='k';

deck[0+i].value=1; // Value Of Card
deck[1+i].value=2;
deck[2+i].value=3;
deck[3+i].value=4;
deck[4+i].value=5;
deck[5+i].value=6;
deck[6+i].value=7;
deck[7+i].value=8;
deck[8+i].value=9;
deck[9+i].value=10;
deck[10+i].value=11;
deck[11+i].value=12;
deck[12+i].value=13;

deck[i].suit=((i/13)+2); //Suit Of Card

deck[i].drawn=0; // The Number Of Cards Drawn or Not
i+=13;
}
i=0;

//Outputs Deck

i=0;
while (i<=51)
{
cout<< deck[i].name<<char(deck[i].suit)<<endl;
i++;
}





cin.get();
cin.get();
return 0;
}
problem is underlined please help!
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
29-Sep-2006, 05:49 PM #2
You're not initializing the deck properly and only setting four out of the 52 with a suit. Here is one way to solve it.

Code:
#include <iostream>
using namespace std;

#define CARDS 52
#define SUITS 4
#define CARDS_IN_SUIT (CARDS/SUITS)

struct card
{
	char name;
	char suit;
	int value;
	bool drawn;
};

int main()
{

	card deck[CARDS] ;
	int i = 0;
	for(int s=0; s<SUITS; s++)
	{
		for(int c=0; c<CARDS_IN_SUIT; c++)
		{
			switch (c + 1)
			{
			case 1:
				deck[i].name = 'a';
				break;
			case 10:
				deck[i].name = 't';
				break;
			case 11:
				deck[i].name = 'j';
				break;
			case 12:
				deck[i].name = 'q';
				break;
			case 13:
				deck[i].name = 'k';
				break;
			default:
				deck[i].name = (char)c + '1';
				break;
			}
			deck[i].suit = (char)(s + 3);
			deck[i].value = c + 1;
			deck[i].drawn = false;
			i++;
		}
	}

	//Outputs Deck

	i=0;
	while (i<CARDS) 
	{ 
		cout<< deck[i].name<<char(deck[i].suit)<<endl;
		i++; 
	}

	cin.get();
	cin.get();
	return 0;
}
blaqDeaph's Avatar
Senior Member with 881 posts.
 
Join Date: Nov 2005
Location: Down Under!
Experience: Enough
29-Sep-2006, 10:25 PM #3
Personally I'd prefer to solve this without so much clutter and switching functions. Just initialize an array of characters, like this:

char cname[] = "a23456789tjqk", then you can simply use:

deck[i].name = cname[c % 13];

rather than a bunch of switch statements. I thought it looks better (it may be slightly faster, but thats probably not noticable, unless you're initializing 1million decks. But both work.
__________________
Windows XP: 64-bit wanna-be OS with a 32-bit graphical shell for 16-bit extensions of a 8-bit patch to an 4-bit operating system originally coded for a 2-bit microprocessor, written by a company that can't stand 1 bit of competition.
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
29-Sep-2006, 10:58 PM #4
Agreed... I was trying to introduce him to more of the language

Best,
Dan
CodeNoob's Avatar
Junior Member with 3 posts.
 
Join Date: Sep 2006
Experience: Beginner
30-Sep-2006, 04:20 PM #5
Thank You Very Much i appreciate the help really!!!
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:31 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.