C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 1st, 2006, 11:17 PM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
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++;
}
}

Reply With Quote
  #2  
Old October 2nd, 2006, 01:16 AM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
You know how to print things, you do it later in your code.
Code:
cout << deck[j];

Reply With Quote
  #3  
Old October 2nd, 2006, 05:11 PM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
Quote:
Originally Posted by ubergeek
You know how to print things, you do it later in your code.
Code:
cout << deck[j];


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

Reply With Quote
  #4  
Old October 2nd, 2006, 05:15 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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.

Reply With Quote
  #5  
Old October 2nd, 2006, 06:11 PM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
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++;
}
}

Reply With Quote
  #6  
Old October 2nd, 2006, 06:18 PM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
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. )

Reply With Quote
  #7  
Old October 2nd, 2006, 07:25 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
There is no int::equals() function...just use the comparison operator ==.

Reply With Quote
  #8  
Old October 3rd, 2006, 02:12 AM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
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'

Reply With Quote
  #9  
Old October 3rd, 2006, 06:07 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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.

Reply With Quote
  #10  
Old October 4th, 2006, 01:05 AM
kolorowy kolorowy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 27 kolorowy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 37 m 12 sec
Reputation Power: 0
AH!!! You right. How I did not see it. Duhh

Thanks

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with print function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT