Saturday, May 9, 2009

Can somebody please help me with this C++ code?

#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;time.h%26gt;





using namespace std;





class cCard


{


public:


char face[13] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};


enum suit[4] = {"S", "H", "D", "C"};


int getSuit ();


int getFace();


void print();


};


int cCard::getSuit()


{


return suit;


}


int cCard::getFace()


{


return face;


}


void cCard::print()


{


std::cout%26lt;%26lt;face %26lt;%26lt;suit;


}





class cDeck


{


public:


void shuffle();


char Card[13][4];


void Deck;


};


void cDeck::shuffle()


{


int face, suit;


for(face = 2; face %26lt; 13; face++)


{


for(suit = 0; suit %26lt; 4; suit++)


Card[face][suit] = NULL;


}


}


void cDeck::Deck()


{


int card, suit, face;


int deck[deckSuit][deckFace];


card = 1 ;


for (suit = 0 ; suit %26lt;= deckSuit - 1 ; suit++ )


{


for (face = 0 ; face %26lt;= deckFace - 1 ; face++ )


{


deck[suit][face] = card ;


card++;


}


}


}





class cPlayer


{


public:


string cPlayer();


void printHand();


void print();


};


string cPlayer::cPlayer(string name, int stake)


{


playerName = name;


playerStake = stake;


}


void cPlayer::printHand()


{


std::cout%26lt;%26lt;Card%26lt;%26lt;"\tab"%26lt;%26lt;Card%26lt;%26lt;"\tab"%26lt;%26lt;C...


}


void cPlayer::print()


{


cout%26lt;%26lt;name %26lt;%26lt;" $" %26lt;%26lt;stake %26lt;%26lt;"\n";


}





class cGame


{


public:


bool addPlayer();


void play();


void deal();


void cardQuality();


void print();


};


bool cGame::addPlayer(string playerName, int playerStake)


{


std::cout %26lt;%26lt; "Enter your name: ";


std::cin %26gt;%26gt; playerName;


std::cout %26lt;%26lt; "\nHow much money you have for the game? ";


std::cin %26gt;%26gt; playerStake;


}


void cGame::play(int player[][2] , int shuffledCard[][13])


{


int sCard, sSuit, sFace, pSuit = 0 , pFace = 0;


static int cardStart = 1;





for (sCard = cardStart ; sCard %26lt;= cardStart + 4; sCard++ ) {


for (sSuit = 0 ; sSuit %26lt;= 3; sSuit++ ){


for(sFace = 0 ; sFace %26lt;= 12; sFace++ ){


if (shuffledCard[sSuit][sFace] == sCard ){


player[pSuit][pFace] = sSuit;


pFace++;


player[pSuit][pFace] = sFace;


pFace--;


pSuit++;








}


}


}


}


cardStart = sCard;





}


void cGame::deal(int playerHand[][2], const char *suit1[], const char *face1[])


{


int a , b;


for ( a = 0 ; a %26lt;= 4; a++ )


{


b = 0;


std::cout%26lt;%26lt;"%s"%26lt;%26lt;suit1[playerHand[a][b]]...


b = 1;


std::cout%26lt;%26lt;"%s "%26lt;%26lt;face1[playerHand[a][b]]);


}


}


void cGame::whatHand( int playerHand [][2] )


{





int x,y;


int positFace[5], positSuit[5];


int *fPtr = positFace, *sPtr = positSuit ;


int Facepass = 0;


int Suitpass = 0 ;


int straight = 0;


for ( x = 0 ; x %26lt;= 4 ; x++ )


{


y = 1 ;


*fPtr = playerHand[x][y];


fPtr++ ;


}


for ( x = 0 ; x %26lt;= 4 ; x++ )


{


y = 0 ;


*sPtr = playerHand[x][y];


sPtr++ ;


}





for ( x = 0 ; x %26lt;= 3 ; x++ )


{


for ( y = (x + 1); y %26lt;= 4 ; y++ )


{


if ( *(positFace + x ) == *(positFace + y ) )


{


Facepass++;


}


}


}


for ( y = 0 ; y %26lt;= 3 ; y++ )


{


for (x = 0 ; x %26lt;= 3 ; x++ )


{


if ( positFace[x] %26gt; positFace[x+1] )


{


Swap(%26amp;positFace[x],%26amp;positFace[x+1]);


}


}


}


for ( y = 0; y %26lt;= 3 ; y++ )


{


if ( positFace[y] == positFace[y + 1] - 1 )


{


straight++;


}


}


for ( y = 1; y %26lt;= 4 ; y++ )


{


if ( *positSuit == *(positSuit + y ) )


{


Suitpass++;


}


}


switch ( Facepass )


{


case 1 :


std::cout%26lt;%26lt;"This poker hand contains a pair\n";


break ;


case 2 :


std::cout%26lt;%26lt;"This poker hand contains two pairs\n";


break;


case 3 :


std::cout%26lt;%26lt;"This poker hand contains three of a kind\n";


break ;


case 4:


std::cout%26lt;%26lt;"This poker hand contains a full house\n";


break ;


case 6:


std::cout%26lt;%26lt;"This poker hand contains four of a kind\n";


break ;


default :


if ( Suitpass == 4 %26amp;%26amp; straight != 4)


{


std::cout%26lt;%26lt;"This poker hand contains a flush\n";





}


if ( straight == 4 %26amp;%26amp; Suitpass != 4)


{


std::cout%26lt;%26lt;"This poker hand contains a straight\n";


}


if ( straight == 4 %26amp;%26amp; Suitpass == 4)


{


std::cout%26lt;%26lt;"This poker hand contains a straight flush\n";


}


break;


}


}





int main()


{


cPlayer thisplayer(Play#1, 1000);


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(Play#1, deck);


deal(Play#1, suit, face);


whatHand(Play#1);


cPlayer thisplayer(Play#2, 1000);


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(Play#2, deck);


deal(Play#2, suit, face);


whatHand(Play#2);


cPlayer thisplayer(Play#3, 1000);


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(Play#3, deck);


deal(Play#3, suit, face);


whatHand(Play#3);


addPlayer thisplayer();


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(playerName, deck);


deal(playerName, suit, face);


whatHand(playerName);


return 0;


}

Can somebody please help me with this C++ code?
this is one issue


enum suit[4] = {"S", "H", "D", "C"}; %26lt;---not an int


int cCard::getSuit()


{


return suit; %26lt;--returning an int


}


******


additional


looked further down realized your next one also returned an int when what you had was a char
Reply:what's the problem?


Please help me rewrite this poker game C++ code and make it work...?

#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;time.h%26gt;





using namespace std;





class cCard


{


public:


char face[13] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};


enum suit[4] = {"S", "H", "D", "C"};


int getSuit ();


int getFace();


void print();


};


int cCard::getSuit()


{


return suit;


}


int cCard::getFace()


{


return face;


}


void cCard::print()


{


std::cout%26lt;%26lt;face %26lt;%26lt;suit;


}





class cDeck


{


public:


void shuffle();


char Card[13][4];


void Deck;


};


void cDeck::shuffle()


{


int face, suit;


for(face = 2; face %26lt; 13; face++)


{


for(suit = 0; suit %26lt; 4; suit++)


Card[face][suit] = NULL;


}


}


void cDeck::Deck()


{


int card, suit, face;


int deck[deckSuit][deckFace];


card = 1 ;


for (suit = 0 ; suit %26lt;= deckSuit - 1 ; suit++ )


{


for (face = 0 ; face %26lt;= deckFace - 1 ; face++ )


{


deck[suit][face] = card ;


card++;


}


}


}





class cPlayer


{


public:


string cPlayer();


void printHand();


void print();


};


string cPlayer::cPlayer(string name, int stake)


{


playerName = name;


playerStake = stake;


}


void cPlayer::printHand()


{


std::cout%26lt;%26lt;Card%26lt;%26lt;"\tab"%26lt;%26lt;Card%26lt;...


}


void cPlayer::print()


{


cout%26lt;%26lt;name %26lt;%26lt;" $" %26lt;%26lt;stake %26lt;%26lt;"\n";


}





class cGame


{


public:


bool addPlayer();


void play();


void deal();


void cardQuality();


void print();


};


bool cGame::addPlayer(string playerName, int playerStake)


{


std::cout %26lt;%26lt; "Enter your name: ";


std::cin %26gt;%26gt; playerName;


std::cout %26lt;%26lt; "\nHow much money you have for the game? ";


std::cin %26gt;%26gt; playerStake;


}


void cGame::play(int player[][2] , int shuffledCard[][13])


{


int sCard, sSuit, sFace, pSuit = 0 , pFace = 0;


static int cardStart = 1;





for (sCard = cardStart ; sCard %26lt;= cardStart + 4; sCard++ ) {


for (sSuit = 0 ; sSuit %26lt;= 3; sSuit++ ){


for(sFace = 0 ; sFace %26lt;= 12; sFace++ ){


if (shuffledCard[sSuit][sFace] == sCard ){


player[pSuit][pFace] = sSuit;


pFace++;


player[pSuit][pFace] = sFace;


pFace--;


pSuit++;








}


}


}


}


cardStart = sCard;





}


void cGame::deal(int playerHand[][2], const char *suit1[], const char *face1[])


{


int a , b;


for ( a = 0 ; a %26lt;= 4; a++ )


{


b = 0;


std::cout%26lt;%26lt;"%s"%26lt;%26lt;suit1[playerH...


b = 1;


std::cout%26lt;%26lt;"%s "%26lt;%26lt;face1[playerHand[a][b]]);


}


}


void cGame::whatHand( int playerHand [][2] )


{





int x,y;


int positFace[5], positSuit[5];


int *fPtr = positFace, *sPtr = positSuit ;


int Facepass = 0;


int Suitpass = 0 ;


int straight = 0;


for ( x = 0 ; x %26lt;= 4 ; x++ )


{


y = 1 ;


*fPtr = playerHand[x][y];


fPtr++ ;


}


for ( x = 0 ; x %26lt;= 4 ; x++ )


{


y = 0 ;


*sPtr = playerHand[x][y];


sPtr++ ;


}





for ( x = 0 ; x %26lt;= 3 ; x++ )


{


for ( y = (x + 1); y %26lt;= 4 ; y++ )


{


if ( *(positFace + x ) == *(positFace + y ) )


{


Facepass++;


}


}


}


for ( y = 0 ; y %26lt;= 3 ; y++ )


{


for (x = 0 ; x %26lt;= 3 ; x++ )


{


if ( positFace[x] %26gt; positFace[x+1] )


{


Swap(%26amp;positFace[x],%26amp;positFace[...


}


}


}


for ( y = 0; y %26lt;= 3 ; y++ )


{


if ( positFace[y] == positFace[y + 1] - 1 )


{


straight++;


}


}


for ( y = 1; y %26lt;= 4 ; y++ )


{


if ( *positSuit == *(positSuit + y ) )


{


Suitpass++;


}


}


switch ( Facepass )


{


case 1 :


std::cout%26lt;%26lt;"This poker hand contains a pair\n";


break ;


case 2 :


std::cout%26lt;%26lt;"This poker hand contains two pairs\n";


break;


case 3 :


std::cout%26lt;%26lt;"This poker hand contains three of a kind\n";


break ;


case 4:


std::cout%26lt;%26lt;"This poker hand contains a full house\n";


break ;


case 6:


std::cout%26lt;%26lt;"This poker hand contains four of a kind\n";


break ;


default :


if ( Suitpass == 4 %26amp;%26amp; straight != 4)


{


std::cout%26lt;%26lt;"This poker hand contains a flush\n";





}


if ( straight == 4 %26amp;%26amp; Suitpass != 4)


{


std::cout%26lt;%26lt;"This poker hand contains a straight\n";


}


if ( straight == 4 %26amp;%26amp; Suitpass == 4)


{


std::cout%26lt;%26lt;"This poker hand contains a straight flush\n";


}


break;


}


}





int main()


{


cPlayer thisplayer(Play#1, 1000);


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(Play#1, deck);


deal(Play#1, suit, face);


whatHand(Play#1);


cPlayer thisplayer(Play#2, 1000);


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(Play#2, deck);


deal(Play#2, suit, face);


whatHand(Play#2);


cPlayer thisplayer(Play#3, 1000);


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(Play#3, deck);


deal(Play#3, suit, face);


whatHand(Play#3);


addPlayer thisplayer();


thisplayer.print();


deck[4][13];


shuffle(deck, deckFace, deckSuit);


play(playerName, deck);


deal(playerName, suit, face);


whatHand(playerName);


return 0;


}

Please help me rewrite this poker game C++ code and make it work...?
Sorry dude no time.
Reply:what error did your compiler give you?

our song

Enum data type?

enum color {pink,blue};


enum color c;


why should we declare a variable of enum color as


enum color c;//stmnt1


and not as


color c;//stmnt 2


why is "enum" word required to declare a variable "c" of type


color? we already know that "color" is of enum type.


what was the purpose of choosing to keep stmnt1 over stmnt2


by the compiler designers?

Enum data type?
Depends on the language. In C#, C++. color c is perfectly valid.





In C, not so much. They work much like structures, which must be declared in the same way. With the move towards more readable code and OOP, it seems silly to declare variables with the enum keyword twice.


Which of the following statements allocates space for a variable of the defined enum type?

a. enum color {red, yellow, blue} paint;


b. enum color {red, yellow, blue};


c. enum { red, yellow, blue} paint;


d. both a and c

Which of the following statements allocates space for a variable of the defined enum type?
Hi !





My answer is "d. both a and c" , That's because in all the given three statements ( a , b , c) the enum definition is valid , matter of fact enum definition alone won't create memory space in memory , memory space will be allocated only if a variable is declared !


In this case options 'a' and 'c' declares enum variable and hence it get memory allocated !





Thanks and regards !


Prince M. Premnath
Reply:a, but maybe also d


Sql Server 2005 Enum Values?

Does anyone know if Sql Server 2005 supports a similar set of values like a C# enum? I don't want to have to create a table for 4 lookup values (e.g cricital, high, medium and low).

Sql Server 2005 Enum Values?
Well I am not sure about SQL Server 2005, but the primitive way which I can think of is to define your field as VARCHAR and impose a CHECK constraint on it to make sure only one of the lookup values you mentioned are entered.





I think you might also benefit from a user defined type here.





Hope this helps.
Reply:Relation database stores data in a single form - a table.


How shall you use these values?


You can use client application for specific purpose.


Using a for loop how to display the names of planets in order using the enum type in programming language C++?

If you define the enumerated values in the order you want, it's easy. Here's a not-so-clever example of what you might do:





#include %26lt;iostream%26gt;





using namespace std;





enum NUMS { BEGIN=0, ONE, TWO, THREE, END };





int main(int argc, char *argv[]) {


for (int i = BEGIN; i %26lt; END; i++) {


switch (i) {


case ONE: cout %26lt;%26lt; "1 "; break;


case TWO: cout %26lt;%26lt; "2 "; break;


case THREE: cout %26lt;%26lt; "3 "; break;


default: break;


}


}


cout %26lt;%26lt; endl;


return 0;


}





A better solution would be one that didn't depend on the enum being defined in the right order, and didn't have hardcoded names in the switch statement to associate the names to the enum values. You'll need to be more creative for that, but for what you're looking for today, the above code will probably suffice.

flower pots

Enum datatype?

enum color {pink,red};


enum color c = 10; //wrong


enum color c = (enum color) 10;//correct





why is the above typecasting allowed for a variable of enum datatype.


if i had to equate something to an integer, i would do the below:


int c;


c = 10; or


c = red;





if c is to be equated to both red and some integer value, c should be declared an integer . if it is going to hold only


the enum value like red or pink, it should be declared of enum type.





Pls illustrate an example when this scenario is valid:


enum color c = (enum color) 10;


and why it cant be substituted by simple int c = 10;





My point is typecasting should be disallowed.

Enum datatype?
When you try to do


enum color c = 10;


the compiler gets to that line of code and says "sorry, color can only be pink or red (which is really just 0 or 1)".





Everytime you typecast something, you're basically telling the compiler "trust me on this, i know what i'm doing", and the compiler just plays along and does as it's told. It may give you a warning, but that's usually about all.





As for why you'd ever assign an enum variable something that is outside of it's defined range... the only reason I can think of, is to make it undefined (ie. no color), but, it's more proper just to have an "undefined" in the enum.


In C# given a string naming an enum how do you get the type? Type.GetType doesn't seem to cut it!?

?

In C# given a string naming an enum how do you get the type? Type.GetType doesn't seem to cut it!?
Check out Enum.Parse() method:





http://msdn2.microsoft.com/en-us/library...


In C++ how do you convert a string to enum?

Lets say I have the code





Code:





enum sKind_t { S_BLUE, S_GREEN, S_YELLOW };








and





Code:





struct square_t { sKind_t kind; string name; };








And Had a file Like this


BLUE apple


GREEN square


GREEN apple


YELLOW Horse





You then write some code to split up the file into two seperrate strings for each line


example





string A= BLUE


string B = apple





how do you store the enum value via a string


so i want say





square_t.kind = A;


but this doesn't work


how do you get strings that are one of the correct values for an enum to work as an enum category?





you change string A to "S_" + A giving S_BLUE

In C++ how do you convert a string to enum?
As the other answerer has said you cannot directly convert from string to enum. What you can do is have a mapping between the strings and the enum. One simple way would be comparing the inputted string with a colour like BLUE, YELLOW, GREEN. If the string is one of these, then use the enum value of it, like S_BLUE, S_YELLOW, S_GREEN.





I do not understand what you are trying to do, but this may help.
Reply:Enum types are named integer constants. You cannot directly convert them to C-style strings, or vice versa.
Reply:I think you need a dictionary. You can use the STL %26lt;map%26gt; class which operates in logarithmic time or for constant time you can use a hash table.


(c#) How can I inherit my enum from another enum?

For example, the enum I want to inherit is:





public enum People


{


Anna,


Mel,


Howard


}





and my enum is:





public enum Lawyer


{


Dan,


Roy,


Diana


}





I want the enum Lawyer to inherit the enum People so that it can access Anna,Mel and Howard. So I somehow want it to look like this:





public enum Lawyer : People


{


Dan,


Roy,


Diana


}





So that Lawter.Anna, Lawyer.Mel and Lawyer.Howard is possible.


Is this possible or is there other ways to do this?

(c#) How can I inherit my enum from another enum?
Hello,





You cannot apply inheritance to an enum since an enum is a type. If you want to do the following ideology, you could create a class with constants, then apply inheritance since it is a class.





--------------


public class People


{


public const int Anna = 0;


public const int Mel = 1;


public const int Howard = 2;


}





public class Lawyer : People


{


public const int Dan = 3;


public const int Roy = 4;


public const int Diana = 5;


}





--------------





This would be immitating the enum structure, and you can allow inheritance. There is no way you can apply inheritance to enum types.





Good Luck





---


Update:





In enums, you can do myenum.ToString() to get back the name of that enum type. The way I showed above cannot do that unless you use reflection. Or use strings to represent each one.





I don't understand why your design is that way? Why can't you have a list of People? Instead of introducing types for each one. Your design seems to be flawed. Would be better to do a List%26lt;People%26gt; and have a People class that contains information about the specific person. Within the People class, you can introduce Properties that describe the person such as, Name, Age, HairColor, whatever you want. So if you already know the people in that application, you can do a InitializePeople() Method that adds a new person to the list then you can do whatever you please...





You can use any other datastructure as well such as Dictionary, Hashtable, etc ..





---


Update 2:





If you want to just access them while coding (the names) then just remove the inherited enums and do them normally. Your design is flawed cause you want to make a list of users as variables. Or just do a class with const string Amy = "Amy"

flower beds

C++ typedefs for struct, enum?

Is there really any benefit to using typdefs in the form:


typedef struct MyStruct { int field1, int field2 };


typedef enum MyEnum { Apple, Banana, Orange};


versus


struct MyStruct { int field1, int field2 };


enum MyEnum { Apple, Banana, Orange };

C++ typedefs for struct, enum?
not sure but it may depend on the compiler. you may need the "typedef" on some but not on others.
Reply:I dont believe that there is. It has been a long time since I was in C++ though.


Explain enum in c# programming,code,usage &...?

An enum is a way to group together a bunch of related constants of which the values (often) don't really matter --the important thing is that you're talking about DIFFERENT values that need to identified.





For example, I'm writing a program in which reviews are stored about DVD's, CD's and books. To identify the type of review, I'm using an enum:





enum ReviewType { None, DVD, CD, Book };





Whenever I need to store the type of the objct I'm reviewing, I don't need to use constant numbers (0, 1, 2, 3, ...) but I can use meaningful constants instead, making the code much easier to read:





switch (thisObject.ReviewType)


{


  case ReviewType.DVD: // Reviewing a DVD!


  .


  .


  break;





  case ReviewType.CD: // Reviewing a CD!


  .


  .


  break;





  case ReviewType.Book: // Reviewing a Book!


  .


  .


  break;





  default: // You forgot to initialize thisObject's ReviewType!


  .


  .


  break;


}





The advantage is, again, that it documents your code without actually having to put comments in the code (wouldn't the code above be just as understandable without the remarks?) without any loss of performance. Also: you get type checking for free, because even though all enums are translated into integers, they do all get a different type, so you can't assign one to another without some type of cast --so it makes your code a lot 'safer'.





I hope that helps!


C++ enum help?

CODE: http://pastebin.com/d74a71c0d





It's telling me that I can't use those brackets in x (line 75-77). How else I am supposed to use enum correctly?

C++ enum help?
There is no such thing as x[0], x[1] or x[2] in your code. Those are instances of array notation, and x is not an array.





x can be assigned any one of those three mediaType values at a time, but you can't reference all three with subscript notation.





Even if you declared mediaType x[3], you would still have to assign a value to any of the x[]'s for it to be valid.





Hope that helps.
Reply:i think that you need to assign values to your variables also put them on a separate lines. Maybe not. check out http://msdn2.microsoft.com/en-us/library...