Hi guys,
I am working on a project on playing cards using enum and class. I really need help with the function defination in this project,
Here i will attach the question, your help will be highly appreciated. You can get bac to me trough jeymineb at yahoo.co.uk.
Thanks.
If you can help, I can give you what Ii have and we can work it out. Thanks again.
Use enum and class to define a Card class. The Card class has two private variables: color and number and all necessary methods including overloaded operator >.

This is how the game is played: Your program asks if the player wants a card, if yes, your program will randomly draw a card (you need to use the random number generator function twice, once for the color and once for the number of card) and displays the player's card on screen. then your program draws a card for dealer and displays the dealer's card as well as the comparison result, such as you win or dealer wins. Use a loop so that the player can play another game if he chooses.

Note on random number generator function.

C++'s library has a rand( ) function you can call to generate a random number between 0 and maximum integer the system allows. It should be seeded with an integer value that is sent to srand(), i.e, you first call srand(seed); before calling the rand( ) function. seed is an integer of your choice. rand( ) % n returns a random number in the range between 0 and n-1 inclusive.

So to generate a card, you can use the following

int seed=234; //use any number you want;

srand(seed);

color=rand( )%4+1; //rand( )%4 generates a random number between 0 and 3, plus 1, then it is a random number between 1 and 4.

similarly you can get a card number.

I am working on a project on playing cards using enum and class. I really need help with the function defination in this project,
Here i will attach the question, your help will be highly appreciated. You can get bac to me trough jeymineb at yahoo.co.uk.
Thanks. If you can help, I can give you what Ii have and we can work it out. Thanks again.

If you ask a question here and want an answer from here, expect it here (it is considered rude to ask someone to privately provide an answer). This also allows potential respondents to gauge a response best suited to your question. Please post the code of your initial attempt (and remember to post the code within code tags).

Edited 3 Years Ago by happygeek: fixed formatting

QUOTE=Dave Sinkula: If you ask a question here and want an answer from here, expect it here (it is considered http://www.catb.org/~esr/faqs/smart-questions.html#noprivate]rude to ask someone to privately provide an answer). This also allows potential respondents to gauge a response best suited to your question.

Please post the code of your initial attempt (and remember to post the code within http://www.daniweb.com/techtalkforums/misc.php?do=bbcode#code code tags!

Hi,
Glad to know that. I was in a hurry and never got to read all rules at a go.
Here is what I already have. Trying to fix in parts
Thanks

#include <iostream>
const int declared_size=64;
using namespace std;
enum Suit { club, diamond, heart, spade};
enum Rank { two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};
class Card 
{
private:
  Suit color;
  Rank number;
public: 
  Card(){};
  void printcard(Card X);
  void SetSuit(Suit s ){ color = s; }
  void SetRank(Rank r ){ number = r; }
  void ShowCard(); 
  bool operator > (Card X);
};

int main()
{

  do {
    Card firstCard;
    Card secondCard;
    int seed=218;
    char start;
    cout<<"\n\n Do you want to draw a card"<<"\n Press Y for yes and n for no"<<end;
    cin>>start;
    if (start=='Y'||start=='y')
    { \\start of if



    } \\end of if

    else
    {
      cout<<"\n Thank you for playing"<<"\n Goodbye!!!"<<endl;
    } \\end of else

  } while (start=='y'||start=='Y');
  return 0;
}

int Card::operator > (Card X)
{
  if (number>X.number)return 1;
  else if (number<X.number)return 0;
  if (color>X.color) return 1;
  else return 0;
}

Code tags added. -Narue

Edited 3 Years Ago by pyTony: fixed formatting

Some touch-ups:

#include <iostream>
#include <cstdlib> // for rand(), srand()
using namespace std;

const int declared_size=64;

enum Suit
{
   club, diamond, heart, spade
};
enum Rank
{
   two = 2, three, four, five, six, seven, eight, nine, ten, 
   jack, queen, king, ace
};

class Card 
{
private:
   Suit color;
   Rank number;
public: 
   Card()
   {
   };
   void printcard(Card X);
   void SetSuit(Suit s )
   {
      color = s;
   }
   void SetRank(Rank r )
   {
      number = r;
   }
   void ShowCard(); 
   bool operator > (Card X);
};

int main()
{

   char start;
   int seed=218;
   srand(seed);
   do
   {
      Card firstCard;
      Card secondCard;
      cout<<"\n\n Do you want to draw a card"<<"\n Press Y for yes and n for no"<<endl;
      cin>>start;
      if ( start=='Y'||start=='y' )
      { //start of if



      } //end of if

      else
      {
         cout<<"\n Thank you for playing"<<"\n Goodbye!!!"<<endl;
      } //end of else

   } while ( start=='y'||start=='Y' );
   return 0;
}

bool Card::operator > (Card X)
{
   if ( number>X.number )return 1;
   else if ( number<X.number )return 0;
   if ( color>X.color ) return 1;
   else return 0;
}

THanks alot.
But I still have a problem with calling the seed when drawing the cards since one has to draw the cards twice, and check for both its rank and seed. How can I do that within the main body function.
Hope to hear from you soon.
Thanks again.

Some touch-ups:

#include <iostream>
#include <cstdlib> // for rand(), srand()
using namespace std;

const int declared_size=64;

enum Suit
{
   club, diamond, heart, spade
};
enum Rank
{
   two = 2, three, four, five, six, seven, eight, nine, ten, 
   jack, queen, king, ace
};

class Card 
{
private:
   Suit color;
   Rank number;
public: 
   Card()
   {
   };
   void printcard(Card X);
   void SetSuit(Suit s )
   {
      color = s;
   }
   void SetRank(Rank r )
   {
      number = r;
   }
   void ShowCard(); 
   bool operator > (Card X);
};

int main()
{

   char start;
   int seed=218;
   srand(seed);
   do
   {
      Card firstCard;
      Card secondCard;
      cout<<"\n\n Do you want to draw a card"<<"\n Press Y for yes and n for no"<<endl;
      cin>>start;
      if ( start=='Y'||start=='y' )
      { //start of if



      } //end of if

      else
      {
         cout<<"\n Thank you for playing"<<"\n Goodbye!!!"<<endl;
      } //end of else

   } while ( start=='y'||start=='Y' );
   return 0;
}

bool Card::operator > (Card X)
{
   if ( number>X.number )return 1;
   else if ( number<X.number )return 0;
   if ( color>X.color ) return 1;
   else return 0;
}

I meant one has to compare both the rank and suit in each draw between the players and the seed has to be called twice since each player has to draw a card. Can I kindly have the syntax for this?
Thanks

I meant one has to compare both the rank and suit in each draw between the players and the seed has to be called twice since each player has to draw a card.

I don't think so. You seed the generator once, and once only per program execution (generally); you call the rand() function for each draw.

Can I kindly have the syntax for this?

You already posted it.

I don't think so. You seed the generator once, and once only per program execution (generally); you call the rand() function for each draw.
You already posted it.

Hi,
here is how the seed is called twice: basically we have to draw two cards, one for the dealer and the other for the player hence have two calls. I will attach the program.

But I need some more clarification on how to use the static_cast in my main function : is there a way I can declare it globally so that I don't have to redefine every function? That is in the main body with switch (color) and switch(number). This is because on compiling the program, the static cast ommition erroris generated.
I really do appreciate your help.
Thanks

#include <iostream>
#include <cstdlib> 

using namespace std;

enum Suit
{
   club=1, diamond, heart, spade
};
enum Rank
{
   two = 2, three, four, five, six, seven, eight, nine, ten, 
   jack, queen, king, ace
};


class Card 
{
private:
   Suit color;
   Rank number;
public: 
   Card()
   {
   };
   void printcard(Card X);
   
   void SetSuit(Suit s )
   {
      color = s;
   }
   void getSuit(); //not used
 
   void SetRank(Rank r )
   {
      number = r;
   } 
   void getRank(); //not used
   void ShowCard(); 
   bool operator > (Card X);
   
};

int main()
{

    char start;
	int seed=4000;
	int color,number;
	
 Card luckycarda,lackycardb;
 
   
   do
   {
      
      cout<<"\n\n Do you want to draw a card"<<"\n Press Y for yes and n for no"<<endl;
      cin>>start;
	  
      if ( start=='Y'||start=='y')

	 
		int seed=4000;
			srand(seed);
			color=rand()%4+1;

	switch(color)
	{

	case 1:luckycarda.SetSuit(club);break;
	case 2:luckycarda.SetSuit(diamond);break;
	case 3:luckycarda.SetSuit(heart);break;
	case 4:luckycarda.SetSuit(spade);break;
	
		
	}
int seed=4000;
srand(seed);
number=rand()%13+1;
	switch(number)
	{
case 1:luckycarda.SetRank(2)break;
case 2:luckycarda.SetRank(3);break;
case 3:luckycarda.SetRank(4);break;
case 4:luckycarda.SetRank(5);break;
case 5:luckycarda.SetRank(6);break;
case 6:luckycarda.SetRank(7);break;
case 7:luckycarda.SetRank(8);break;
case 8:luckycarda.SetRank(9);break;
case 9:luckycarda.SetRank(10);break;
case 10:luckycarda.SetRank(jack);break;
case 11:luckycarda.SetRank(queen);break;
case 12:luckycarda.SetRank(king);break;
case 13:luckycarda.SetRank(ace);break;

}

	printcard(luckycarda);

	luckycarda=luckycardb;

	if((luckycarda.SetRank>luckycardsb.SetRank)&&((luckycarda.Setsuit>luckycardb.SetSuit)
		||(luckycarda.Setsuit==luckycardb.SetSuit)));
	{
	cout<<" YOU WIN....!!"<<"\n";
	else
	{
	cout<<"\n\n YOU PICKED"<<endl;
	}
else
{
cout<<"\n Thank you for playing"<<"\n Goodbye!!!"<<endl;
}
	
	
	}while(start=='Y'||start=='y');
	return 0;
}


int Card::operator > (Card X)
{
                        // first check the number
    if( number > X.number) return 1;

    else if(number < X.number)return 0;

    //now if the numbers are the same, check the suit
    if( color > X.color)return 1;
    else return 0;
}


void Card::ShowCard()
{
	cout<<"The is "<<color<<" "<<number<<endl;
}

void printcard(Card X)
{
	switch(X.color)
	{
	case club:	cout<<"Club ";
				break;
	case diamond:	cout<<"diamond "; 
					break;
			case heart:	cout<<"heart ";
				break;
			case spade:	cout<<"space ";
				
	}
	switch(X.number)
	{
	case two:	cout<<"2"; break;
	case three:	cout<<"3"; break;
	case four:	cout<<"4"; break;
	case five:	cout<<"5"; break;
	case six:	cout<<"6"; break;
	case seven:	cout<<"7"; break;
	case eight:	cout<<"8"; break;
	case nine:	cout<<"9"; break;
	case ten:	cout<<"10"; break;
	case jack:	cout<<"Jack"; break;
	case queen:	cout<<"Queen"; break;
	case king:	cout<<"King"; break;
	case ace:	cout<<"Ace"; break;
	}

<< moderator edit: added code tags: [code][/code] tags -- next time do so yourself >>

here is how the seed is called twice: basically we have to draw two cards, one for the dealer and the other for the player hence have two calls. I will attach the program.

That may make a rather dull game in which the same cards are always drawn.

#include <iostream>
#include <cstdlib> 
using namespace std;

int main()
{
   srand(4000);
   cout << rand() << endl;
   srand(4000);
   cout << rand() << endl;
   return 0;
}

/* my output
22441
22441
*/

This might be more interesting.

#include <iostream>
#include <cstdlib> 
using namespace std;

int main()
{
   srand(4000);
   cout << rand() << endl;
   cout << rand() << endl;
   return 0;
}

/* my output
22441
22791
*/

[edit]Here is somethe to look at.

#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;

int main()
{
   static const char *rank[] = 
   {
      "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
      "Jack", "Queen", "King", "Ace"
   };
   static const char *suit[] =
   {
      "Clubs", "Diamonds", "Spades", "Hearts"
   };

   cout << "View a deck of cards:\n";
   int card, r, s, i;
   for ( card = 0; card < 52; ++card )
   {
      r = card % 13;
      s = card / 13;
      cout << setw(5) << rank[r] << " of " << suit[s] << endl;
   }
   
   cout << "Choose several cards at random:\n";
   for (i = 0; i < 5; ++i)
   {
      card = rand() % 52;
      r = card % 13;
      s = card / 13;
      cout << setw(5) << rank[r] << " of " << suit[s] << endl;
   }
   return 0;
}

/* my (partial) output
Choose several cards at random:
  Two of Spades
Queen of Clubs
 King of Hearts
  Ten of Clubs
Eight of Hearts
*/

[/edit]

But I need some more clarification on how to use the static_cast in my main function : is there a way I can declare it globally so that I don't have to redefine every function? That is in the main body with switch (color) and switch(number). This is because on compiling the program, the static cast ommition erroris generated.

Huh?

Uh! That is great. Thanks for the new way I can view it. But your output displays all cards but all the program needs is just to draw one card and make comparison with the other. Well, how does that come in?
I have used the above for the main fuction of the program but still it issues an error on direct reference to private variables on the switch statement.....well this seems weard .
Check this out: at x.color and x.number.
Kindly elucidate on this. Thanks again

void printcard(Card X)
{

    switch(X.color)
    {
    case club:  cout<<"Club ";
                break;
    case diamond:   cout<<"diamond "; 
                    break;
            case heart: cout<<"heart ";
                break;
            case spade: cout<<"space ";

    }
    switch(X.number)
    {
    case two:   cout<<"2"; break;
    case three: cout<<"3"; break;
    case four:  cout<<"4"; break;
    case five:  cout<<"5"; break;
    case six:   cout<<"6"; break;
    case seven: cout<<"7"; break;
    case eight: cout<<"8"; break;
    case nine:  cout<<"9"; break;
    case ten:   cout<<"10"; break;
    case jack:  cout<<"Jack"; break;
    case queen: cout<<"Queen"; break;
    case king:  cout<<"King"; break;
    case ace:   cout<<"Ace"; break;
    }
    cout<<endl;
}

Edited 3 Years Ago by pyTony: fixed formating

Uh! That is great. Thanks for the new way I can view it. But your output displays all cards but all the program needs is just to draw one card and make comparison with the other. Well, how does that come in?

The first part shows a full deck. The second part draws random cards; this was meant to be a hint.

I have used the above for the main fuction of the program but still it issues an error on direct reference to private variables on the switch statement.....well this seems weard .

How about putting the switch and its output into printcard? Then you can access the data that is intended to be private and produce just such an error when you try to access it directly.

The first part shows a full deck. The second part draws random cards; this was meant to be a hint.

How about putting the switch and its output into printcard? Then you can access the data that is intended to be private and produce just such an error when you try to access it directly.

Hi,
This doen't work either:
Can you kindly compile this:You will see the error am talking about. Thank you. Try to see if it plays the cards: Hope you have the program question. Well, I am a bother but I love learning new things:)

#include <iostream>
#include <cstdlib> // for rand(), srand()
//#include <cmath>
using namespace std;

enum Suit
{
   club=1, diamond, heart, spade
};
enum Rank
{
   two = 2, three, four, five, six, seven, eight, nine, ten, 
   jack, queen, king, ace
};


class Card 
{
private:
   Suit color;
   Rank number;
public: 
   Card()
   {
   };
   void printcard(Card X);
   
   void SetSuit(Suit s )
   {
      color = s;
   }
   void getSuit(); //not used
 
   void SetRank(Rank r )
   {
      number = r;
   } 
   void getRank(); //not used
   void ShowCard(); 
   bool operator > (Card X);
   
};

int main()
{

    char start;
	int seed=4000;
	int color,number;
	
 Card luckycarda,lackycardb;
 
   
   do
   {
      
      cout<<"\n\n Do you want to draw a card"<<"\n Press Y for yes and n for no"<<endl;
      cin>>start;
	  
      if ( start=='Y'||start=='y')

	 
		int seed=4000;
			srand(seed);
			color=rand()%4+1;

	switch(color)
	{

	case 1:luckycarda.SetSuit(club);break;
	case 2:luckycarda.SetSuit(diamond);break;
	case 3:luckycarda.SetSuit(heart);break;
	case 4:luckycarda.SetSuit(spade);break;
	
		
	}
int seed=4000;
srand(seed);
number=rand()%13+1;
	switch(number)
	{
case 1:luckycarda.SetRank(2)break;
case 2:luckycarda.SetRank(3);break;
case 3:luckycarda.SetRank(4);break;
case 4:luckycarda.SetRank(5);break;
case 5:luckycarda.SetRank(6);break;
case 6:luckycarda.SetRank(7);break;
case 7:luckycarda.SetRank(8);break;
case 8:luckycarda.SetRank(9);break;
case 9:luckycarda.SetRank(10);break;
case 10:luckycarda.SetRank(jack);break;
case 11:luckycarda.SetRank(queen);break;
case 12:luckycarda.SetRank(king);break;
case 13:luckycarda.SetRank(ace);break;

}

	printcard(luckycarda);

	luckycarda=luckycardb;

	if((luckycarda.SetRank>luckycardsb.SetRank)&&((luckycarda.Setsuit>luckycardb.SetSuit)
		||(luckycarda.Setsuit==luckycardb.SetSuit)));
	{
	cout<<" YOU WIN....!!"<<"\n";
	else
	{
	cout<<"\n\n YOU PICKED"<<endl;
	}
else
{
cout<<"\n Thank you for playing"<<"\n Goodbye!!!"<<endl;
}
	
	
	}while(start=='Y'||start=='y');
	return 0;
}


bool Card::operator > (Card X)
{
                        // first check the number
    if( number > X.number) return 1;

    else if(number < X.number)return 0;

    //now if the numbers are the same, check the suit
    if( color > X.color)return 1;
    else return 0;
}


void Card::ShowCard()
{
	cout<<"The is "<<color<<" "<<number<<endl;
}

void printcard(Card X)
{
	switch(X.color)
	{
	case club:	cout<<"Club ";
				break;
	case diamond:	cout<<"diamond "; 
					break;
			case heart:	cout<<"heart ";
				break;
			case spade:	cout<<"space ";
				
	}
	switch(X.number)
	{
	case two:	cout<<"2"; break;
	case three:	cout<<"3"; break;
	case four:	cout<<"4"; break;
	case five:	cout<<"5"; break;
	case six:	cout<<"6"; break;
	case seven:	cout<<"7"; break;
	case eight:	cout<<"8"; break;
	case nine:	cout<<"9"; break;
	case ten:	cout<<"10"; break;
	case jack:	cout<<"Jack"; break;
	case queen:	cout<<"Queen"; break;
	case king:	cout<<"King"; break;
	case ace:	cout<<"Ace"; break;
	}
	cout<<endl;
}

And this is same as what you gave me, just displays cards ofn computer: Does not play them:

#include <iostream>
#include<iomanip>
using namespace std;

enum Suit { club, diamond, heart, spade};
enum Rank { two = 2, three, four, five, six, seven, eight, nine, 
		ten, jack, queen, king, ace};

		struct Card
		{
		Suit color;
		Rank number;
		};

		int main()
		{

			Card cards[52];
			cout<<"\n Show me the cards:\n\n";

			int i,j,cardp=0;
			for (i=0;i<4;++i)
				{
					for (j=2;j<15;++j)
					{
					cards[cardp].color=(Suit)i;
					cards[cardp].number=(Rank)j;
					++cardp;
					}

				}
			int temp_rank;
			cardp=0;	//reset index to zero
				for (i=0;i<4;++i)
				
				{
				  for (j=2;j<15;++j)
				  {

					temp_rank=cards[cardp].number;
					if(temp_rank<11) cout<<cards[cardp].number;
					else if(temp_rank==jack) cout<<"J";
					else if (temp_rank==queen) cout<<"Q";
					else if (temp_rank==king) cout<<"K";
					else cout<<"A";

					switch(cards[cardp].color)
					{
					case spade:cout<<char(6)<<" ";break;
						case heart:cout<<char(3)<<" ";break;
							case diamond:cout<<char(4)<<" ";break;
								case club:cout<<char(5)<<" ";break;

					}
					++cardp;
				}
				cout<<endl;
			}
			return 0;
		}

Can you kindly compile this:You will see the error am talking about. Thank you.

Which one?

MAKE Version 5.0  Copyright (c) 1987, 1997 Borland International
	c:\progra~1\lint\lint-nt.exe -Ic:\progra~1\lint std.lnt testpp.cpp -oo(testpp.lob)
PC-lint for C/C++ (NT) Ver. 8.00m, Copyright Gimpel Software 1985-2003

--- Module:   testpp.cpp
testpp.cpp 2: [537 Warning] Repeated include file 'C:\Borland\BCC55\Include\cstdlib.h'
testpp.cpp 25: [1401 Warning] member 'Card::color' (line 20) not initialized by constructor
testpp.cpp 20: [830 Info] Location cited in prior message
testpp.cpp 25: [1401 Warning] member 'Card::number' (line 21) not initialized by constructor
testpp.cpp 21: [830 Info] Location cited in prior message
C:\Borland\BCC55\Include\istream.h 238: [1055 Error] Symbol 'std::eof' undeclared, assumed to return int
testpp.cpp 58: [534 Warning] Ignoring return value of function 'std::operator>>(std::basic_istream<char,std::char_traits<char>> &, char &)' (compare with line 494, file C:\Borland\BCC55\Include\istream.h)
C:\Borland\BCC55\Include\istream.h 494: [830 Info] Location cited in prior message
testpp.cpp 63: [578 Warning] Declaration of symbol 'seed' hides symbol 'seed' (line 48)
testpp.cpp 48: [830 Info] Location cited in prior message
testpp.cpp 64: [539 Warning] Did not expect positive indentation from line 60
testpp.cpp 60: [830 Info] Location cited in prior message
testpp.cpp 64: [732 Info] Loss of sign (arg. no. 1) (int to unsigned int)
testpp.cpp 76: [744 Info] switch statement has no default
testpp.cpp 77: [18 Error] Symbol 'seed' redeclared (auto vs. auto) line 63
testpp.cpp 63: [830 Info] Location cited in prior message
testpp.cpp 77: [578 Warning] Declaration of symbol 'seed' hides symbol 'seed' (line 48)
testpp.cpp 48: [830 Info] Location cited in prior message
testpp.cpp 77: [31 Error] Redefinition of symbol 'seed' compare with line 63
testpp.cpp 63: [830 Info] Location cited in prior message
testpp.cpp 78: [525 Warning] Negative indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 78: [732 Info] Loss of sign (arg. no. 1) (int to unsigned int)
testpp.cpp 79: [525 Warning] Negative indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 82: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 82: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 82: [10 Error] Expecting ';'
testpp.cpp 83: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 83: [616 Warning] control flows into case/default
testpp.cpp 83: [825 Info] control flows into case/default without -fallthrough comment
testpp.cpp 83: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 84: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 84: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 85: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 85: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 86: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 86: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 87: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 87: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 88: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 88: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 89: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 89: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 90: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 90: [64 Error] Type mismatch (arg. no. 1) (int/enum)
testpp.cpp 91: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 92: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 93: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 94: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 96: [525 Warning] Negative indentation from line 80
testpp.cpp 80: [830 Info] Location cited in prior message
testpp.cpp 96: [744 Info] switch statement has no default
testpp.cpp 98: [725 Info] Expected positive indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 98: [1055 Error] Symbol 'printcard' undeclared, assumed to return int
testpp.cpp 98: [746 Info] call to function 'printcard()' not made in the presence of a prototype
testpp.cpp 98: [534 Warning] Ignoring return value of function 'printcard()' (compare with line 98)
testpp.cpp 98: [830 Info] Location cited in prior message
testpp.cpp 100: [725 Info] Expected positive indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 100: [40 Error] Undeclared identifier 'luckycardb'
testpp.cpp 102: [40 Error] Undeclared identifier 'luckycardsb'
testpp.cpp 102: [10 Error] Expecting a structure or union
testpp.cpp 102: [1013 Error] Symbol 'SetRank' not a member of class ''
testpp.cpp 102: [40 Error] Undeclared identifier 'SetRank'
testpp.cpp 102: [1013 Error] Symbol 'Setsuit' not a member of class 'Card'
testpp.cpp 102: [40 Error] Undeclared identifier 'Setsuit'
testpp.cpp 102: [40 Error] Undeclared identifier 'luckycardb'
testpp.cpp 102: [10 Error] Expecting a structure or union
testpp.cpp 102: [1013 Error] Symbol 'SetSuit' not a member of class ''
testpp.cpp 102: [40 Error] Undeclared identifier 'SetSuit'
testpp.cpp 103: [1013 Error] Symbol 'Setsuit' not a member of class 'Card'
testpp.cpp 103: [40 Error] Undeclared identifier 'Setsuit'
testpp.cpp 103: [40 Error] Undeclared identifier 'luckycardb'
testpp.cpp 103: [10 Error] Expecting a structure or union
testpp.cpp 103: [1013 Error] Symbol 'SetSuit' not a member of class ''
testpp.cpp 103: [40 Error] Undeclared identifier 'SetSuit'
testpp.cpp 103: [721 Info] Suspicious use of ;
testpp.cpp 104: [548 Warning] else expected
testpp.cpp 105: [725 Info] Expected positive indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 106: [42 Error] Expected a statement
testpp.cpp 108: [725 Info] Expected positive indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 110: [42 Error] Expected a statement
testpp.cpp 111: [525 Warning] Negative indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 112: [525 Warning] Negative indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 113: [525 Warning] Negative indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 116: [722 Info] Suspicious use of ;
testpp.cpp 117: [725 Info] Expected positive indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 118: [525 Warning] Negative indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 121: [525 Warning] Negative indentation from line 54
testpp.cpp 54: [830 Info] Location cited in prior message
testpp.cpp 121: [10 Error] Expecting 'while'
testpp.cpp 121: [78 Error] Symbol 'Card' typedef'ed at line 17 used in expression
testpp.cpp 17: [830 Info] Location cited in prior message
testpp.cpp 121: [61 Error] Bad type
testpp.cpp 121: [10 Error] Expecting ')'
testpp.cpp 122: [10 Error] Expecting ';'
testpp.cpp 122: [527 Warning] Unreachable code at token '{'
testpp.cpp 124: [40 Error] Undeclared identifier 'X'
testpp.cpp 124: [10 Error] Expecting a structure or union
testpp.cpp 124: [1013 Error] Symbol 'number' not a member of class ''
testpp.cpp 126: [40 Error] Undeclared identifier 'X'
testpp.cpp 126: [10 Error] Expecting a structure or union
testpp.cpp 126: [1013 Error] Symbol 'number' not a member of class ''
testpp.cpp 129: [40 Error] Undeclared identifier 'X'
testpp.cpp 129: [10 Error] Expecting a structure or union
testpp.cpp 129: [1013 Error] Symbol 'color' not a member of class ''
testpp.cpp 134: [527 Warning] Unreachable code at token 'void'
testpp.cpp 137: [1762 Info] Member function 'Card::ShowCard(void)' could be made const
testpp.cpp 141: [1060 Error] private member Card::color is not accessible to non-member non-friend functions
testpp.cpp 152: [1060 Error] private member Card::number is not accessible to non-member non-friend functions
testpp.cpp 169: [1746 Info] parameter 'X' in function 'printcard(Card)' could be made const reference
testpp.cpp 44: [533 Warning] function 'main(void)' should return a value (see line 44, file testpp.cpp)
testpp.cpp 48: [529 Warning] Symbol 'seed' (line 48, file testpp.cpp) not subsequently referenced
testpp.cpp 139: [529 Warning] Symbol 'printcard(Card)' (line 139, file testpp.cpp) not subsequently referenced
testpp.cpp 173: [10 Error] Expecting '}'
testpp.cpp 26: [754 Info] local structure member 'Card::printcard(Card)' (line 26, file testpp.cpp) not referenced
testpp.cpp 32: [754 Info] local structure member 'Card::getSuit(void)' (line 32, file testpp.cpp) not referenced
testpp.cpp 38: [754 Info] local structure member 'Card::getRank(void)' (line 38, file testpp.cpp) not referenced
testpp.cpp 40: [754 Info] local structure member 'Card::operator>(Card)' (line 40, file testpp.cpp) not referenced

--- Global Wrap-up

testpp.cpp 98: [526 Warning] 'printcard()' (line 98, file testpp.cpp) not defined
testpp.cpp 98: [628 Warning] no argument information provided for function 'printcard()' (line 98, file testpp.cpp)
testpp.cpp 139: [714 Info] Symbol 'printcard(Card)' (line 139, file testpp.cpp) not referenced
testpp.cpp 26: [1714 Info] Member function 'Card::printcard(Card)' (line 26, file testpp.cpp) not referenced
testpp.cpp 26: [1526 Warning] Member function 'Card::printcard(Card)' (line 26, file testpp.cpp) not defined
testpp.cpp 32: [1714 Info] Member function 'Card::getSuit(void)' (line 32, file testpp.cpp) not referenced
testpp.cpp 32: [1526 Warning] Member function 'Card::getSuit(void)' (line 32, file testpp.cpp) not defined
testpp.cpp 38: [1714 Info] Member function 'Card::getRank(void)' (line 38, file testpp.cpp) not referenced
testpp.cpp 38: [1526 Warning] Member function 'Card::getRank(void)' (line 38, file testpp.cpp) not defined
testpp.cpp 134: [1714 Info] Member function 'Card::ShowCard(void)' (line 134, file testpp.cpp) not referenced
testpp.cpp 40: [1714 Info] Member function 'Card::operator>(Card)' (line 40, file testpp.cpp) not referenced
testpp.cpp 40: [1526 Warning] Member function 'Card::operator>(Card)' (line 40, file testpp.cpp) not defined
C:\Borland\BCC55\Include\istream.h 238: [628 Warning] no argument information provided for function 'std::eof()' (line 238, file C:\Borland\BCC55\Include\istream.h)
Outputting to file testpp.lob
	c:\borland\bcc55\bin\bcc32 -c @MAKE0004.@@@
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
testpp.cpp:
Warning W8004 testpp.cpp 63: 'seed' is assigned a value that is never used in function main()
Warning W8006 testpp.cpp 82: Initializing Rank with int in function main()
Error E2379 testpp.cpp 82: Statement missing ; in function main()
Warning W8006 testpp.cpp 83: Initializing Rank with int in function main()
Warning W8006 testpp.cpp 84: Initializing Rank with int in function main()
Warning W8006 testpp.cpp 85: Initializing Rank with int in function main()
Warning W8006 testpp.cpp 86: Initializing Rank with int in function main()
Warning W8006 testpp.cpp 87: Initializing Rank with int in function main()
Warning W8006 testpp.cpp 88: Initializing Rank with int in function main()
Warning W8006 testpp.cpp 89: Initializing Rank with int in function main()
Warning W8006 testpp.cpp 90: Initializing Rank with int in function main()
Error E2268 testpp.cpp 98: Call to undefined function 'printcard' in function main()
Warning W8074 testpp.cpp 98: Structure passed by value in function main()
Error E2451 testpp.cpp 100: Undefined symbol 'luckycardb' in function main()
Error E2451 testpp.cpp 102: Undefined symbol 'luckycardsb' in function main()
Error E2316 testpp.cpp 102: 'Setsuit' is not a member of 'Card' in function main()
Error E2316 testpp.cpp 103: 'Setsuit' is not a member of 'Card' in function main()
Warning W8019 testpp.cpp 103: Code has no effect in function main()
Error E2054 testpp.cpp 106: Misplaced else in function main()
Error E2054 testpp.cpp 110: Misplaced else in function main()
Warning W8019 testpp.cpp 116: Code has no effect in function main()
Error E2308 testpp.cpp 121: do statement must have while in function main()
Error E2134 testpp.cpp 169: Compound statement missing } in function main()
*** 10 errors in Compile ***

;) Granted, a lot of that is just lint, but there a number of syntax errors you ought to fix.

I think what I'm saying is that if printcard is supposed to be a member function, write it as a member function -- don't pass it any parameters -- and display the information of the object. From my earlier anticipation of what your response might be, but without much modification:

#include <iostream>
#include <cstdlib>
#include <ctime>

class card
{
   int value;
public:
   card() : value(std::rand() % 52) {}
   void draw()
   {
      value = std::rand() % 52;
   }
   void show()
   {
      using std::cout;
      using std::endl;
      static const char *rank[] =
      {
         "2", "3", "4", "5", "6", "7", "8", "9", "10",
         "Jack", "Queen", "King", "Ace"
      };
      static const char *suit[] =
      {
         "Clubs", "Diamonds", "Spades", "Hearts"
      };
      cout << rank [ value % 13 ] << " of " << suit [ value / 13 ] << endl;
   }
};

int main()
{
   srand(time(0));
   card player, dealer;
   player.show();
   dealer.show();
   player.draw();
   player.show();
   return 0;
}

/* my output
Five of Diamonds
Queen of Hearts
Seven of Diamonds
*/

Below is the complete program for playing the cards: It compiles but as usual picks same cards over again: Problem 1. I need to cout a seed to avoid this problem, where can I locate that. I have tried out( seen in the program) to use if-else statements but they don't work:
2. Only the equals execution is displayed each time, is there a problem with the drwaing of cards?!

#include <iostream>
#include <cstdlib> // for rand(), srand()

using namespace std;

enum Suit
{
   club=1, diamond, heart, spade
};
enum Rank
{
   two = 2, three, four, five, six, seven, eight, nine, ten, 
   jack, queen, king, ace
};


class Card 
{
private:
   Suit color;
   Rank number;
public: 
   Card()
   {
   };
   friend void printcard(Card X);
  
   
   void SetSuit(Suit s )
   {
      color = s;
   }
   void getSuit(); //not used
 
   void SetRank(Rank r )
   {
      number = r;
   } 
   void getRank(); //not used
   void ShowCard(); 
   bool operator > (Card X);
   bool operator==(Card X);
};

int main()
{

		char start;
		int seed=4000;
		int color,number;
	
	Card luckycarda,luckycardb;
	//Luckycarda is the dealer
	//luckycardb is the player
 
   cout<<"\n\n Thank you for choosing to play cards. It is a pleasure to have you in my game.";
   
			do
				{
				
      
					cout<<"\n\n Do you want to draw a card"<<"\n Press Y for yes and n for no"<<endl;
					cin>>start;
					
					if  ((seed>0)||(seed<4000))
					{
				cout<<"\n\n  Enter the lucky number to pick a card...\n\n";
				cin>>seed;
				cout<<"\n";
					}
					else
					{
						cout<<" The number"<<seed<<" is out of range!!\n";
								cin>>start;
								abort();
					}

		if ( start=='Y'||start=='y')
				
	

				{
			
						srand(seed);
						color=rand()%4+1;

							switch(color)
						{

					case 1:luckycarda.SetRank(queen);break;
					case 2:luckycarda.SetSuit(diamond);break;
					case 3:luckycardb.SetRank(ace);break;
					case 4:luckycardb.SetSuit(club);break;
	
		
						}

						srand(seed);
						number=rand()%13+1;
							switch(number)
						{
					case 1:luckycarda.SetRank(two);break;
					case 2:luckycarda.SetRank(three);break;
					case 3:luckycarda.SetRank(four);break;
					case 4:luckycarda.SetRank(five);break;
					case 5:luckycarda.SetRank(six);break;
					case 6:luckycarda.SetRank(seven);break;
					case 7:luckycarda.SetRank(eight);break;
					case 8:luckycarda.SetRank(nine);break;
					case 9:luckycarda.SetRank(ten);break;
					case 10:luckycarda.SetRank(jack);break;
					case 11:luckycarda.SetRank(queen);break;
					case 12:luckycarda.SetRank(king);break;
					case 13:luckycarda.SetRank(ace);break;

						}
							srand(seed);
						color=rand()%4+1;

							switch(color)
						{

					case 1:luckycardb.SetRank(queen);break;
					case 2:luckycardb.SetSuit(diamond);break;
					case 3:luckycarda.SetRank(ace);break;
					case 4:luckycarda.SetSuit(club);break;
	
		
						}

						srand(seed);
						number=rand()%13+1;
							switch(number)
						{
					case 1:luckycardb.SetRank(two);break;
					case 2:luckycardb.SetRank(three);break;
					case 3:luckycardb.SetRank(four);break;
					case 4:luckycardb.SetRank(five);break;
					case 5:luckycardb.SetRank(six);break;
					case 6:luckycardb.SetRank(seven);break;
					case 7:luckycardb.SetRank(eight);break;
					case 8:luckycardb.SetRank(nine);break;
					case 9:luckycardb.SetRank(ten);break;
					case 10:luckycardb.SetRank(jack);break;
					case 11:luckycardb.SetRank(queen);break;
					case 12:luckycardb.SetRank(king);break;
					case 13:luckycardb.SetRank(ace);break;

						}

							cout<<"The dealer picked";
							printcard(luckycarda);
							cout<<endl;



						cout<<"The Player's card is";
						printcard(luckycardb);
						cout<<endl;


		
						

						if(luckycarda>luckycardb)
							{
								cout<<" YOU WIN....!!"<<"\n";
							}	
						else if (luckycarda==luckycardb)
							{
								cout<<" Both dealer and player have drawn the same card!"<<"\n";
							}
						else
							{					
						cout<<"\n\n YOU PICKED"<<endl;
							}
						
						
							//else
						//	{
								//cout<<" The number"<<start<<" is out of range!!\n";
								//cin>>start;
								//abort();
							//}

				}
			else
			{
				cout<<"\n Thank you for playing"<<"\n Goodbye!!!"<<endl;
			}
	
	
	}while(start=='Y'||start=='y');
	return 0;
}


bool Card::operator > (Card X)
{
                        // first check the number
    if(( number > X.number)&&(color > X.color)) return 1;

    else if((number < X.number)&&( color> X.color))return 1;
	else if (number>X.number)return 0;

    //now if the numbers are the same, check the suit
    if( color > X.color)return 1;
    else return 0;
}
bool Card::operator ==(Card X) 
{
 if( number == X.number) return 1;

    //now if the numbers are the same, check the suit
    else if( color == X.color)return 1;

    else return 0; 
}


void Card::ShowCard()
{
	cout<<"The is "<<color<<" "<<number<<endl;
}

void printcard(Card X)
{
	switch(X.color)
	{
	case club:	cout<<"Club ";
				break;
	case diamond:	cout<<"diamond "; 
					break;
			case heart:	cout<<"heart ";
				break;
			case spade:	cout<<"space ";
				
	}
	switch(X.number)
	{
	case two:	cout<<"2"; break;
	case three:	cout<<"3"; break;
	case four:	cout<<"4"; break;
	case five:	cout<<"5"; break;
	case six:	cout<<"6"; break;
	case seven:	cout<<"7"; break;
	case eight:	cout<<"8"; break;
	case nine:	cout<<"9"; break;
	case ten:	cout<<"10"; break;
	case jack:	cout<<"Jack"; break;
	case queen:	cout<<"Queen"; break;
	case king:	cout<<"King"; break;
	case ace:	cout<<"Ace"; break;
	}
	cout<<endl;
}

The seed cout part is in the main program after the first [do] statement

if  ((seed>0)||(seed<4000))
					{
				cout<<"\n\n  Enter the lucky number to pick a card...\n\n";
				cin>>seed;
				cout<<"\n";
					}
					else
					{
						cout<<" The number"<<seed<<" is out of range!!\n";
								cin>>start;
								abort();
					}

The == operator is at the end of the main fuction with the > operator.

Below is the complete program for playing the cards: It compiles but as usual picks same cards over again: Problem 1. I need to cout a seed to avoid this problem, where can I locate that. I have tried out( seen in the program) to use if-else statements but they don't work:
2. Only the equals execution is displayed each time, is there a problem with the drwaing of cards?!

  1. If you need to re-seed a random number generator, it is likely an indicator of a design flaw, IMO.

    A seed is meant to be planted and left to grow and do it's thing. It is not meant to be driven around like a car.

    If you have a deck of cards, draw a card. It will likely be one of the cards.

    If you stack the deck, and specify exactly where you cut the cards, it's not very fair.

  2. I have no 2.

HI Guys, I have a question on Multidemensional arrays in matrices:

The question needs one to write a program that of a two 3by3 matrice that adds and also multiplies the two matrices together to display a reulting matrix
Below is my program:

/* Write a C++ program that reads two 3x3 matrices, 
multiply these two matrices together and  displays 
the resulting matrix.*/

#include <iostream>
using namespace std;


void Times(int A[][3], int B[][3],int C[][3]);//prototype int first, int last);
			   
			 


int main()
{
	int N[3][3];
	int M[3][3];
	int K[3][3];
	int i, j;

	{
	
	cout<<"Enter a three by three matrix for first array"<<endl;
	
		
	for(i=0; i<3;i++)//for rows
		{
			for(j=0;j<3;j++)//for columns
		
			cin>>N[i][j];//"\t";
		}
		cout<<" Martix for array N is:\n\n";

		for(i=0; i<3;i++)
			{
				for(j=0;j<3;j++)
			
		
				cout<<N[i][j]<<"\t";
				cout<<endl;
			}
	}

		
//cout<<"Matrix N is:"<<"\n";
	

 cout<<"Enter a three by three matrix for second array"<<endl;

		for(i=0; i<3;i++)
		
		{
			for(j=0;j<3;j++)
		
				cin>>M[i][j];
		}

		cout<<" Martix for array M is:\n\n";

		for(i=0; i<3;i++)
			{	
			for(j=0;j<3;j++)
		
				cout<<M[i][j]<<"\t";
			cout<<endl;
		
		}
		for(i=0;i<3;i++);
		{
				for(j=0;j<3;j++);
		}
				cout<<"The product is:"<<Times(K,3,3)<<endl;
		

	//	cout<<endl;
	

	return 0;
}

void Times(int A[][3], int B[][3],int C[][3])
		{
	int i,j, product;
	
		for(i=0;i<3;i++);
		{
				for(j=0;j<3;j++);
			
				

				product=(A[i][0]*B[0][j]+A[i][1]*B[1][j]+A[i][0]*B[0][j]);
				
				C[i][j]=product;
				
				cout<<C[i][j];
				cout<<endl;
		

		}

	return ;
	
}

Kindly help me with a way to call the function for this multiplication.
Why is it that when you make the array range a global constant, the program does to work out i.r
const int SIZE=3, SIZE=3;
for both i and j?
Thanks in advance

I am seeing a problem in the following two lines:

void Times(int A[][3], int B[][3],int C[][3]);//prototype int first, int last);

cout<<"The product is:"<< Times(K,3,3) <<endl;

the protoype calls for the matricies and you are passing it a matrix and two constants...

but what other problems are you having with it?

My Major problem is how I can call the fuction with the two arrays: I couldn't figure out a syntax for it. Let me have an example of how to call this function.
Thank you

Let me have an example of how to call this function.
Thank you

Very simple way to pass them:

void myFunction(arg1 *[], arg2 *[], arg3 *[]);//prototype - doesn't have
                          //to be pointer array, but I prefer this notation

int myArray[3][3], myArray2[3][3], myArray[3][3];//create my variables

myFunction(myArray, myArray2, myArray3);//function call

Does that show you how to call the function using the three arrays?

Very simple way to pass them:

void myFunction(arg1 *[], arg2 *[], arg3 *[]);//prototype - doesn't have
                          //to be pointer array, but I prefer this notation

int myArray[3][3], myArray2[3][3], myArray[3][3];//create my variables

myFunction(myArray, myArray2, myArray3);//function call

Does that show you how to call the function using the three arrays?

Got it,
Thanks.
My next question is:
I want to overload the operations *+and_ using the class Mat and two class objects of mat to perform the above operations to a matrix of 3 by 3.
How do I pass the function to the defination part
i.e

class Mat
{
public:
Mat;
void setmat();
void display();
operator*(Mat M);
operator+(Mat A);
operator -(Mat S);
private:
int arrayz[3][3];
};
int main()
// Is this output right?
Mat M, N;

Mat K=M+N;
K.display()

K=M*N;
K.display

K=M-N
K.display

Mat Mat::operator +(Mat A)
{
//how do you pass this to main for all the overloaded operators?
}

I will high;y appreciate your help.
Thanks

Got it,
How do I pass the function to the defination part

I'm not sure what you mean by this...

int main()
// Is this output right?

I don't see any output... also, you don't have any curly brackets defining the main() function

Mat Mat::operator +(Mat A)
{
//how do you pass this to main for all the overloaded operators?
}

how do you pass this to main??? do you mean how do you call this function from main? You already did it using:

Mat K = M+N;

this is the same thing as:

Mat K.operator=((M.operator+(N)));

And where are you learning C++ from? School? Book?

And where are you learning C++ from? School? Book?

I use my head, well it has brains:) and heresay combined. Seems there is a gap in both, otherwise thanks:
I just realised I didnt formulate the question in a right way.

How do I access the private variable of an array in the function defination?
Thanks

Edited 3 Years Ago by Dani: Formatting fixed

How do I access the private variable of an array in the function defination?
Thanks

do you mean a private data member that is an array in your class?
since the function is a function of the class, it has access to it as if it were a global variable. Just use it's name:

Mat Mat::operator+(Mat d)
{
     myVar += d.myVar;
     //...
}
Mat Mat::operator+(Mat d)
{
     myVar += d.myVar;
     //...
}

Thanks . This is quite helpful. Just wondering though, how do you use the setmat to initialize the matrix objects?
Thanks again.
The setmat is a public function in the class of Mat.

Edited 3 Years Ago by Dani: Formatting fixed

Well, using what I've given you, it seems you need to combine two things... how to use private data members in a function.. and how to call the function... The rest is just writing the function to do so.

This question has already been answered. Start a new discussion instead.