| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with print function
I have problem and I just can't get it
I have this fuction wich deals 52 card Deck:: Deck() { srand(time(0)); // seed the random number generator for (int i = 0; i < NUMSUITS; i++) for (int j = 0; j < NUMNUMS; j++) deck[(i * NUMNUMS) + j] = Card(suits[i], nums[j]); } then I want to print this to the screen with this funcion. void Deck:: printDeck() const { int count =1; for(int j= 0; j < 52; j++) { Deck::deck[j]; // << this I do not know // where to put or how to // print it. cout << "\t"; if(count == 13) { cout << endl; count = 0; } count++; } } |
|
#2
|
|||
|
|||
|
You know how to print things, you do it later in your code.
Code:
cout << deck[j]; |
|
#3
|
|||
|
|||
|
Quote:
if I use that I get this message: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const Card' (or there is no acceptable conversion) if I use cout << deck ; then it prints in hexadecimal |
|
#4
|
|||
|
|||
|
Oh, my bad, I didn't realize deck was of the type Card. You need to define a function of Card that will return a string or an int, allowing you to print it out.
|
|
#5
|
|||
|
|||
|
Ok this is what I did
It changed my program some becouse I have to define two getFuncion for "suit" and "num" It works fine. void Deck: : printDeck() { int count = 1; for(int j= 0; j < 52; j++) { //cout << deck[j] << " "; cout << deck[j].getSuit() << deck[j].getNum() << " "; if(count == 13) { cout << endl; count = 0; } count++; } } |
|
#6
|
|||
|
|||
|
But now I have another problem.
I get this error. Don't know how to fix it. error C2228: left of '.equals' must have class/struct/union type is 'int' This is for both funcions equals and isLessThan Funcion compCards is deck.cpp and I am trying to call equals and isLessThan int Deck:: compCards() const { int c1, c2; c1 = rand() % 52; c2 = rand() % 52; if(c1.equals(c2)) return true; if(c1.isLessThan(c2)) return true; } Both boolean funcions are in card.cpp bool Card::equals(const Card &c2) const { return (num == c2.num && suit == c2.suit); } bool Card::isLessThan(const Card &c2) const { if (suit > c2.suit) return true; if (num > c2.num) return true; if (num <= c2.num) return false; if (suit <= c2.suit) return false; // if the ranks are also equal, return false } Now any idea how to fix this problem?? I'm kind of stuck here. I'm also not positive about those boolean funcions if it will work since I have to do this: (Relative order of strength of the cards is determined by both suit and number. As is standard, * 'C' weaker than 'D' weaker than 'H' weaker than 'S' (i.e., any Club is weaker than any Diamond which in turn is weaker than any Heart, and so on). * Within a suit, '2' weaker than '3' weaker than ... '9' weaker than 'T' weaker than 'J' weaker than 'Q' weaker than 'K' weaker than 'A'. For instance, C2 weaker than C8 weaker than CA weaker than D2. ) |
|
#7
|
|||
|
|||
|
There is no int::equals() function...just use the comparison operator ==.
|
|
#8
|
|||
|
|||
|
No. The equal() funcion and isLessThan() funcion I had to write myself
this is where it is at // filename: card.cpp #include <iostream> #include <cstring> using namespace std; #include "card.h" Card:: Card() { suit = 'C'; num = '2'; } Card:: Card(char s, char n) { suit = s; num = n; } void Card:: printCard() const { cout << suit << num; } bool Card:: equals(const Card &c2) const { return (num == c2.num && suit == c2.suit); } bool Card:: isLessThan(const Card &c2) const { // first check the suits if (suit > c2.suit) return true; if (num > c2.num) return true; if (num <= c2.num) return false; if (suit <= c2.suit) return false; } Now I need to call it form my compCard() funcion which is in // filename: deck.cpp #include <iostream> #include <ctime> #include <cstdlib> using namespace std; #include "deck.h" Deck:: Deck() { srand(time(0)); // seed the random number generator for (int i = 0; i < NUMSUITS; i++) for (int j = 0; j < NUMNUMS; j++) deck[(i * NUMNUMS) + j] = Card(suits[i ], nums[j]); } void Deck:: printDeck() { int count = 1; for(int j= 0; j < 52; j++) { //cout << deck[j] << " "; cout << deck[j].getSuit() << deck[j].getNum() << " "; if(count == 13) { cout << endl; count = 0; } count++; } } void Deck:: Shuffle() { srand(time(0)); Card temp; int r; for(int i = 1; i < 53; i++) { r = rand() % 52; temp = deck[0]; deck[0] = deck[r]; deck[r] = deck[51]; deck[51] = temp; } } int Deck:: compCards() const { char rc1, rc2; rc1 = rand() % 52; rc2 = rand() % 52; if (rc1.equals(rc2)){ return 0; }else if (rc1.isLessThan(rc2)) { return 1; }else { return 2; } } And this should work fine compCards() funcion should read equal() and isLessThan() funcion. But I can't compile I get this message for c:\csc309\homework4\deck.cpp(60) : error C2228: left of '.equals' must have class/struct/union type is 'char' c:\csc309\homework4\deck.cpp(62) : error C2228: left of '.isLessThan' must have class/struct/union type is 'char' |
|
#9
|
|||
|
|||
|
But look. the .equals() and .isLessThan() functions are defined for the type Card. Then you try to call the method char::equals(), which doesn't exist. You need to create Card objects to call the Card::equals() function.
|
|
#10
|
|||
|
|||
|
AH!!! You right. How I did not see it. Duhh
Thanks |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with print function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|